Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
1  
The arguments are $0 for the name of the program, $1, $2 … for other arguments, $# for number of arguments, $* for all arguments from 1, $@ as $*, but if in quotes, arguments are kept separate. Also the command shift will delete $1 and shift the others down, $2 becomes $1, $3 becomes $2 … –  richard Aug 18 '13 at 21:17

5 Answers 5

#!/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
share|improve this answer
    
thanks a lot, could you just take a quick look at my edit? I'm not exactly sure how to work this in for my situation. –  user Aug 18 '13 at 15:25
    
answer updated. –  Uwe Aug 18 '13 at 15:46

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"
share|improve this answer
    
thanks for the help, could you take a look at my edit? –  user Aug 18 '13 at 15:24

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
share|improve this answer
    
As you get more familiar with bash, look into replacing the if chain with a case statement. It tends to be cleaner and more readable/maintainable. Depending on your coding style, you might also like to use select: select name [ in word ] ; do list ; done which makes a primitive menu for you. –  Joe Aug 19 '13 at 23:15

As I understand, what you want is to get command-line arguments.

Take the example below:

#! /bin/bash
echo "Welcome to mybashcript!"
echo "Selected option: $1"

$1 represents the first command-line argument that is passed onto your program.

If we call the script above (lets use the name you had on your question, mybashscript) with login as the argument

$ mybashscript login

We get the output below:

Welcome to mybashscript!
Selected option: login

If more arguments are given, you can access them using $2, $3,..., and so on. But that is only useful when you have a fixed number of arguments. If you want to use a variable number of arguments, check this post on StackOverflow.

share|improve this answer
$ cat test.sh
#!/bin/bash
launch() {
    if [[ "$1" =~ ^(sign|view|save|erase)$ ]]; then
        echo python /usr/local/bin/guestbook.data/${1}_guestbook.py
    else
        [ -n "$1" ] && echo "Sorry, I didn't understand that."
        return 1
    fi
}
show_help() {
        echo -n "Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): "
}
launch "$1" && exit
show_help
while read option; do
        if launch "$option"; then
                break
        else
                show_help
        fi
done

Testing the code:

$ ./test.sh
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): a
Sorry, I didn't understand that.
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): sign
python /usr/local/bin/guestbook.data/sign_guestbook.py
$ ./test.sh
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): asign
Sorry, I didn't understand that.
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): view
python /usr/local/bin/guestbook.data/view_guestbook.py
$ ./test.sh a
Sorry, I didn't understand that.
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): save
python /usr/local/bin/guestbook.data/save_guestbook.py
$ ./test.sh erase
python /usr/local/bin/guestbook.data/erase_guestbook.py

Explanation:

[[ "$1" =~ ^(sign|view|save|erase)$ ]]; tests the input using regular expressions for more concise code, instead of an if-else ladder. The $1 refers to the first positional argument that this function receive, not the script itself. The test for [ -n "$1" ] is only to print the Sorry statement if there's some input entered.

launch "$1" && exit attempts to check for any input via $1, if it succeeds the script will exit.

Assumptions:

  1. The python script will exit with a successful status, which is how if launch "$option"; then ... will work. If this assumption is false and the exit condition checking is not improved, we will be stuck in a loop.
  2. We can use $1 to launch the correct script in the way ${1}_guestbook.py.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.