菜鸟请教shell编程问题 case if else
小弟编写的脚本,请用户先输入了一个数字,然后再输入一个运算符号,接着输入第二个数字,而后得出结果[root@mailonesh]#cat-nd.sh1#!/bin/bas...
小弟编写的脚本,请用户先输入了一个数字,然后再输入一个运算符号,接着输入第二个数字,而后得出结果
[root@mailone sh]# cat -n d.sh
1 #!/bin/bash
2 read -p "please input the first number:" first_num
3 read -p "please input example + - * /:" a
4 test "$a" != '+' && (echo you are using a valid parameter;exit)
5 read -p "please input the second number:" second_num
6 case $a in
7 "+")
8 result=$(($first_num+$second_num))
9 ;;
10 -)
11 result=$(($first_num-$second_num))
12 ;;
13 /)
14 result=$(($first_num/$second_num))
15 ;;
16 *)
17 result=$(($first_num*$second_num)) ;;
18 esac
19 echo $first_num $a $second_num = $result.
这已经达到预期目的,要继续挑战下自己的水平,希望在脚本第三行之后验证用户输入的运算符号是否符合(+ - * /),如果不符合的话,提醒用户echo "you are inputing a invalid parameter",并中止程序(不继续脚本其它内容的执行).
小弟尝试用if elif
if [ "$a" != '+' ];then
echo "you are inputing a invalid parameter"
elif [ "$a" != '-' ];then
echo "you are inputing a invalid parameter"
感觉逻辑存在问题
改用
test "$a" != '+' && (echo you are inputing a invalid parameter;exit 1))
test "$a" != '-' && (echo you are inputing a invalid parameter;exit 1))
也不行 展开
[root@mailone sh]# cat -n d.sh
1 #!/bin/bash
2 read -p "please input the first number:" first_num
3 read -p "please input example + - * /:" a
4 test "$a" != '+' && (echo you are using a valid parameter;exit)
5 read -p "please input the second number:" second_num
6 case $a in
7 "+")
8 result=$(($first_num+$second_num))
9 ;;
10 -)
11 result=$(($first_num-$second_num))
12 ;;
13 /)
14 result=$(($first_num/$second_num))
15 ;;
16 *)
17 result=$(($first_num*$second_num)) ;;
18 esac
19 echo $first_num $a $second_num = $result.
这已经达到预期目的,要继续挑战下自己的水平,希望在脚本第三行之后验证用户输入的运算符号是否符合(+ - * /),如果不符合的话,提醒用户echo "you are inputing a invalid parameter",并中止程序(不继续脚本其它内容的执行).
小弟尝试用if elif
if [ "$a" != '+' ];then
echo "you are inputing a invalid parameter"
elif [ "$a" != '-' ];then
echo "you are inputing a invalid parameter"
感觉逻辑存在问题
改用
test "$a" != '+' && (echo you are inputing a invalid parameter;exit 1))
test "$a" != '-' && (echo you are inputing a invalid parameter;exit 1))
也不行 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
展开全部
#
# 用一个while loop 直到不想玩了为止
#
while :
do
#
# ask user to enter the 1st number
#
read -p "please input the first number (or 'q' to quit): " first_num
if [ "$first_num" == "q" ]
then
echo "bye!"
exit
else
# check if the input is all digits
re='^[0-9]+$'
if ! [[ $first_num =~ $re ]]
then
echo "first_num is non-numeric, 重来!"
continue
fi
fi
#
# ask user to enter the 2nd number
#
read -p "please input the second number (or 'q' to quit): " second_num
if [ "$second_num" == "q" ]
then
echo "bye!"
exit
else
# check if the input is all digits
re='^[0-9]+$'
if ! [[ $second_num =~ $re ]]
then
echo "second_num is non-numeric, 重来!"
continue
fi
fi
#
# ask user to enter the operator (+,-,*,/)
#
read -p "please enter 运算符号(+,-,*,/): " operator
case $operator
in
'+') let result=$first_num+$second_num
echo "$first_num + $second_num = " $result
;;
'-') # 这些你自己可以做了
;;
'*') # 这些你自己可以做了
;;
'/') # 这些你自己可以做了
;;
*) # anything else is invalid
echo "Invalid operator, please enter '+','-','*','/', try again!"
;;
esac
done
# 用一个while loop 直到不想玩了为止
#
while :
do
#
# ask user to enter the 1st number
#
read -p "please input the first number (or 'q' to quit): " first_num
if [ "$first_num" == "q" ]
then
echo "bye!"
exit
else
# check if the input is all digits
re='^[0-9]+$'
if ! [[ $first_num =~ $re ]]
then
echo "first_num is non-numeric, 重来!"
continue
fi
fi
#
# ask user to enter the 2nd number
#
read -p "please input the second number (or 'q' to quit): " second_num
if [ "$second_num" == "q" ]
then
echo "bye!"
exit
else
# check if the input is all digits
re='^[0-9]+$'
if ! [[ $second_num =~ $re ]]
then
echo "second_num is non-numeric, 重来!"
continue
fi
fi
#
# ask user to enter the operator (+,-,*,/)
#
read -p "please enter 运算符号(+,-,*,/): " operator
case $operator
in
'+') let result=$first_num+$second_num
echo "$first_num + $second_num = " $result
;;
'-') # 这些你自己可以做了
;;
'*') # 这些你自己可以做了
;;
'/') # 这些你自己可以做了
;;
*) # anything else is invalid
echo "Invalid operator, please enter '+','-','*','/', try again!"
;;
esac
done
追问
长见识了,让人大开眼界。请问re='^[0-9]+$'中的+$换成*号代替可以吗?
还有if ! [[ $second_num =~ $re ]],if后面接一个!,=后面接一个约等号表示啥呢?感谢你的脚本。真是我的偶像呀。
追答
严格说来,不能用*必须用+,因为:
*是match 0 个或多个[0-9]
+是match 1 个或多个[0-9]
!是把后面得出的答案变成负的。
如果在右边的variable是个 Regular Expression (e.g. $re),那么就必须用 =~ 来执行 matching 的动作。
Shell脚本里面的Regular Expression非常重要,请参考:
http://code.linuxnix.com/2012/12/regular-expreesion-in-linux-bash-and-ksh-shells.html
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询