Linux命令行与shell脚本编程大全(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liouyi250/article/details/82956002

十一、处理用户输入

命令行参数

读取参数: $0是程序名,$1是第一个参数,$2是第二个参数,以此类推,直到第9个参数$9。当参数个数超过10以后,需要在变量数字周围加上花括号,如${10},如果输入到命令行的参数是字符串且含有空格,需要使用引号。

#! /bin/bash
echo "Your name is $1"
total=$[ ${10} * ${11} ]
echo "${10} * ${11} is $total"

root@kali:~/Desktop/Scripts# ./test14.sh 'Rich Blum' 2 3 4 5 6 7 8 9 10 11
Your name is Rich Blum
10 * 11 is 110

特殊参数变量

参数统计: $#含有脚本运行时携带的命令行参数的个数,可以在脚本任何地方使用这个特殊的变量,和普通变量一样。如果要获取最后一个参数,需要使用 ${!#}

抓取所有数据: $*变量会将命令行上所提供的所有惨所当作一个单词保存。 $@变量将命令行上提供的所有参数当作同一个字符串中的多个独立的单词,可以通过for遍历所有的参数值。

#! /bin/bash
#  $#统计参数个数
echo "there were $# parameters supplied"
#  ${!#}获取最后一个参数
param=$#
echo "the last parameter is ${!#}" ##当没有参数返回脚本名
echo "the second trial last parameter is ${param}" ##当没有参数时返回0
# $# 和 $@抓取所有数据
# test for '$*'
count=1
for param in "$*"
do
       echo "\$* param #$count=$param"	
	count=$[ $count + 1 ]
done

# test for '$@'
count=1
for param in "$@"
do
	echo "\$@ param #$count=$param"
	count=$[ $count + 1 ]
done

移动变量

使用shift命令时,默认情况下它会将每个参数变量向左移动一个位置,$3的值会移到$2中,$2会移到$1中,而$1的值则会被删除,无法再恢复。

#! /bin/bash
# shift命令移动变量
count=1
while [ -n "$1" ]
do
	echo "Parameter #$count = $1"
	count=$[ $count + 1 ]
	shift
done

处理选项

查找选项

1.对于简单选项,可以使用case语句来判断某个参数是否为选项。

## 简单选项
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 "$1 is not an option" ;;
	esac
	shift
done

root@kali:~/Desktop/Scripts# ./test14_4.sh -a -b -c -d
found the -a option
found the -b option
found the -c option
-d is not an option

2.分离参数和选项,使用特殊字符(--)表明选项列表结束。

## 分离参数和选项
while [ -n "$1" ]
do 
	case "$1" in
		-a) echo "fount the -a option" ;;
		-b) echo "fount the -b 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

root@kali:~/Desktop/Scripts# ./test14_4.sh -a -b -c -d -- test1 test2 test3
fount the -a option
fount the -b option
-c is not an option
-d is not an option
parameter #1 : test1
parameter #2 : test2
parameter #3 : test3

3. 处理带值的选项

## 处理带值的选项
while [ -n "$1" ]
do
	case "$1" in
		-a) echo "found the -a option";;
		-b) param=$2
		    echo "fount the -b option,with parameter value $param"
		    shift ;;
		-c) echo "fount the -c option" ;;
		--) shift
		    break;;
		 *) "$1 is not an option" ;;
	esac
       shift
done
count=1
for param in "$@"
do
	echo "parameter #$count : $param"
	count=$[ $count + 1 ]
done

root@kali:~/Desktop/Scripts# ./test14_4.sh -a -b test1 -c -d --  test2 test3 test4
found the -a option
fount the -b option,with parameter value test1
fount the -c option
./test14_4.sh: line 47: -d is not an option: command not found
parameter #1 : test2
parameter #2 : test3
parameter #3 : test4

使用getopt命令

## 使用getopt命令,可以将选项和起来使用,当参数带空格时识别不了。
# 命令格式 getopt optstring parameters
# getopt ab:cd -a -b test1 -cd test2 test3,定义了4个有效字母a,b,c,d,冒号(:)被放在b之后,因为b需要一个参数值
set -- $(getopt -q ab:cd "$@")  ##-q选项去掉错误消息
while [ -n "$1" ]
do 
	case "$1" in
		-a) echo "Found the -a option" ;;
		-b) param=$2
		    echo "fount the -b option,with paramter value $param"
		    shift ;;
	        -c) echo "fount 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

root@kali:~/Desktop/Scripts# ./test14_4.sh -ab test1 -cd test2 test3
Found the -a option
fount the -b option,with paramter value 'test1'
fount the -c option
-d is not an option
parameter #1 : 'test2'
parameter #2 : 'test3'

使用getops命令

## 使用getopts命令
# 命令格式同getopt,如果要去掉错误消息,需要在optstring前加一个冒号
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
shift $[ $OPTIND -1 ]
count=1
for param in "$@"
do
	echo "Parameter $count : $param"
	count=$[ $count + 1 ]
done

root@kali:~/Desktop/Scripts# ./test14_4.sh -ab test1 -cd test2 test3
found the -a option
found the -b option,with value test1
found the -c option
unknown option :?
Parameter 1 : test2
Parameter 2 : test3

获得用户输入

read命令从标准输入或者另一个文件描述符中接受输入。

#! /bin/bash
## read 命令
echo -n "please enter your name: "
read name
echo "Hello $name,welcome to my program"
## -p 选项允许直接在read命令行指定提示符
read -p "Please enter your age:" age
day=$[ $age * 365 ]
echo "that makes you over $day days old"
## 如果在read命令行不指定变量,read命令把它收到的任何数据都会放在REPLY中
read -p "Enter your name: "
echo "Hello $REPLY,welcome to my program"
## -t选项设置超时
if read -t 5 -p "Please enter you name:" name
then
	echo "Hello $name,welcome to my script"
else
	echo 
	echo "sorry,too slow"
fi
## -n选项设置读取的字符个数
read -n1 -p "Do you want to continue [Y/N]:"  ##-n和1一起使用,表示read在接受单个字符后退出
## -s选项隐藏方式读取,通常用于输入密码
read -s -p "Enter your password:" pass
echo "Is your password really $pass"
## 从文件中读取,通常使用cat命令将结果通过管道直接传给含有read命令的while命令
count=1
cat test14_2.sh | while read line
do 
	echo "Line $count: $line"
	count=$[ $count + 1 ]
done

猜你喜欢

转载自blog.csdn.net/liouyi250/article/details/82956002