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命令接收标准输 ...
随机推荐
- 尚学python课程---13、python基础语法
尚学python课程---13.python基础语法 一.总结 一句话总结: legend2系统使我能够快速掌握一门语法,特别有用 pass 语句:空语句:是为了保持程序结构的完整性 :作用:比如: ...
- json的dump和dumps的区别
dumps是将dict转化成str格式,loads是将str转化成dict格式. dump和load也是类似的功能,只是与文件操作结合起来了. In [1]: import json In [2]: ...
- MySQL数据库CRUD命令用法
数据库CRUD操作即添加(Create).读取(Read).更新(Update)和删除(Delete). 1. 添加操作也称插入操作,使用Insert语句,Insert语句可以用于几种情况: 插入完整 ...
- 2.vue插件总结——总有你能用上的插件
UI组件 框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI - ...
- kafka数据分区的四种策略
kafka的数据的分区 探究的是kafka的数据生产出来之后究竟落到了哪一个分区里面去了 第一种分区策略:给定了分区号,直接将数据发送到指定的分区里面去 第二种分区策略:没有给定分区号,给定数据的ke ...
- 面向对象_访问修饰符_构造与析构函数_this指针
1:面向对象 以codeblocks举例,在一个工程里面: File-->new -->Class可以建一个类,可以设置类的参数,是否有set get方法,有无构造函数等设置,.h文件主要 ...
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- 使用jqselectable构建美化的select元素
本文只对此插件的应用做一些探讨,本插件版权属于原作者,插件原始下载地址:http://www.jq22.com/jquery-info288 原插件也有些许不足之处,比如样式定义名称太过普通,容易和页 ...
- 服务器迁移部署OmsWeb
绑定 基本设置 高级设置
- 报错C1189 #error: "No Target Architecture"
根本原因: 是因为单独包含了一些windows.h已经包含了的头文件如"fileapi.h","WinUser.h",但是却没有包含windows.h 或者先包 ...