当前位置:主页 > 服务器 > Linux >

一条命令让你明白shell中read命令的常用参数

时间:2021-04-18 09:48:05 | 栏目:Linux | 点击:

我们知道,在Shell中接收传入的参数有两种方式。一种是通过脚本进行参数传递,另外一种是通过read来接收传入的参数。通过脚本来传递参数的简单示例如下:

# 通过脚本来传递,这里$0指脚本名,$1为第一个参数,$2为第二个参数
[root@host ~]# ./script.sh 1 2
Total = 3
[root@host ~]# vim script.sh
#!/bin/bash
function add() {
  total=$(expr $1 + $2)
  echo -e "Total = $total"
}
add $1 $2

再来看通过read来接收传入的参数,先看read的基本格式:

read [-rs] [-a ARRAY] [-d delim] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [var1 var2 var3......]

[root@host ~]# ./script.sh 
Enter Password:
The password your input is: Test@1234\
[root@host ~]# vim script.sh
#!/bin/bash
read -n10 -t30 -r -s -d $ -p "Enter Password:" password
echo -e "\nThe password your input is:$password"

从上面一个例子,基本上囊括了上面的大部分常用功能,特别是-p,-n,-t,-s等参数,可以很好的学习read这个命令

总结

您可能感兴趣的文章:

相关文章