Linux – Shell Scripts (Faq)

Linux Shell Scripts

1. Command to check number of arguments passed to shell scripts.

echo $#

2. Command to list all arguments passed to shell scripts.

echo $*

3. How to check if previous command execute successfully

echo $?
if equal to 0, then it was successful, if anyother number then error.

4. Basic linux command to add value

[[email protected] scripts]$ cat first.sh
echo “Command: $0” # this is to print the command or filename
echo “one $1” # this line is to get first parameter
echo “two $2” # this line is to get second parameter
echo $* # this line is to print all parameters that is passed
echo $# # this line is to print the number of parameters that is passed

c=`expr $1 + $2`
echo “The result is :” $c

Result :
———[[email protected] scripts]$ ./first.sh 20 30
Command: ./first.sh
one 20
two 30
20 30
2
The result is : 50