shell处理用户输入

命令行参数

读取参数

bash shell会将一些称为位置参数的特殊变量分配给输入到命令行中的所有参数。这也包括shell所执行的脚本名称。位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2是第二个参数,依次类推,直到第九个参数$9

#!/bin/bash
echo "The zero parameter is $0"
echo "The first parameter is $1"
echo "The second parameter is $2"  
echo "The third parameter is $3"  

执行脚本

$ bash scipth.sh 1 2 3
The zero parameter is scipth.sh
The first parameter is 1
The second parameter is 2
The third parameter is 3
$ bash scipth.sh "1 2 3" "11 22 33" '111 222 333'
The zero parameter is scipth.sh
The first parameter is 1 2 3
The second parameter is 11 22 33
The third parameter is 111 222 333

如果脚本需要的命令行参数不止9个,你仍然可以处理,但是需要稍微修改一下变量名。在
第9个变量之后,你必须在变量数字周围加上花括号,比如${10}。

#!/bin/bash
echo "The 10 parameter is ${10}"
echo "The 11 parameter is ${11}"

执行脚本

$ bash scipth.sh  1 2 3 4 5 6 7 8 9 10 11
The 10 parameter is 10
The 11 parameter is 11

读取脚本名

$0参数获取shell在命令行启动的脚本名,basename命令会返回不包含路径的脚本名。

#!/bin/bash
echo "The 0 parameter is ${0}"
name=$(basename $0)
echo "the shell name is " $name

执行脚本

$ bash /home/ocean/workspace/scipth.sh 
The 0 parameter is /home/ocean/workspace/scipth.sh
the shell name is  scipth.sh

确定参数是否存在

在使用参数之前一定要检查参数是否存在

#!/bin/bash
if [ -n "$1" ]
then 
	echo "Hello $1"
else
	echo "please input ont paremeter"
fi

特殊参数变量

参数统计: $#

#!/bin/bash
if [ $# -ne 2 ]
then 
	echo
	echo Usage: test9.sh a b 
	echo
else
	total=$[$1 + $2 ]
	echo $total
fi
  • 找出最后一个参数
#!/bin/bash
params=$#
echo
echo "The last parameter is $params"
echo "The last parameter is ${!#}"

执行脚本

$ bash scipth.sh 

The last parameter is 0
The last parameter is scipth.sh
$ bash scipth.sh 1

The last parameter is 1
The last parameter is 1

抓取所有参数: $ *和$@

$ bash scipth.sh 1 2 3 4 5 
Using the $* method: 1 2 3 4 5
Using the $@ method: 1 2 3 4 5

$* param #1 = 1 2 3 4 5

$@ param #1 = 1
$@ param #2 = 2
$@ param #3 = 3
$@ param #4 = 4
$@ param #5 = 5

移动变量:shift

在使用shift命令时,默认情况下它会将每个参数变量向左移动一个位置。所以,变量$3
的值会移到$2中,变量$2的值会移到$1中,而变量$1的值则会被删除(注意,变量$0的值,也就是程序名,不会改变)。

#!/bin/bash
echo
count=1
while [ -n "$1" ]
do
	echo "Parameter #$count=$1"
	count=$[ $count + 1 ]
	shift
done

备注:shift n 指定跳过n个参数。

处理选项

查找选项

处理简单选项

#!/bin/bash
echo
count=1
while [ -n "$1" ]
do
	case "$1" in
		-a) echo "-a option";;
		-b) echo "-b option";;
		-c) echo "-c option";;
		*) echo "no $1 option"
	esac
	shift
done

分离参数和选项

shell会用双破折线来表明选项列表结束。

#!/bin/bash
echo
count=1
while [ -n "$1" ]
do
	case "$1" in
		-a) echo "-a option";;
		-b) echo "-b option";;
		-c) echo "-c option";;
		--) shift
		break;;
		*) echo "$1 is not a option";;
	esac
	shift
done

count=1
for param in $@
do
	echo "Parameter #$count:$param"
	count=$[ $count+1 ]
done

处理带值的选项

#!/bin/bash
count=1
while [ -n "$1" ]
do
	case "$1" in
		-a) echo "-a option";;
		-b) echo "-b option with value $2"
		    shift;;
		-c) echo "-c option";;
		--) shift
		break;;
		*) echo "$1 is not a option";;
	esac
	shift
done

执行脚本

$ bash scipth.sh  -a -a -a -a -b 222 -a
-a option
-a option
-a option
-a option
-b option with value 222
-a option

getopt命令

  • 如何使用

getopt将参数格式化成固定的格式,其使用方法是

getopt optstring parameters

例子:

$ 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 -cd test2 test3),并基于提供的optstring进行解析。注意,它会自动将-cd选项分成两个单独的选项,并插入双破折线来分隔行中的额外参数。

用户输入

read

#!/bin/bash
echo -n "Enter your name:" #echo -n不会在字符串末尾输出换行符
read name
echo "Hello $name"
  • read 指定变量
#!/bin/bash
read -p "Enter your name:" name
echo "Hello $name"
#!/bin/bash
read -p "Enter your name:" first last
echo "Hello $first $last"
  • read不指定变量时,会存储到特殊环境变量REPLY中
#!/bin/bash
read -p "Enter your name:" 
echo "Hello $REPLY"
  • read -t 等待时间。如果计时器过期,会返回一个非0退出码
in/bash
if read -t 5 -p "Please enter your name: " name
then
	echo "hello $name"
else
	echo "too late"
fi
  • read -n 输入字符个数。当输入的字符达到预设的字符数时,就自动退出,将输入的数据赋给变量
#!/bin/bash
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y) echo
	echo "contine on ..";;
N | n) echo
	echo "byebye!"
	exit;;
esac
echo "End"
  • read -s输入密码(实际上,数据会被显示,只是read命令会将文本颜色设成跟背景色一样)。
#!/bin/bash
read -s -p "Enter your password: " pas
echo "password:$pas"
  • 通过管道 从文件中读取

每次调用read命令,它都
会从文件中读取一行文本。当文件中再没有内容时,read命令会退出并返回非零退出状态码。

#!/bin/bash
count=1
cat out.txt | while read line
do
	echo "Line $count:$line"
	count=$[ $count + 1]
done

<Linux命令行与shell脚本编程大全.第3版> 读书笔记

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值