How can I make a Bash Script take an input before the script is invoked?
Basically, you call my Bash Script by typing mybashscript
in console. Then this starts:
Welcome to mybashcript! Choose an option (login|logout|view|erase|quit|):
The user then types whichever input they want, which then activates a Python Script further down in each option's respective if tree.
What I'm wondering is, how can I make it so the user only has to type (if they've used the program before) something like mybashscript login
or mybashscript view
.
So whatever word is added after the name of the Bash script is then taken as the first input in the script itself. Is this possible?
Here is my script so far, I don't exactly understand how to incorporate $1 without also letting it ask if there isn't an argument.
#!/bin/bash
echo -n "Hello $USER, welcome to the guestbook! Choose an option (sign|view|save|erase): "
read option
if [ "$option" == "sign" ]; then
python /usr/local/bin/guestbook.data/sign_guestbook.py
elif [ "$option" == "view" ]; then
python /usr/local/bin/guestbook.data/view_guestbook.py
elif [ "$option" == "save" ]; then
python /usr/local/bin/guestbook.data/save_guestbook.py
elif [ "$option" == "erase" ]; then
python /usr/local/bin/guestbook.data/erase_guestbook.py
else
echo "Sorry, I didn't understand that."
fi
#!/bin/bash
case $# in
0) echo -n "Hello $USER, welcome to the guestbook! Choose an option (sign|view|save|erase): "
read option ;;
1) option=$1 ;;
*) echo "Sorry, I didn't understand that (too many command line arguments)"
exit 2 ;;
esac
if [ "$option" == "sign" ]; then
python /usr/local/bin/guestbook.data/sign_guestbook.py
elif [ "$option" == "view" ]; then
python /usr/local/bin/guestbook.data/view_guestbook.py
elif [ "$option" == "save" ]; then
python /usr/local/bin/guestbook.data/save_guestbook.py
elif [ "$option" == "erase" ]; then
python /usr/local/bin/guestbook.data/erase_guestbook.py
else
echo "Sorry, I didn't understand that."
fi
In your bash script $1
is the first argument, $2
is the second argument, and so forth.
e.g. if you have the following in test.sh
:
#!/bin/sh
echo You said \"$1\"
Then you will get the following:
user@host:~$ test.sh hello
You said "hello"
The way to take input from the command line and pass it off to your program is by using $1 (for the first word after the command), $2, $3 and so on.
For example, let's say you wrote this Bash Script (filename: testscript):
#!/bin/bash
echo $1
echo $2
echo $5
This would be the output when the script is called by a user from the console (remember to add x permissions so the script can be executed by the shell and put the script in /usr/local/bin/):
user:$ testscript how are you doing today?
how
are
today?
Thanks for all your help, it pushed me in the right direction. Here's what I ended up doing for my specific case if you were curious; it may not be the best or most elegant way to Bash Script, but I just started today!:
#!/bin/bash
if [ "$1" == "sign" ]; then
python /usr/local/bin/guestbook.data/sign_guestbook.py
elif [ "$1" == "view" ]; then
python /usr/local/bin/guestbook.data/view_guestbook.py
elif [ "$1" == "save" ]; then
python /usr/local/bin/guestbook.data/save_guestbook.py
elif [ "$1" == "erase" ]; then
python /usr/local/bin/guestbook.data/erase_guestbook.py
else
echo -n "Hello $USER, welcome to the guestbook! Choose an option (sign|view|save|erase): "
read option
if [ "$option" == "sign" ]; then
python /usr/local/bin/guestbook.data/sign_guestbook.py
elif [ "$option" == "view" ]; then
python /usr/local/bin/guestbook.data/view_guestbook.py
elif [ "$option" == "save" ]; then
python /usr/local/bin/guestbook.data/save_guestbook.py
elif [ "$option" == "erase" ]; then
python /usr/local/bin/guestbook.data/erase_guestbook.py
else
echo "Sorry, I didn't understand that."
fi
fi