写一个shell脚本,进行两个数字的相加,如果没有输入参数就输出错误信息和一行使用说明
最简洁的:
#!/bin/bash
if [ $# -eq 0 ];then
echo "Error :There is no input."
exit 1
fi
if [ $# -eq 1 ];then
echo "Error :Misss one input.You shoud use $0 digital1 digital2"
exit 1
fi
a=$1
b=$2
let s=a+b
echo "$1 and $2 sum is : $s"
或者
#!/bin/sh
echo "Please input the first value:"
read a
test -z "$a"
flag=$?
if [ $flag != 0 ] ;then
echo "Please input the second value:"
else
echo "Error: Miss the first value."
exit
fi
read b
test -z "$b"
flag=$?
if [ $flag != 0 ]; then
sum=$((a+b))
echo "The above two values' sum is"
echo "$sum"
else
echo "Error:Miss the second value."
fi
类似的:
#!/bin/bash
echo -n "Please input the 1st digital:"
read a
echo "You input digital is: $a"
test -z "$a"
flag=$?
if [ $flag == 0 ];then
echo "Error:You didn't input the 1st value."
exit 1
fi
echo -n "Please input the 1st digital:"
read b
echo "You input digital is: $b"
test -z "$b"
flag=$?
if [ $flag == 0 ];then
echo "Error:You didn't input the 2nd value."
exit 1
fi
s=$((a+b))
echo "$a and $b sum is $s"
exit 0