两非空数求和t1.sh

写一个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

习题集:http://www.jb51.net/article/51574.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值