if [ $# -ne 2 ] # If two Inputs are not received from Standard Input
then # then execute the below statements
echo "Usage - $0 x y" # print on standard output, how-to use the script (Usage - ./1.sh x y )
echo " Where x and y are two nos for which I will print sum" # print on standard output, “Where x and y are two nos for which I will print sum ”
exit 1 # Leave shell in Error Stage and before the task was successfully carried out.
fi # End of the if Statement.
echo "Sum of $1 and $2 is `expr $1 + $2`" # If the above condition was false and user Entered two numbers as a command Line Argument, it will show the sum of the entered numbers.
给他添加可执行权限,并且执行。
1 2
# chmod 755 two-numbers.sh
情形一: 未输入两个数字作为命令行参数运行脚本,你将得到下面的输出。
样例输出
1 2 3 4 5
# ./two-numbers.sh
Usage - ./two-numbers.sh x y Where x and y are two nos for which I will print sum
#!/bin/bash if [ $# -ne 1 ] then echo "Usage: $0 number" echo " I will find reverse of given number" echo " For eg. $0 0123, I will print 3210" exit 1 fi
n=$1 rev=0 sd=0
while [ $n -gt 0 ] do sd=`expr $n % 10` rev=`expr $rev \* 10 + $sd` n=`expr $n / 10` done echo "Reverse number is $rev"
授予对文件的执行权限,并运行如下所示的脚本。
1 2
# chmod 755 numbers.h
情形一: 当输入不包含命令行参数,你将得到下面的输出。
样例输出
1 2 3 4 5 6
./numbers.sh
Usage: ./numbers.sh number I will find reverse of given number For eg. ./2.sh 123, I will print 321