Linux SHELL编程小练习(2)
read命令的基本用法以及判断语句格式
read命令可以用来获取键盘输入,-p选项用于输出提示信息,-t选项设置时间限制,单位为秒,代码如下:
#!/bin/bash
#
if read -t 5 -p "Please input the argument : " NUMBER_II
then
if [ -z $NUMBER_II ]
then
echo "You inputed nothing!"
exit
else
echo What you typed in was : $NUMBER_II
exit
fi
else
echo ""
echo "You are too slow!"
exit
fi
说明:输入什么就打印什么,输入为空会打印提示,超过5s未输入输出提示并退出进程。
执行结果:
root@MCB:~# bash sh.sh
Please input the argument : 123
What you typed in was : 123
root@MCB:~#
root@MCB:~#
root@MCB:~# bash sh.sh
Please input the argument : abc
What you typed in was : abc
root@MCB:~#
root@MCB:~#
root@MCB:~#
root@MCB:~# bash sh.sh
Please input the argument :
You inputed nothing!
root@MCB:~#
root@MCB:~#
root@MCB:~#
root@MCB:~# bash sh.sh
Please input the argument :
You are too slow!
if语句的用法:
if后跟一个逻辑判断式,当其结果为真时,执行then语句后面的代码;当其结果为假时,执行else语句后面的代码,一个if语句对应一个fi语句。