Linux shel编程之命令行参数处理

本文介绍了如何在Shell脚本中使用命令行参数,包括位置参数的获取、特殊参数变量的作用及命令行参数的移位等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在运行脚本程序中,用户可以通过命令行参数将参数传递给脚本程序:

# ./test 10 a
通过一些特殊的变量:位置参数,可以在脚本中取得命令行参数。其中,$0为程序名称,$1为第一个参数,$2为第二个参数,依此类推 $9为第九个参数。
# cat test.sh 
#!/bin/bash
echo "shell name is $0"
echo "first args is $1"
echo "second args is $2"
# ./test.sh 
shell name is ./test.sh
first args is 
second args is 
# ./test.sh  1 2 
shell name is ./test.sh
first args is 1
second args is 2
在第一次运行./test.sh时,没有传入参数,可以看出$1,$2值为空。
每个参数都是通过空格分隔的,如果输入的参数中包含空格,要用引号(" "或' ‘)包起来。
# ./test.sh  "hello world!" 2
shell name is ./test.sh
first args is hello world!
second args is 2
如果命令行参数多于9个,如果想取得后面的参数,使用${10},${11}..,$(n)取得。
虽然我们可以通过$0确定程序的名称,但是$0中包括程序完整的路径。
# cat test.sh
#!/bin/bash
echo "shell name is $0"
# ./test.sh
shell name is ./test.sh
# /home/jie/code/shellcode/test.sh 
shell name is /home/jie/code/shellcode/test.sh
有时候我们仅仅想获得程序的名称(不包括具体路径),可以使用basename命令取得程序名称(name=`basename $0`)。
# cat test.sh
#!/bin/bash
name=`basename $0`
echo "shell name is $name"
# ./test.sh
shell name is test.sh
# /home/jie/code/shellcode/test.sh 
shell name is test.sh
在使用shell脚本中的命令行参数之前,必须对参数进行检查,以保证命令行参数中确实包含我们想要的数据。
$ cat test.sh
#!/bin/bash
if [ -n "$1" ]
then
   echo "hello $1!"
else
   echo "Please input a name!"
fi
$ ./test.sh mike
hello mike!
$ ./test.sh 
Please input a name!
2、特殊的参数变量
在bash shell中有一些特殊的参数变量,用于跟踪命令行参数。

(1)命令行参数个数
变量$#记录了脚本启动时的命令行参数的个数。

# cat test.sh
#!/bin/bash
echo "the count of args is : $#"
# ./test.sh 1 2 3 4 
the count of args is : 4
# ./test.sh 1 2 3 4 5 6 "hello"
the count of args is : 7
(2)命令行所有参数
变量$*和$@包含了全部命令行参数(不包括程序名称$0),变量$*将命令行的所有参数作为一个整体处理。而$@将所有参数当作同一个字符串的多个单词处理,允许对其中的值进行迭代。
# cat test.sh 
#!/bin/bash
echo "Using the \$* method:$*"
echo "Using the \$@ method:$@"
# ./test.sh 1 2 3 
Using the $* method:1 2 3
Using the $@ method:1 2 3
下面这个例子展示了$*和$@的不同之处
# cat test.sh
#!/bin/bash
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
# ./test.sh 1 2 3
$* Parameter #1 = 1 2 3
$@ Parameter #1 = 1
$@ Parameter #2 = 2
$@ Parameter #3 = 3
3、命令行参数移位
bash shell提供shift命令帮助操作命令行参数,shift命令可以改变命令行参数的相对位置,默认将每个参数左移一个位置,$3的值移给$2,$2的值移给$1,$1的值被丢弃。
注意:$0的值和位置不受shift命令影响。$1移掉后,该参数值就丢失了,不能恢复。
# cat test.sh
#!/bin/bash
count=1
while [ -n "$1" ]
do
  echo "Parameter #$count = $1"
  count=$[$count+1]
  shift
done
echo "\$#=$#"
echo "\$*=$*"
echo "\$@=$@"
echo "\$1=$1"
# ./test.sh 1 2 3 
Parameter #1 = 1
Parameter #2 = 2
Parameter #3 = 3
$#=0
$*=
$@=
$1=
shift命令可以带一个参数实现多位移变化,命令行参数向左多个位置。
# cat test.sh
#!/bin/bash
echo "The origin parameters are:$*"
shift 2
echo "after shift 2 the parameters are:$*"
# ./test.sh 1 2 3 4 5 
The origin parameters are:1 2 3 4 5
after shift 2 the parameters are:3 4 5 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值