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.

I am new to bash scripting (not programming in general).

I am writing a bash script that will run a Python script I have written.

I want to be able to do the following:

  • Pass parameters to the bash script via the cronjob (so I can have two cron jobs) one to be run with parameter 'foobar', and the other 'foo'

  • switch based on the parameter passed to the bash script (by switching, I mean an if/else based on the paramter passed to the bash script).

share|improve this question

3 Answers 3

up vote 2 down vote accepted

I was thinking of referring you to stackoverflow, but this is such low level 'programming' it may be best to just do it here.

The first parameter to a shell script is referenced by $1, the second $2, etc... The tenth can't be called $10 because that's $1 followed by a 0, so you refer to it as ${10}, though if you need to refer to the tenth parameter explicitly (and not handling it in a loop) you're probably doing something wrong.

The count of parameters is in the variable $#

I'd suggest:

if [ $# -ne 1 ]
then
    echo "No parameter given" >&2
    exit 1
elif [ "$1" == 'foo' ]
then
    ./script someparams ....
elif [ "$1" == 'foobar' ]
then
    ./script otherparams ....
else
    echo "Bad parameter given" >&2
    exit 1
fi
share|improve this answer
    
Always put double quotes around parameter substitutions, otherwise the shell will split them into words. (Yeah, I know, now you don't even imagine putting special characters there, but tomorrow you'll put a space in a file name and wonder why your scripts break, or you'll do this in a CGI script and wonder why your website was cracked.) –  Gilles Dec 7 '10 at 23:48
    
You can use a case statement to avoid a long sequence of if and elif if you're going to have more than just a few options. –  Dennis Williamson Dec 8 '10 at 0:51

Another form for if..elif..elif..else..fi is the case statement

case "$1" in
  foobar) 
    ./script.py arg1 arg2
    ;;
  foo)
    ./script.py arg3 arg4
    ;;
  *)
    echo "unknown argument: $1"
    ;;
esac

case uses glob-style matching, so the * means "everything else"

You'll find that bash is a pretty finicky language, where quoting or not quoting a variable can cause big differences in how your script runs. Get into the habit of quoting all your variables.

share|improve this answer

You can also use getopts for a more flexible way of passing arguments to bash script. By using getopts you can remove the fixed ordering associated with the arguments you are passing, which has the risk of passing wrong data if you forget the order of argument.

share|improve this answer
    
Passing arguments works jus the same, you simply change how you receive/process them. –  Daniel Beck Jul 2 '11 at 14:43

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.