tags:

views:

80

answers:

5

What is the purpose of this bash script? (It is a portion of a larger script.)

if [ $# -gt 0 ]
then
  case $1 in
  -*) ;;
  *) exec $* ;;
esac
fi

A related question:

http://stackoverflow.com/questions/2046762/problem-with-metamap-inappropriate-ioctl-for-device

+1  A: 

Not knowing any bash scripting I'd say this

  • looks for whether the number of arguments is larger than 0
  • if it is, it looks at the first argument
    • If it starts with - it does nothing
    • Otherwise it executes all arguments as a single command line
Joey
+1  A: 

The case ... esac part is a switch statement. If $1 matches against -* (that is if it starts with -) the first case will be executed - and will do nothing. Otherwise (if $1 matches *, which depending on shell setting might exclude things starting with .) exec $* will be run.

Around that there is an if statement making sure that the switch is only executed if there actually are any parameters to be checked against (the parameter count is greater than zero).

sth
+7  A: 

In English, line-by-line:

if the number of arguments is greater than 0
then
if the first argument...
  starts with '-', do nothing
  else, "exec" the arguments (run the entire set of arguments as a command replacing this process, not as a child process)
(end of case)
(end of if)
Laurence Gonsalves
A: 

It takes the first argument passed in and executes it with the remaining arguments I.E.:

./script.sh ls dir1 dir2

would act as if you had typed

ls dir1 dir2
zipcodeman
A: 

If the first parameter placed on the command-line for this script is a file, not an option, then try to run it as an executable file or script.

Alex Brown