脚本包含了一个计数集,用户将其赋予一个新值就可改变它,脚本然后将当前值100加入一个新值。 用户输入一个新值改变其值,如果输入回车键,则不改变它,打印当前值,脚本退出。如果用户用y或Y响应新值,将提示用户输入变量。如果键入回车键,原址人未变。加入一个增量,首先测试是否为数字,如果是,加入计数COUNTOR中,然后显示新值。
- #!/bin/bash
-
# ifcounter.sh
-
COUNTER=100
-
echo "Do you wish to change the counter value crretnly set at $COUNTER ?[y..n]"
-
read ANS
-
if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]
-
#yes user wants to change the value
-
then
-
echo "enter a sensible value"
-
read VALUE
-
# simple test to see if it's numeric,add any number to VALU
-
# then check out return
-
# code
-
expr $VALUE + 10 > /dev/null 2>&1
-
STATUS=$?
-
# check return code of the expr
-
if [ "$STATUS" = "" ] || [ "$STATUS" != "0" ]
-
# send errors to standard error
-
then
-
echo "you either entered nothing or a non-numeric" >&2
-
echo "sorry now exiting..counter stays at $COUNTER" >&2
-
exit 1
-
fi
-
# if we are here,then it's a numer,so add it to COUNTER
-
COUNTER=$(expr $COUNTER + $VALUE)
-
echo "Counter stays at $COUNTER"
-
else
-
# if we are here then user just hit return instedad of entering a number
-
# or answered n to the change a value prompt
-
echo "Counter stays at $COUNTER"
-
fi
- ywx@ywx:~/Desktop/linux_shell$ ./ifcounter.sh
-
Do you wish to change the counter value crretnly set at 100 ?[y..n]
-
y
-
enter a sensible value
-
569
-
Counter stays at 669
阅读(1311) | 评论(0) | 转发(0) |