一、命令行参数
向shell脚本传递数据的最基本方式是使用命令行参数
(1)读取参数($1 $2 .....)
bash shell将在命令行中输入的所有参数赋值给一些特殊变量,这些变量称为位置参数。
[root@ceph01 test1]# cat shell-arg.sh
#!/bin/bash
#using two comand line parameters
total=$[ $1 * $2 ]
echo The first oaramerer is $1
echo The second parameter is $2
echo The total value is $total
运行脚本:
[root@ceph01 test1]# ./shell-arg.sh 1 2
The first oaramerer is 1
The second parameter is 2
The total value is 2
(2)读取程序名称($0)
使用参数$0可以确定shell从命令启动的程序的名称。
[root@ceph01 test1]# cat shell-program-name.sh
#!/bin/bash
# using basename with the $0 parameter
name=`basename $0`
echo The command entered is: $name
[root@ceph01 test1]# ./shell-program-name.sh
The command entered is: shell-program-name.sh
(3)测试参数
[root@ceph01 test1]# cat shell-test-arg.sh
#!/bin/bash
# testing parameters before use
if [ -n "$1" ]
then
echo Hello $1,glad to meet you!
else
echo "Sorry,you didn't identify yourself!"
fi
运行脚本:
[root@ceph01 test1]# ./shell-test-arg.sh
Sorry,you didn't identify yourself!
[root@ceph01 test1]# ./shell-test-arg.sh Rich
Hello Rich,glad to meet you!
二、特殊的参数变量
(1)参数计数($#)
例1:
[root@ceph01 test1]# cat shell-test-arg-num.sh
#!/bin/bash
# getting the number of parameters
echo There were $# parameters supplied
[root@ceph01 test1]# ./shell-test-arg-num.sh 1 2 3 4
There were 4 parameters supplied
例2:
[root@ceph01 test1]# cat shell-test-arg-num1.sh
#!/bin/bash
# testing parameters
if [ $# -ne 2 ]
then
echo Usage: test9 a b
else
total=$[ $1 + $2 ]
echo The total is $total
fi
[root@ceph01 test1]# ./shell-test-arg-num1.sh
Usage: test9 a b
[root@ceph01 test1]# ./shell-test-arg-num1.sh 1 2
The total is 3
(2)获取所有数据($* 和 $@)
变量$*将命令行中提供的所有参数作为一个单词处理;
变量$@将命令ha同一行中提供的所有参数作为同一个字符串中的多个单词处理
[root@ceph01 test1]# cat shell-data.sh
#!/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
运行脚本:
[root@ceph01 test1]# ./shell-data.sh rich kate helloy
$* Parameter #1 = rich kate helloy
$@ Parameter #1 = rich
$@ Parameter #2 = kate
$@ Parameter #3 = helloy
三、移位
shift命令时,默认将每个参数变量左移一个位置。
[root@ceph01 test1]# cat shift.sh
#!/bin/bash
# demonstarting the shift command
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
运行脚本:
[root@ceph01 test1]# ./shift.sh rich katie band
Parameter #1 = rich
Parameter #2 = katie
Parameter #3 = band
四、处理选项
选项是由破折号引导的单个字母,它更改命令的行为。
(1)找出选项
处理简单选项
[root@ceph01 test1]# cat shell-options.sh
#!/bin/bash
# extracting command line options as parameters
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@ceph01 test1]# ./shell-options.sh -a -b -c -d
Found the -a option
Found the -b option
Found the -c option
-d is not an option
从参数中分离选项
[root@ceph01 test1]# cat shell-options1.sh
#!/bin/bash
# extracting options and parameters
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
运行脚本:
[root@ceph01 test1]# ./shell-options1.sh -c -a -b test1 test2 test3
Found the -c option
Found the -a option
Found the -b option
test1 is not an option
test2 is not an option
test3 is not an option
[root@ceph01 test1]# ./shell-options1.sh -c -a -b -- test1 test2 test3
Found the -c option
Found the -a option
Found the -b option
Parameter #1: test1
Parameter #2: test2
Parameter #3: test3
处理带值的选项
[root@ceph01 test1]# cat shell-options2.sh
#!/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 2;;
-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
[root@ceph01 test1]# ./shell-options2.sh -a -b test1 -d
Found the -a option
Found the -b option,with parameter value test1
(2)使用getopt命令
1》命令格式
getopt命令可以接受任意形式的命令选项和参数列表,并自动将这些选项和参数转换为适当的格式,命令格式
getopt options optstring parameters
[root@ceph01 test1]# getopt ab:cd -a -b test1 -cd test2 test3
-a -b test1 -c -d -- test2 test3
2》在脚本中使用getopt
[root@ceph01 test1]# cat getopt.sh
#!/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
运行脚本:
[root@ceph01 test1]# ./getopt.sh -ac
Found the -a option
Found the -c option
[root@ceph01 test1]# ./getopt.sh -a -b test1 -cd test2 test3 test4Found the -a option
Found the -b option,with parameter value 'test1' shift
'test1' is not an option
Found the -c option
Parameter #1: 'test2'
Parameter #2: 'test3'
Parameter #3: 'test4'
3》更高级的getopts命令
getopts命令格式为:
getopts optstring variable
[root@ceph01 test1]# cat getopts.sh
#!/bin/bash
#simple demonstration of the getopts command
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
运行脚本:
[root@ceph01 test1]# ./getopts.sh -ab test1 -c
Found the -a option
Found the -b option, with value test1
Found the -c option
五、标准化选项
六、获取用户输入
(1)基本读取
read命令接受标准输入(键盘)的输入,或其他文件描述符的输入。
[root@ceph01 test1]# cat read.sh
#!/bin/bash
# testing the read command
echo -n "Enter your name: "
read name
echo "Hello $name, welcome to my program!"
运行脚本:
[root@ceph01 test1]# ./read.sh
Enter your name: haha
Hello haha, welcome to my program!
(2)计时
-t选项指定一个计时器,指定read命令等待输入的秒数。
[root@ceph01 test1]# cat read-t.sh
#!/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
运行脚本:
[root@ceph01 test1]# ./read-t.sh
Please enter your name: haha
Hello haha, welcome to my script
[root@ceph01 test1]# ./read-t.sh
Please enter your name:
Sorry,too slow!
(3)默读
-s选项能够使read命令中输入的数据不显示在监视器上
[root@ceph01 test1]# cat read-s.sh
#!/bin/bash
# hiding input data from the monitor
read -s -p "Enter your passwd: " pass
echo
echo "Is your passwd really $pass"
运行脚本:
[root@ceph01 test1]# ./read-s.sh
Enter your passwd:
Is your passwd really hshs
(4)读取文件
[root@ceph01 test1]# cat read-file.sh
#!/bin/bash
# reading data from a file
count=1
cat test | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done
echo "Finished processing the file"
运行脚本
[root@ceph01 test1]# ./read-file.sh
Line 1: root:x:0:0:root:/root:/bin/bash
Line 2: bin:x:1:1:bin:/bin:/sbin/nologin
Line 3: daemon:x:2:2:daemon:/sbin:/sbin/nologin
Line 4: adm:x:3:4:adm:/var/adm:/sbin/nologin
Line 5: lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Line 6: sync:x:5:0:sync:/sbin:/bin/sync
Line 7: shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Finished processing the file