摘要:命令行与脚本编程大全第三版第十四章学习总结第十四章处理用户输入本章内容传递参数跟踪参数移动变量处理选项将选项标准化获取用户输入命令行参数读取参数会将一些称为位置参数的特殊变量分配给输入到命令行中的所有参数。
时间:2017年09月19日星期二
说明:本文部分内容均摘取自书籍《Linux命令行与shell脚本编程大全》,版权归原作者所有。《Linux命令行与shell脚本编程大全》(第三版)第十四章学习总结
本章内容
传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获取用户输入14.1 命令行参数 14.1.1 读取参数
bash shell会将一些称为位置参数的特殊变量分配给输入到命令行中的所有参数。
位置参数变量是标准的数字
$0是程序名 $1是第一个参数 $2是第二个参数 依次类推,知道第九个参数$9
编写test1.sh脚本
#!/bin/bash factorial=1 for (( number=1;number<=$1;number++ )) do factorial=$[ $factorial * $number ] done echo The factorial of $1 is $factorial
执行命令
./test1.sh 5
编写test2.sh脚本
#!/bin/bash total=$[ $1 * $2 ] echo The first paramter is $1. echo The second paramter is $2. echo Thetotal paramter is $total.
执行命令
./test2.sh 2 5
编写test3.sh脚本
#!/bin/bash echo Hello $1,glad to meet you.
执行命令
./test3.sh "Rich Blum"
如果脚本需要的命令行参超过9个时,使用${10}形式获取
编写test4.sh脚本
#!/bin/bash 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 1214.1.2 读取脚本名
使用$0参数获取shell在命令行启动的脚本名
编写test5.sh脚本
#!/bin/bash echo The zero parameter is set to:$0
执行命令
bash test5.sh
使用basename命令获取不包含路径的脚本名
编写test5b.sh
#!/bin/bash name=$(basename $0) echo echo The script name is:$name
使用这种方法来编写基于脚本名执行不同功能的脚本
编写test6.sh脚本
#!/bin/bash name=$(basename $0) if [ $name = "addem" ] then total=$[ $1 + $2 ] elif [ $name = "multem" ] then total=$[ $1 * $2 ] fi echo echo The calculated value is $total
执行命令
cp test6.sh addem cp test6.sh multem ./addem 2 5 ./multem 2 514.1.3 测试参数
脚本在使用参数前,需要检查其中是否存在数据
编写test7.sh脚本
#!/bin/bash if [ -n "$1" ] then echo Hello $1,glad to meet you. else echo "Sorry,you did not identify yourself." fi
执行命令
./test7.sh Rich ./test7.sh14.2 特殊参数变量 14.2.1 参数统计
特殊变量$#含有脚本运行时携带的命令行参数的个数
编写test8.sh脚本
#!/bin/bash echo There were $# parameters supplied.
执行命令
./test8.sh ./test8.sh 1 2 3 4 5 ./test8.sh "Rich Blum"
编写test9.sh脚本
#!/bin/bash if [ $# -ne 2 ] then echo echo Usage:test9.sh a b echo else total=$[ $1 + $2 ] echo echo The total is $total echo fi
执行命令
./test9.sh ./test9.sh 10 ./test9.sh 10 15
获取最后一个命令行参数变量
编写test10.sh脚本
#!/bin/bash params=$# echo echo The total parameter is $params echo The last parameter is ${!#} echo
执行命令
./test10.sh ./test10.sh 1 2 3 ./test10.sh 1 2 3 414.2.2 抓取所有的数据
使用$*和$@变量来访问所有的参数 使用for命令遍历这两个变量时 $*变量会将所有参数当成单个参数 $@变量会多带带处理每个参数
编写test11.sh脚本
#!/bin/bash echo echo "Using the $* method:$*" echo "Using the $@ method:$@" echo
执行命令
./test11.sh rich barbara katie jseeica
编写test12.sh脚本
#!/bin/bash echo count=1 for param in "$*" do echo "$* parameter #Scount = $param" count=$[ $count + 1 ] done echo count=1 for param in "$@" do echo "$@ parameter #Scount = $param" count=$[ $count + 1 ] done
执行命令
./test12.sh rich barbara katie jseeica14.3 移动变量
shift命令会根据所在的相对位置来移动命令行参数
敲门:使用shift命令的时候要小心。如果某个参数被移除,它的值就被丢弃了,无法再恢复
编写test13.sh脚本
#!/bin/bash echo count=1 while [ -n "$1" ] do echo "Parameter #$count = $1" count=$[ $count+1 ] shift done
执行命令
./test13.sh rich barbara katie jessica
编写test14.sh脚本
#!/bin/bash echo echo "The original parameters: $*" shift 2 echo "Here"s the new first parameter: $1"
执行命令
./test14.sh 1 2 3 4 514.4 处理选项 14.4.1 查找选项
1.处理简单选项
在提取每个多带带参数时,用case语句来判断某个参数是否为选项
编写test15.sh脚本
#!/bin/bash echo while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option" ;; -b) echo "Found the -b option" ;; -c) echo "Found the -c option" ;; *) echo "Found the an option" ;; esac shift done
执行命令
./test15.sh -a -b -c -d
2.分离参数和选项
Linux通过双破折线(--)来区分选项和参数
编写test16.sh脚本
#!/bin/bash echo while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option" ;; -b) echo "Found the -b option" ;; -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.sh -a -b -c test1 test2 test3 ./test16.sh -a -b -c -- test1 test2 test3
3.处理带值的选项
有些选项会带上一个额外的参数值,脚本必须能够检测到并正确处理
编写test17.sh脚本
#!/bin/bash echo 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
执行命令
./test17.sh -a -b test1 -d ./test17.sh -b test1 -a -d14.4.2 使用getopt命令
getopt命令是一个在处理命令选项和参数时非常方便的工具。它能够识别命令行参数,从而在脚本中解析它们时更方便。
1.命令的格式
命令格式:getopt optstring parameters
命令说明:getopt命令可以接受一系列任意形式的命令行选项和参数
敲门:getopt命令有一个更高级的版本叫做getopts(注意这是复数形式)。getopts命令会在本章随后部分降到。因为这两个命令的拼写几乎一摸一样,所以很容易混淆,一定要小心。
命令演示:getopt ab:cd -a -b test1 -cd test2 test3
演示结果:-a -b test1 -c -d – test2 test3
演示说明:optstring定义了四个有效选项字母:a,b,c和d。冒号(:)被放在了字母b后面,因为b选项需要一个参数值。当getopt命令运行时,它会检查提供的参数列表(-a -b test1 -c -d – test2 test3),并基于提供的optstring进行解析。注意,它会自动将-cd选项分成两个多带带的选项,并插入双破折线来分割行中的额外参数。
getopt说明
如果指定了一个不在optstring中的选项,getopt命令会产生一条错误的消息 如果想忽略这条错误消息,可以在命令后加-q选项 getopt命令选项必须出现在optstring之前 getopt命令并不擅长处理带空格和引号的参数值
2.在脚本中使用getopt
编写test18.sh脚本
#!/bin/bash # set -- $(getopt -q ab:cd "$@") # echo 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" ;; -d) echo "Found the -d 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.sh -ac ./test18.sh -a -b test1 -cd test2 test3 test414.4.3 使用更高级的getopts
getopts命令(注意是复数)内建于bash shell。
命令格式:getopts optstring variable
命令说明: optstring与 getopt命令中的optstring相同
使用OPTARG环境变量获取选项的参数值
编写test19.sh脚本
#!/bin/bash echo while getopts :ab:c 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" ;; *) echo "Unknown option: $opt" ;; esac done
执行命令
./test19.sh -ab test1 -c ./test19.sh -b "test1 test2" -a ./test19.sh -abtest1 ./test19.sh -acde
使用shift命令和OPTIND值来移动参数
编写test20.sh脚本
#!/bin/bash echo 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 ] # echo count=1 for param in "$@" do echo "Parameter $count: $param" count=$[ $count+1 ] done
执行命令
./test20.sh -a -b test1 -d test2 test3 test414.5 将选项标准化
在创建shell脚本时,可以决定使用哪些字母以及它们的用法
但有些字母选项在linux世界里已经拥有了某种程度的标准含义
常用的Linux命令选项
选项:描述 -a:显示所有对象 -c:生成一个计数 -d:指定一个目录 -e:扩展一个对象 -f:指定读入数据的文件 -h:显示命令的帮助信息 -i:忽略文本大小写 -l:产生输出的长格式版本 -n:使用非交互模式(批处理) -o:将所有输出重定向到指定的输出文件 -q:以安静模式运行 -r:递归地处理目录和文件 -s:以安静模式运行 -v:生成详细输出 -x:排除某个对象 -y:对所有问题回答yes14.6 获得用户输入 14.6.1 基本的读取
read命令从标准输入(键盘)或另一个文件描述符中接受输入。
编写test21.sh脚本
#!/bin/bash echo -n "Enter your name:" read name echo "Hello $name,welcome to my program."
编写test22.sh脚本
#!/bin/bash read -p "Please enter your age:" age days=$[ $age * 365] echo "That makes you over $days days old!"
编写test23.sh脚本
#!/bin/bash read -p "Enter your name:" first last echo "Checking data for $last, $first..."
read命令会将它收到的任何数据都放进特殊环境变量REPLY中
编写test24.sh脚本
#!/bin/bash read -p "Enter your name:" echo echo Hello $REPLY, welcome to my program.14.6.2 超时
使用read命令时,可以使用-t选项指定read命令等待输入的秒数。当计数器过期后,read命令返回一个非零退出状态码
编写test25.sh
#!/bin/bash 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
使用read命令-n选项统计输入的字符数。当输入的字符达到预设的字符数时,就自动退出,将输入的数据赋给变量
编写test26.sh脚本
#!/bin/bash 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,goodbay" exit;; esac echo "The is the end of the script"14.6.3 隐藏方式读取
使用-s选项,隐藏read命令中输出的数据,避免直接出现在显示器上
编写test27.sh命令
#!/bin/bash read -s -p "Enter your password:" pass echo echo "Is your password really $pass?"14.6.4 从文件中读取
使用read读取Linux系统上文件里保存的数据。每次调用read命令时,它都会从文件中读取一行数据。当文件中没有内容时,read命令退出并返回非零退出状态码
编写test文本
One Two Three
编写test28.sh脚本
#!/bin/bash count=1 cat test | while read line do echo "line $count: $line" count=$[ $count+1 ] done echo "Finished processing the file"14.7 小结
本章讲述了3种不同的方法来从脚本用户处理获得数据。命令行参数允许用户运行脚本时直接从命令行输入数据。脚本通过位置参数来取回命令行参数并将它们赋给变量
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/33195.html
摘要:命令行与脚本编程大全第三版第十三章学习总结第十三章更多的结构化命令本章内容循环语句迭代语句使用语句循环重定向循环的输出命令命令格式命令说明变量为中的迭代值,在和语句之间输入的命令可以是一条或多条标准的命令。 时间:2017年09月13日星期三说明:本文部分内容均摘取自书籍《Linux命令行与shell脚本编程大全》,版权归原作者所有。《Linux命令行与shell脚本编程大全》(第三版...
摘要:命令允许你在脚本结束时指定一个退出状态码编写脚本编写脚本编写脚本当指定的退出状态码超过时,最终的结果是指定的数值除以后得到的余数。 时间:2017年08月28日星期一说明:本文部分内容均摘取自书籍《Linux命令行与shell脚本编程大全》,版权归原作者所有。《Linux命令行与shell脚本编程大全》(第三版)第十一章学习总结 第十一章:构建基本脚本 本章内容 使用多个命令 创建脚本...
摘要:探查进程当程序运行在系统上时,我们称之为进程。第四行说的是系统的物理内存总共有多少内存,当前用了多少,还有多少空闲。第五行显示的也是系统的物理内存,不过是针对系统交换空间如果分配了的话的状态而言的。 时间:2017年04月07日 说明:本文部分内容均摘取自书籍《Linux命令行与shell脚本编程大全》,版权归原作者所有。《Linux命令行与shell脚本编程大全》(第三版)第四章学习...
摘要:例如,函数名会在变量中定义,函数命令行上的任何参数都会通过等定义。在开源世界中,共享代码才是关键,而这一点同样适用于脚本函数。这里将说明如何下载安装使用脚本函数库。 时间:2017年10月09日星期一说明:本文部分内容均摘取自书籍《Linux命令行与shell脚本编程大全》,版权归原作者所有。《Linux命令行与shell脚本编程大全》(第三版)第十七章学习总结 第十七章:创建函数 本...
摘要:通过读取格式的数据文件,输出语句来将数据插入数据库编写文本编写脚本执行命令小结允许在脚本中创建自己的文件描述符。 时间:2017年09月25日星期一说明:本文部分内容均摘取自书籍《Linux命令行与shell脚本编程大全》,版权归原作者所有。《Linux命令行与shell脚本编程大全》(第三版)第十五章学习总结 第十五章:呈现数据 本章内容 再探重定向 标准输入和输出 报告错误 丢弃错...
阅读 1792·2021-09-29 09:35
阅读 1677·2019-08-30 14:15
阅读 2825·2019-08-30 10:56
阅读 785·2019-08-29 16:59
阅读 399·2019-08-29 14:04
阅读 1157·2019-08-29 12:30
阅读 895·2019-08-28 18:19
阅读 359·2019-08-26 11:51