views:

33

answers:

2

Hey,

I'm writing a shell script (tcsh) that is supposed to received 3 parameters or more. The first 3 are to be passed to a program, and the rest are supposed to be passed to another program. All in all the script should look something like:

./first_program $1 $2 $3
./second program [fourth or more]

The problem is that I don't know how to do the latter - pass all parameters that are after the third.

+1  A: 

I present to you the shift command:

shift [variable]
Without arguments, discards argv[1] and shifts the members of argv to the left.

Example:

$ cat /tmp/tcsh.sh
#!/bin/tcsh

echo "$1" "$2" "$3"
shift
shift
shift
echo "$*"
$ /tmp/tcsh.sh 1 2 3 4 5 6
1 2 3
4 5 6
John Kugelman
+1  A: 

You can make use of shift as follows:

./first_program $1 $2 $3

shift # shift 3 times to remove the first 3 parameters.
shift
shift

./second program $* 

$* will contain the remaining parameters.

You must also do error checking before you do a shift by checking $#argv and ensuring it is non-zero. Alternatively you can check the value of $#argv at the star of the script and ensure that it is atleast 3.

codaddict