Linux shell 学习笔记(10)— 处理用户输入(命令行读取参数、读取用户输入、超时处理)
1. 命令行参数
向 shell 脚本传递数据的最基本方法是使用命令行参数。命令行参数允许在运行脚本时向命令行添加数据。
$ ./addem 10 30
本例向脚本 addem 传递了两个命令行参数(10 和 30)。
1.1 读取参数
bash shell 会将一些称为位置参数(positional parameter)的特殊变量分配给输入到命令行中的所有参数。这也包括 shell 所执行的脚本名称。位置参数变量是标准的数字:$0 是程序名,$1是第一个参数,$2 是第二个参数,依次类推,直到第九个参数 $9。
使用单个命令行参数的简单例子
$ cat test1.sh #!/bin/bash # using one command line parameter # factorial=1 for (( number = 1; number <= $1 ; number++ )) dofactorial=$[ $factorial * $number ] done echo The factorial of $1 is $factorial $ $ ./test1.sh 5 The factorial of 5 is 120 $
可以在 shell 脚本中像使用其他变量一样使用 $1 变量。shell 脚本会自动将命令行参数的值分配给变量,不需要你作任何处理。
如果需要输入更多的命令行参数,则每个参数都必须用空格分开。
$ cat test2.sh #!/bin/bash # testing two command line parameters # total=$[ $1 * $2 ] echo The first parameter is $1. echo The second parameter is $2. echo The total value is $total. $ $ ./test2.sh 2 5 The first parameter is 2. The second parameter is 5. The total value is 10. $
也可以在命令行上用文本字符串。
$ cat test3.sh #!/bin/bash # testing string parameters # echo Hello $1, glad to meet you. $ $ ./test3.sh Rich Hello Rich, glad to meet you. $
记住,每个参数都是用空格分隔的,所以 shell 会将空格当成两个值的分隔符。要在参数值中
包含空格,必须要用引号(单引号或双引号均可)。$ ./test3.sh 'Rich Blum' Hello Rich Blum, glad to meet you. $ $ ./test3.sh "Rich Blum" Hello Rich Blum, glad to meet you. $
如果脚本需要的命令行参数不止 9 个,你仍然可以处理,但是需要稍微修改一下变量名。在
第 9 个变量之后,你必须在变量数字周围加上花括号,比如 ${10}。$ cat test4.sh #!/bin/bash # handling lots of parameters # total=$[ ${10} * ${11} ] echo The tenth parameter is ${10} echo The eleventh parameter is ${11} echo The total is $total $ $ ./test4.sh 1 2 3 4 5 6 7 8 9 10 11 12 The tenth parameter is 10 The eleventh parameter is 11 The total is 110 $
1.2 读取脚本名
可以用 $0 参数获取 shell 在命令行启动的脚本名。
$ cat test5.sh
#!/bin/bash
# Testing the $0 parameter
#
echo The zero parameter is set to: $0
#
$
$ bash test5.sh
The zero parameter is set to: test5.sh
$
当传给 $0 变量的实际字符串不仅仅是脚本名,而是完整的脚本路径时,变量 $0 就会使用整个路径。
basename 命令会返回不包含路径的脚本名。
$ cat test5b.sh
#!/bin/bash
# Using basename with the $0 parameter
#
name=$(basename $0)
echo
echo The script name is: $name
#
$ bash /home/Christine/test5b.sh
The script name is: test5b.sh
$
$ ./test5b.sh
The script name is: test5b.sh
$
2. 特殊参数变量
2.1 参数统计
特殊变量 $# 含有脚本运行时携带的命令行参数的个数。可以在脚本中任何地方使用这个特殊变量,就跟普通变量一样。
$ cat test8.sh
#!/bin/bash
# getting the number of parameters
#
echo There were $# parameters supplied.
$
$ ./test8.sh
There were 0 parameters supplied.
$
$ ./test8.sh 1 2 3 4 5
There were 5 parameters supplied.
$
$ ./test8.sh 1 2 3 4 5 6 7 8 9 10
There were 10 parameters supplied.
$
$ ./test8.sh "Rich Blum"
There were 1 parameters supplied.
$
2.2 抓取所有的数据
$* 变量会将命令行上提供的所有参数当作一个单词保存。这个单词包含了命令行中出现的每一个参数值。基本上 $* 变量会将这些参数视为一个整体,而不是多个个体。
$@ 变量会将命令行上提供的所有参数当作同一字符串中的多个独立的单词。
下面的例子给出了二者的差异。
$ cat test12.sh
#!/bin/bash
# testing $* and $@
#
echo
count=1
#
for param in "$*"
doecho "\$* Parameter #$count = $param"count=$[ $count + 1 ]
done
#
echo
count=1
#
for param in "$@"
doecho "\$@ Parameter #$count = $param"count=$[ $count + 1 ]
done
$
$ ./test12.sh rich barbara katie jessica
$* Parameter #1 = rich barbara katie jessica
$@ Parameter #1 = rich
$@ Parameter #2 = barbara
$@ Parameter #3 = katie
$@ Parameter #4 = jessica
$
$* 变量会将所有参数当成单个参数,而 $@ 变量会单独处理每个参数。
3. 移动变量
shift 命令会根据它们的相对位置来移动命令行参数。
在使用 shift 命令时,默认情况下它会将每个参数变量向左移动一个位置。所以,变量 $3 的值会移到 $2 中,变量 $2 的值会移到 $1中,而变量 $1 的值则会被删除(注意,变量 $0 的值,也就是程序名,不会改变)。
$ cat test13.sh
#!/bin/bash
# demonstrating the shift command
echo
count=1
while [ -n "$1" ]
doecho "Parameter #$count = $1"count=$[ $count + 1 ]shift
done
$
$ ./test13.sh rich barbara katie jessica
Parameter #1 = rich
Parameter #2 = barbara
Parameter #3 = katie
Parameter #4 = jessica
$
窍门 使用 shift 命令的时候要小心。如果某个参数被移出,它的值就被丢弃了,无法再恢复。也可以一次性移动多个位置,只需要给 shift 命令提供一个参数,指明要移动的位置数就行了。
$ cat test14.sh
#!/bin/bash
# demonstrating a multi-position shift
#
echo
echo "The original parameters: $*"
shift 2
echo "Here's the new first parameter: $1"
$
$ ./test14.sh 1 2 3 4 5
The original parameters: 1 2 3 4 5
Here's the new first parameter: 3
$
4. 处理选项
待补充
5. 获得用户输入
5.1 基本的读取
read 命令从标准输入(键盘)或另一个文件描述符中接受输入。在收到输入后,read 命令会将数据放进一个变量。
$ cat test21.sh
#!/bin/bash
# testing the read command
#
echo -n "Enter your name: "
read name
echo "Hello $name, welcome to my program. "
#
$
$ ./test21.sh
Enter your name: Rich Blum
Hello Rich Blum, welcome to my program.
$
生成提示的 echo 命令使用了 -n 选项。该选项不会在字符串末尾输出换行符,允许脚本用户紧跟其后输入数据,而不是下一行。
实际上,read 命令包含了 -p 选项,允许你直接在 read 命令行指定提示符。
$ cat test22.sh
#!/bin/bash
# testing the read -p option
#
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That makes you over $days days old! "
#
$
$ ./test22.sh
Please enter your age: 10
That makes you over 3650 days old!
$
read 命令会将姓和名保存在同一个变量中。read 命令会将提示符后输入的所有数据分配给单个变量,要么你就指定多个变量。输入的每个数据值都会分配给变量列表中的下一个变量。如果变量数量不够,剩下的数据就全部分配给最后一个变量。
$ cat test23.sh
#!/bin/bash
# entering multiple variables
#
read -p "Enter your name: " first last
echo "Checking data for $last, $first…"
$
$ ./test23.sh
Enter your name: Rich Blum
Checking data for Blum, Rich...
$
也可以在 read 命令行中不指定变量。如果是这样,read 命令会将它收到的任何数据都放进特殊环境变量 REPLY 中。
$ cat test24.sh
#!/bin/bash
# Testing the REPLY Environment variable
#
read -p "Enter your name: "
echo
echo Hello $REPLY, welcome to my program.
#
$
$ ./test24.sh
Enter your name: Christine
Hello Christine, welcome to my program.
$
REPLY 环境变量会保存输入的所有数据,可以在 shell 脚本中像其他变量一样使用。
5.2 超时
-t 选项指定了 read 命令等待输入的秒数。当计时器过期后,read 命令会返回一个非零退出状态码。
$ cat test25.sh
#!/bin/bash
# timing the data entry
#
if read -t 5 -p "Please enter your name: " name
then
echo "Hello $name, welcome to my script"
else
echo
echo "Sorry, too slow! "
fi
$
$ ./test25.sh
Please enter your name: Rich
Hello Rich, welcome to my script
$
$ ./test25.sh
Please enter your name:
Sorry, too slow!
$
5.3 隐藏方式读取
-s 选项可以避免在 read 命令中输入的数据出现在显示器上(实际上,数据会被显示,只是 read 命令会将文本颜色设成跟背景色一样)
$ cat test27.sh
#!/bin/bash
# hiding input data from the monitor
#
read -s -p "Enter your password: " pass
echo
echo "Is your password really $pass? "
$
$ ./test27.sh
Enter your password:
Is your password really T3st1ng?
$
输入提示符输入的数据不会出现在屏幕上,但会赋给变量,以便在脚本中使用。
5.4 从文件中读取
也可以用 read 命令来读取 Linux 系统上文件里保存的数据。每次调用 read 命令,它都会从文件中读取一行文本。当文件中再没有内容时,read 命令会退出并返回非零退出状态码。
最常见的方法是对文件使用 cat 命令,将结果通过管道直接传给含有 read 命令的 while 命令。
$ cat test28.sh
#!/bin/bash
# reading data from a file
#
count=1
cat test | while read line
do
echo "Line $count: $line"
count=$[ $count + 1]
done
echo "Finished processing the file"
$
$ cat test
The quick brown dog jumps over the lazy fox.
This is a test, this is only a test.
O Romeo, Romeo! Wherefore art thou Romeo?
$
$ ./test28.sh
Line 1: The quick brown dog jumps over the lazy fox.
Line 2: This is a test, this is only a test.
Line 3: O Romeo, Romeo! Wherefore art thou Romeo?
Finished processing the file
$
Linux shell 学习笔记(10)— 处理用户输入(命令行读取参数、读取用户输入、超时处理)相关推荐
- 【Linux】Linux Shell 学习笔记:什么是Linux?
I Linux 简介 Linux系统简介 (biancheng.net) Linux和UNIX的关系及区别 (biancheng.net) Linux内核_百度百科 (baidu.com) [什么是 ...
- linux shell 学习笔记
shell中定义变量形式: yourname='tengxi' 注意等号两边不能有空格,否则会报语法错误 使用变量 echo $yourname 或者 echo ${yourname}(推荐带花括号的 ...
- oracle命令解锁用户,在命令行下进行Oracle用户解锁
在DBA的日常工作中,经常遇到为Oracle用户解锁的操作:这篇文章给出在命令行下进行Oracle用户解锁的操作方法,通过几条简单的解锁语句就能完成此项工作.下面是具体的过程: 默认的scott用户是 ...
- oracle命令解锁用户,在命令行下进行Oracle用户解锁的语句
在DBA的日常工作中,经常遇到为Oracle用户解锁的操作:这篇文章给出在命令行下进行Oracle用户解锁的操作方法,通过几条简单的解锁语句就能完成此项工作.下面是具体的过程: 默认的scott用户是 ...
- Linux | Shell 学习笔记(二)Shell 流程控制 if、case、for、while| read读取输入 | 函数的使用 | cut、sed、awk、sort命令 +Demo
文章目录 参考资料 运行环境 一.流程控制 1.1 if 判断 1.2 case 语句 1.3 for 循环 1.4 while 循环 二. read 读取控制台输入 三.函数 3.1 系统函数 ba ...
- Linux shell 学习笔记(5)— 文件权限(添加、修改、删除用户及创建、修改群组)
1. Linux的安全性 Linux 安全系统的核心是用户账户.每个能进入 Linux 系统的用户都会被分配唯一的用户账户.用户对系统中各种对象的访问权限取决于他们登录系统时用的账户. 用户权限是通过 ...
- Linux shell 学习笔记(7)— 构建基本脚本(变量、重定向、管道、状态码)
1. 使用多个命令 如果要两个命令一起运行,可以把它们放在同一行中,彼此间用分号隔开. $ date ; who Mon Feb 21 15:36:09 EST 2014 Christine tty2 ...
- Linux shell 学习笔记(1)— 文件和目录(查看、创建、复制、软硬链接、重命名及删除操作)
1. 启动 shell /etc/passwd 文件包含了所有系统用户账户列表以及每个用户的基本配置信息: christine:x:501:501:Christine Bresnahan:/home/ ...
- linux shell学习笔记(二) 变量和运算符
1.什么是shell变量?本地变量.环境变量.变量替换(显示变量).位置变量.标准变量.特殊变量.影响变量的命令 2.本地变量:本地变量在用户现在的shell生命期的脚本中使用 variable-na ...
最新文章
- 清华陈文光教授:AI 超算基准测试的最新探索和实践(附演讲视频)
- 一个权限树的设计与实现
- Html5table控件,Table Web 控件、TableRow Web 控件及TableCell Web 控件
- 8 FI配置-财务会计-把总账科目组的字段状态变式分配给公司代码
- mock-api 使用手册
- FD.io VPP:用户文档:VPP RPM包的构建与离线安装
- BERT4GCN:利用BERT中间层特征增强GCN进行基于方面的情感分类
- SVN实现自动更新(Windows平台)
- 码农辞职一年后:独立工程师太难了
- hammerJs-v2.0.4详解
- dw怎么保存HTML手机可以看,【dw网页制作】如何使用Dreamweaver制作网页?如何用Dreamweaver制作个人虚拟网站?dreamweaver如何制作手机网站?...
- HDFS BALANCER
- html发起微信或支付宝支付,vue实现-微信网页中唤起支付宝支付
- Cesium 相机视角控制
- jquery实现字数限制,超过部分...代替,后缀点击展开,点击后展开全文
- JAXWS CXF JAXB + MyEclipse + Maven Byron自學視頻04
- Emacs Lisp语言
- 行列式的子式、主子式、顺序主子式、余子式、代数余子式
- 06.奇特的一生评语
- ShardingSphere分库分表实战与核心原理