Sunday, 18 August 2013

How can I make a Bash Script take an input before the script is invoked?

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?
edit: 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:
http://i.imgur.com/oiflGnL.png, sorry if it's not very good Bash, I'm a
Python programmer! Also that it's in img format, I'm working through a
virtual machine with no copy paste access. I'll start typing it up here in
text form now.
edit: here's my code in text form
#!/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

No comments:

Post a Comment