When I am executing my really simple shell script below, I got an error:
`end of file' unexpected. Script is suppose to print out a list of supplied parameters.
Here is my script:
#!/bin/sh
a=$# #number or parameters
b=0 #starting counter
while [ $a > $b ]
do
b=`expr $b + 1`
echo $b
echo "Parameter is $[$b]"
done
Modified version of the script to process the input parameters in order:
Notice the use of "shift" to cause each parameter in turn to be called $1 .
#!/bin/sh
a=$# #number or parameters
b=0 #starting counter
while [ $a -gt $b ]
do
b=`expr $b + 1`
echo "Parameter $b is $1"
shift
done
./scriptname a b c
Parameter 1 is a
Parameter 2 is b
Parameter 3 is c
"getopts" is a good solution. It relies on a structured command line which follows normal unix syntax. No problem with that.
Each parameter starts with a hyphen character and can have an optional parameter value.
e.g. A script with two parameters called "-v" and "-z". The parameter "-v" has a value of "myvalue". The parameter "-z" does not have an associated value.