一、获取用户输入
1、基本读取
read命令接收标准输入的输入,或其它文件描述符的输入。得到输入后,read命令将数据输入放入一个标准变量中。
[root@rac2 ~]# cat t8.sh
#!/bin/bash
#testing the read command
echo -n "enter your name:" ---:-n用于允许用户在字符串后面立即输入数据,而不是在下一行输入。
read name
echo "hello $name ,welcome to my program."
[root@rac2 ~]# ./t8.sh
enter your name:zhou
hello zhou ,welcome to my program.
read命令的-p选项,允许在read命令行中直接指定一个提示:
[root@rac2 ~]# cat t9.sh
#!/bin/bash
#testing the read -p option
read -p "please enter your age:" age ---age与前面必须有空格
echo "your age is $age"
[root@rac2 ~]# ./t9.sh
please enter your age:10
your age is 10
2、在read命令中也可以不指定变量,如果不指定变量,那么read命令会将接收到的数据防止在环境变量REPLAY中
[root@rac2 ~]# cat t10.sh
#!/bin/bash
#tesing the replay environment variable
read -p "enter a number:"
factorial=1
for (( count=1; count<=$REPLY; count++ ))
do
factorial=$[ $factorial * $count ]
done
echo "the factorial of $REPLY is $factorial"
linux shell获取用户输入
于 2017-07-03 15:53:20 首次发布

最低0.47元/天 解锁文章
6035

被折叠的 条评论
为什么被折叠?



