shell script 交互式脚本,能够读取命令行参数、选项,用户输入数据

1.命令行参数

1.1通常参数

1.1.1读取参数

位置参数是标准的数字:$0是程序名$1~$9是9个参数,若是脚本须要多于9个参数,须要在变量数字周围加花括号,好比${10}、${11}、、、、能够实现向脚本添加任意多要用的命令行参数。shell

#!/bin/bash
total1=$[ $1 * $2 ]
total2=$[ ${11} * ${12} ]
echo The tenth number is ${10}
echo The eleventh number is ${11}
echo The twelfth number is ${12}
echo The total1 is $total1
echo The total2 is $total2


执行./shell1.sh 1 2 3 4 5 6 7 8 9 10 11 12

The tenth number is 10
The tenth number is 11
The tenth number is 12
The total1 is 2
The total2 is 132
1.1.2读取程序名$0

$0参数获取shell在命令行启动的程序的名字。basename命令能够获取脚本名字bash

能够编写一些基于所用脚本名而执行不一样功能的脚本测试

#!/bin/bash
echo The command entered is:$0
执行结果:会带有路径,将第一个参数直接输出
caishu@lab403-1F:~$ /home/caishu/shell_script/shell1.sh
The command entered is: /home/caishu/shell_script/shell1.sh

caishu@lab403-1F:~/shell_script$ ./shell1.sh
The command entered is: ./shell1.sh

改善:basename命令会只返回脚本名字
#!/bin/bash
name=`basename $0`
echo The command entered is:$bansename

执行结果:输出脚本名字
caishu@lab403-1F:~$ /home/caishu/shell_script/shell1.sh
The command entered is: shell1.sh

caishu@lab403-1F:~/shell_script$ ./shell1.sh
The command entered is: shell1.sh
1.1.3测试参数 if [ -n $1 ]

在shell脚本中使用命令参数时,若执行脚本时,没有带参数,执行会报错spa

所以在写脚本时应先测试参数是否存在,能够用 if 语句测试,例如 if [ -n $1 ] 命令行

而后再对参数进行处理,若检测到没有参数,则给出提示。code

1.2.特殊参数变量  $#、$* 和$@

1.2.1参数计数$#

$#— —含有脚本运行时命令行参数的个数  注:${$#}不能反回最后一个参数值,应改成${!#}对象

#!/bin/bash
#catch the last parameter
parameter=$#
echo the last parameter is $parameter
echo the last parameter is ${!#}
echo the last parameter is ${$#}

执行结果:
当有参数时,parameter和${!#}返回值同样
caishu@lab403-1F:~/shell_script$ ./shell2.sh 1 2 3 4 5
the last parameter is 5
the last parameter is 5
the last parameter is 27804
当无参数时$parameter返回0,而${!#}返回$0参数所指——脚本名
caishu@lab403-1F:~/shell_script$ ./shell2.sh
the last parameter is 0
the last parameter is ./shell2.sh
the last parameter is 27803
1.2.2抓取全部数据$*和$@

$* 和$@提供了对全部命令行参数的访问递归

$* 将命令行上全部参数当成一个参数ip

$@以空格为分隔符,单独处理每个参数get

1.3移动变量shift

shift命令能够移动变量,将每个参数变量减一,因此变量$3的值会移到$2,变量$2的值会移到$1,变量$1的值会删除。(变量$0不会改变)

shift n  实现多位移动,shift 2移动2位

应用:在不知道命令行有多少参数时,能够只操做第一个参数,移动参数,而后继续操做第一个参数,知道检测到第一个参数长度为0,以实现对全部参数的操做。

注:shift使用要当心,当参数被移除后,被丢掉没法恢复。

2.处理选项

选项(options):跟在单破折线后的单个字母,能改变命令的行为

2.1查找选项

2.1.1处理简单选项

使用case语句检查每一个选项,是否是有效选项。

#!/bin/bash
#处理选项
while [ -n "$1" ]
do
  case "$1" in
  -a)command;;
  -b)command;;
  -c)command;;
  -d)command;;
   ...
  *)command;;
  esac
  shift
done
2.1.2分离参数和选项

选项和参数同时存在时,使用双破直线(--)将两者分开,shell会用(--)来代表选项结束

#!/bin/bash
#处理选项和参数
while [ -n "$1" ]
do
  case "$1" in
  -a)command;;
  -b)command;;
  -c)command;;
  -d)command;;
   ...
  *)command;;
  esac
  shift
done

for  parameter in $@
do
  command
done

执行时选项和参数要用--分开,如:./test -a -b -c -- parameter1 parameter2 parameter3
2.1.3处理带值的选项

有些选项会带有额外的参数值,当命令行选项带有额外参数时,脚本正确检测和处理.

以下:-b选项有额外参数,因为要处理的选项为$1,所以额外的参数就应该位于$2位置,所以设置shift命令移动2次

#!/bin/bash
#处理带参数的选项
while [ -n "$1" ]
do
  case "$1" in
  -a)command;;
  -b)parameter=$2 
     command
     shift;;
  -c)command;;
    ...
  --)shift
     break;;
  *)command;;
  esac
  shift
done

for  parameter in $@
do
  command
done

2.2使用getopt命令

2.2.1命令格式

getopt命令能够接受一系列任意形式的命令行选项和参数,并自动将它们转换成适当的格式。

getopt  optstring  options parameters

optstring 定义了选项有效字母,还定义了哪些选项须要参数(加:);若是指定了一个不在optstring中的选项,getopt命令会产生一条错误消息,若想忽略错误,则在getopt后加-q:

$ getopt ab:cd -a parameter1 -b parameter2 -cde parameter3
getopt 无效选项 --e
-a -b parameter2 -c -d -- parameter1  parameter3
$ getopt -q ab:cd -a parameter1 -b parameter2 -cde parameter3
-a -b 'parameter2' -c -d -- 'parameter1' 'parameter3'
2.2.2在脚本中使用getopt

set --命令的带双破折线选项,它会将命令行参数替换成set命令的命令行的值。在脚本中使用方法:

set -- `getopt -q ab:c "$@"`  #注意反引号


#!/bin/bash
#使用getopt
set -- `getopt -q ab:c "$@"` 

while [ -n "$1" ]
do
  case "$1" in
  -a)command;;
  -b)parameter=$2 
     command
     shift;;
  -c)command;;
    ...
  --)shift
     break;;
  *)command;;
  esac
  shift
done

for  parameter in $@
do
  command
done

 2.3使用高级的getopts命令

命令格式:getopts optstring variable                                                                                          optstring与以前相同,忽略错误信息不是-q,而是:optstring    getopts命令将当前参数保存在variable中。

getopts命令用到两个环境变量:OPTARG和OPTIND                                                    OPTARG保存了选项须要跟的参数值;OPTIND 保存了正在处理的参数位置

#!/bin/bash
#使用getopts

while getopts :ab:c opt
do
  case "$opt" in
  a)command;;
  b)  with value $OPTARG
     command
     shift;;
  c)command;;
    ...
  *)command;;
  esac
done

注:getopts命令解析命令行选项时,移除开头的单破折线-(脚本中a b c前面都没有-)

执行脚本时字母和选项能够放在一块儿不用加空格:./test -abparameter -c  (-c 前必须空格,不然没法识别-c选项,会被当成-b参数的一部分)

getopts命令知道什么时候中止处理选项,并将参数留给你处理。在getopts处理每一个选项时,OPTIND环境变量的值增一,处理完选项后,加一句shift $[ $OPTIND -1 ] 做为处理参数的开始

#!/bin/bash
#使用getopts

while getopts :ab:c opt
do
  case "$opt" in
  a)command;;
  b)  with value $OPTARG
     command
     shift;;
  c)command;;
    ...
  *)command;;
  esac
done

shift $[ $OPTIND -1 ]

for para in "$@"
do
  command
done

2.4将选项标准化

在建立shell脚本时,尽可能保持选项与Linux通用的选项含义相同,Linux通用选项有:

-a 显示全部对象    -c 生产一个计数    -d 指定一个目录    -e 扩展一个对象    -f指定读入数据的文件    -h显示命令的帮助信息    -i 忽略文本大小写   -l 产生输出得长格式文本    -n 使用非交互模式    -o 指定将全部输出重定向到输出文件    -q 以安静模式运行    -r 递归的处理目录和文件    -s 以安静模式运行    -v  生成详细输出   -x 排除某个对象   -y 对全部问题回答yes

3.得到用户输入read

3.1基本读入

read接受用户从键盘的输入:

#!/bin/bash
echo -n "Enter your name:"       #-n选项移调末尾换行符,不换行
read name
echo "Hello $name"

输出结果
Enter your name: caishu
Hello caishu

read的-p选项,直接置顶参数:

read -p "Enter your name:" name 
echo "Hello $name"

read命令会为每一个提示符分配变量,若提示符用完了,则将剩下的全部变量,分配给最后一个提示符。

若在read命中不指定变量,read命令会将它收到的任何数据都放进特殊环境变量REPLY中,

$read -p "Enter your para:" para1 para2 para3; echo "your parameter is $para1,$para2,$para3..."
Enter your para:1 2 3 4 5 6 7
your parameter is 1,2,34567...

$read -p "Enter your para:" ; echo "your parameter is $REPLY"
Enter your para:caishu
your parameter is caishu

3.2超时

-t:使用read命令时,会一直等用户输入,能够用-t选项来制定计时器,当计时器过时后,read命令会返回一个非零退出状态码。

read -t 5 -p "Please enter your name: " name   #会等5s,能够改变数字以改变等的时间

-n和数字:对输入的字符计数,当输入的字符达到预设的字符数时,它会自动退出,将输入的数据赋给变量。

read -n1 -p "Please enter your name: " name       #只接受一个字符,空格也是字符。

3.3隐藏方式读取

-s 阻止用户的输入显示在显示器上,(实际上,数据会被显示,只是read命令将文本颜色设置成跟背景颜色同样)

3.4从文件中读取

read line 会从文本中读取一行,用cat命令的输出经过管道传给含有read的while命令

#!/bin/bash
#read data from a file

count=1
cat filename | while read line
do 
  echo "Line $count: $line
  count=$[ $count + 1 ]
done
echo "Finished processing the file"
相关文章
相关标签/搜索