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命令时,默认情况下它会将每个参数变量向左移动一个位置。所以,变量$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 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选项分成两个单独的选项,并插入双破折线来分隔行中的额外参数。
#!/bin/bash
echo -n "Enter your name:" #echo -n不会在字符串末尾输出换行符
read name
echo "Hello $name"
#!/bin/bash
read -p "Enter your name:" name
echo "Hello $name"
#!/bin/bash
read -p "Enter your name:" first last
echo "Hello $first $last"
#!/bin/bash
read -p "Enter your name:"
echo "Hello $REPLY"
in/bash
if read -t 5 -p "Please enter your name: " name
then
echo "hello $name"
else
echo "too late"
fi
#!/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"
#!/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