Shell 语法之用户输入
bash shell 提供了一些不同的方法从用户处获取数据,这些方法包括命令行参数、命令行选项和直接读取键盘输入。
命令行参数
bash shell 将在命令行中输入的所有参数赋值给一些特殊变量,称为位置参数,通过标准数据表示,其中$0为程序名称,$1为第一个参数,$2为第二个参数,依此类推,直到$9为第九个参数。在第九个变量之后,必须使用大括号将变量括起来,如${10}。
示例
#!/bin/bash
# using one command line parameter
factorial=1
for (( number=1; number<=$1; number++ ))
do
factorial=$[ factorial * $number ]
done
echo The factorial of $1 is $factorial
调用:$ ./test1 5
The factorial of 5 is 120
读取程序名称
使用参数$0可以确定 shell 从命令行启动的程序的名称。
示例
#!/bin/bash
# testing the $0 parameter
echo The command entered is : $0
调用:./test5
如果需要去除通过命令行运行脚本时使用的所有路径,使用 basename 命令。
示例
#!/bin/bash
# using basename with the $0 parameter
name=`basename $0`
echo The command entered is: $name
调用:./test5b
The command entered is : ./test11-5
参数计数变量
特殊变量 $# 中存储执行脚本时包含的命令行参数的个数。
示例
#!/bin/bash
# testing parameters
if [ $# -ne 2 ]
then
echo Uage: test11-9 a b
else
total=$[ $1 + $2 ]
echo The total is $total
fi
另外,使用变量 ${!#} 可以得到最后一个命令行参数值。注意不能使用 ${$#} 这种格式,否则会得到错误的结果。
调用:./test9 ×
调用:./test9 10 ×
调用:./test9 10 15 20 ×
Uage: test11-9 a b
调用:./test9 10 15 √
The total is 25
获取所有数据变量
变量 $* 将命令行中提供的所有参数作为一个单词处理。这个单词中包含出现在命令行中的每一个参数值。
变量 $@ 将命令行中提供的所有参数作为同一个字符串中的多个单词处理。允许对其中的值进行迭代,分隔开所提供的不同参数。
示例
#!/bin/bash
# testing $* and $@
count=1
for param in "$*"
do
echo "\$* Parameter #$count= $param"
count=$[ $count + 1 ]
done
count=1
for param in "$@"
do
echo "\$@ Parameter #$count=$param"
count=$[ $count + 1 ]
done
调用:./test12 rich barbara katie jessica
$* Parameter #1=rich barbara katie jessica
$@ Parameter #1=rich
$@ Parameter #2=barbara
$@ Parameter #3=katie
$@ Parameter #4=jessica
命令行选项
选项是由破折号引导的单个字母,它更改命令的行为。
执行 shell 脚本时经常会遇到既需要使用选项又需要使用参数的情况。在 Linux 中的标准方式是使用特殊符号双破折号(--)将二者分开,这个特殊字符号码告诉脚本选项结束和普通参数开始的位置。
示例
#!/bin/bash
# extracting options and parameters
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift 1 ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac
shift
done
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
调用:./test16 -c -a -b test1 test2 test3
Found the -c option
Found the -a option
Found the -b option, with parameter value test1
test2 is not an option
test3 is not an option
调用:./test16 -c -a -b -- test1 test2 test3
Found the -c option
Found the -a option
Found the -b option, with parameter value --
test1 is not an option
test2 is not an option
test3 is not an option
使用 getopt 命令
getopt 命令可以接受任意形式的命令行选项和参数列表,并自动将这些选项和参数转换为适当的格式。
格式:
getopt options optstring parameters
optstring :选项字符串,定义命令行中的有效选项字母。在每个需要参数值的选项字母后面放置一个冒号,如:
getopt ab:cd -a -b test1 -cd -test2 -test3
当执行 getopt 命令时,会监测提供的参数列表,然后基于提供的选项字符串对列表进行解析。
-a -b test1 -c -d test2 test3
如果指定的选项不在选项字符串中,默认会生成一个错误消息。
getopt ab:cd -a -b test1 -cde test2 test3
getopt: invalid option -- e
-a -b test1 -c -d test2 test3
在脚本中使用 getopt
将原始脚本命令行参数送给 getopt 命令,然后将 getopt 命令的输出送给 set 命令:
set -- `getopt -q ab:cd "$@"`
set 命令的一个选项是双破折号,表示将命令行参数变量替换为 set 命令的命令行中的值。
示例
#!/bin/bash
# extracting command line options and values with getopt
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option" ;;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
调用:./test18 -ac
Found the -a option
Found the -c option
调用:./test18 -a -b test1 -cd test2 test3 test4
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2'
Parameter #2: 'test3'
Parameter #3: 'test4'
高级的 getopts 命令
getopts 命令顺序的对现有的 shell 参数变量进行处理。非常适宜用在循环中解析所有命令行参数。
getopts 命令的格式为:
getopts optstring variable
optstring 与 getopt 中的相似。如果要禁止输出错误消息,那么使选项字符串以冒号开头。
getopts 使用两个环境变量:
OPTARG:包含需要参数值的选项要使用的值
OPTIND:包含的值表示 getopts 停止处理时在参数列表中的位置
示例
#!/bin/bash
# processing options and parameters with getopts
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG" ;;
c) echo "Found the -c option" ;;
d) echo "Found the -d option" ;;
*) echo "Unknown option: $opt" ;;
esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done
调用:./test20 -a -b test1 -d test2 test3 test4
Found the -a option
Found the -b option, with value test1
Found the -d option
Parameter 1: test2
Parameter 2: test3
Parameter 3: test4
getopt 与 getopts 区别
getopt是个外部binary文件,而getopts是built-in。getopts 有两个参数,第一个参数是一个字符串,包括字符和“:”,每一个字符都是一个有效的选项,如果字符后面带有“:”,表示这个字符有自己的参数。 getopts从命令中获取这些参数,并且删去了“-”,并将其赋值在第二个参数中,如果带有自己参数,这个参数赋值在“OPTARG”中。提供getopts的shell内置了OPTARG这个变量,getopts修改了这个变量。而getopt则不能。因此getopt必须使用set来重新设定位置参数$1,$2....,然后在getopt中用shift的方式依次来获取。基本上,如果参数中可能含有空格,那么必须用getopts。否则仅仅从使用上看,他们没有区别。
键盘输入
read 命令接受标准输入(键盘)的输入,或其他文件描述符的输入。
示例
#!/bin/bash
# testing the read command
echo -n "Enter your name: "
read name
echo "Hello $name, welcome to my program."
调用:./test21
Enter your name: vian
Hello vian, welcome to my program.
在 read 命令包含 -p 选项,允许在 read 命令行中直接指定一个提示
示例
#!/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
Please enter your age: 18
That makes you over 6570 days old!
在 read 命令行中也可以不指定变量。如果不指定变量,那么 read 命令会将接收到的数据放置在环境变量 REPLY 中
示例
#!/bin/bash
# testing the REPLY 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"
调用:./test24
Enter a number:12
The factorial of 12 is 479001600
计时功能
使用 -t 选项指定 一个计时器。
示例
#!/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
Please enter your name: ivan
Hello ivan, welcome to my script
除了输入时间计时,还可以设置 read 命令计数输入的字符。
示例
#!/bin/bash
# getting just one character of input
read -n1 -p "Do you want to continue [Y/N]? " answer
case $answer in
Y | y) echo
echo "fine, continue on..." ;;
N | n) echo
echo "OK, goodbye"
exit;;
esac
echo "This is the end of the script"
调用:./test26
Do you want to continue [Y/N]? y
fine, continue on...
This is the end of the script
默读
-s 选项能够使输入数据不显示在屏幕上。
示例
#!/bin/bash
# hiding input data from the monitor
read -s -p "Enter your password: " pass
echo
echo "Is your password really $pass?"
调用:./test27
Enter your password:
Is your password really abc?
读取文件
读取文件的关键是如何将文件中的数据传送给 read 命令。
示例
#!/bin/bash
# reading data from a file
count=1
cat states | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done
echo "Finished processing the file"
调用:./test28
Line 1: Alabama
Line 2: Alaska
Line 3: Arizona
Line 4: Arkansas
Line 5: Colorado
Line 6: Connecticut
Line 7: Florida
Line 8: Georgia
Finished processing the file
Shell 语法之用户输入的更多相关文章
- shell编程中用户输入处理(shell 04)
shell编程中用户输入处理1.命令行参数2.脚本运行时获取输入 命令行参数 通过空格来进行分割的位置参数 :$+position $0,$1,$2 ....$0 :程序名$1,$2,$3 ... $ ...
- shell中处理用户输入
1.使用命令行参数 在shell执行的时候命令行中输入的所有参数可以赋值给一些特殊变量,这些变量成为位置变量参数. 包括: $0返回脚本名称.$1为第一个参数.$2为第二个参数 ...$9第九个参数 ...
- shell初级-----处理用户输入
命令行参数 读取参数 位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2,是第二个参数,直到第九个参数$9. 特殊的变量:$#表示参数个数,$?表示最后运行的命令的结束代码(返回值) 每个 ...
- Linux&shell之处理用户输入
写在前面:案例.常用.归类.解释说明.(By Jim) 命令行参数$1为第一个参数,$2为第二个参数,依次类推...示例: #!/bin/bash # using one command line p ...
- Linux - 简明Shell编程13 - 用户输入(UserInput)
脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 1 - arguments #!/bin/bash i ...
- Shell - 简明Shell入门13 - 用户输入(UserInput)
示例脚本及注释 1 - arguments #!/bin/bash if [ -n "$1" ];then # 验证参数是否传入 echo "The first para ...
- Shell 脚本处理用户输入
传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户的输入 bash shell提供了一些不同的方法来从用户处获取数据,包括命令行参数(添加在命令后数据),命令行选项(可以修改命令行为的单个 ...
- Linux shell脚本读取用户输入的参数
新建一个test.sh文件 #!/bin/sh echo "1 : For Test" echo "2 : For nohup &" whiletrue ...
- shell获取用户输入
主题: 再学shell之获取用户输入echo -n(不换行)和read命令-p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 和“读文件”深入学习 1.基本读取read命令接收标准输 ...
随机推荐
- (转)java源程序加密解决方案(基于Classloader解密)
转:http://cjnetwork.iteye.com/blog/851544 源程序加密解决方案 1. 概述: Java源程序的加密,有如下两种: 1使用混淆器对源码进行混淆,降低反编译工具的作用 ...
- exit与return的区别
===========================PHP的解释=========================================================== return ...
- quartz的job中注入spring对象!
一般情况下,quartz的job中使用autowired注解注入的对象为空,这时候我们就要使用spring-quartz提供的AdaptableJobFactory类. 自定义一个类: public ...
- SQL SERVER 备份数据库时候错误处理
当备份数据库时候出现如下错误时候 只需要删除备份目标就行了
- reboot与shutdown -r now 区别与联系(又收集了init和halt的小知识)
在linux命令中reboot是重新启动,shutdown -r now是立即停止然后重新启动,都说他们两个是一样的,其实是有一定的区别的. shutdown命令可以安全地关闭或重启Linux系统,它 ...
- import socket模块
编写两个小脚本实现聊天功能0.1: 脚本一,服务器端:server.py import socket # 调用模块 sk = socket.socket() # 创建socket addess = ( ...
- Java之RabbitMQ(一)与SpringBoot整合
应用场景:异步处理.应用解耦.流量削峰 参照:https://blog.csdn.net/weixin_38364973/article/details/82834348 开端 RabbitAutoC ...
- Django之跨表查询——正反向查询(ForeignKey)
1.正向查询和反向查询: 外键的查询操作: 正向查询: # 正向查询 # 基于对象,跨表查询 book_obj = models.Book.objects.all().first() ret = bo ...
- 配置vue项目的自定义config.js
[1]不采用脚手架的config文件夹中的配置文件 [2]在static文件夹下,自定义一个congfig.js文件 // 配置开发环境下服务器地址 window.Glod = { pmsApiUrl ...
- python语句结构(控制语句与pass语句)
python语句结构(控制语句和pass语句) break-跳出循环:语句可以跳出for和while语句的循环体.如果你从for和while循环中终止,任何对应循环的else语块均终止 continu ...