Shell脚本位置参数的具体使用

2023-12-05 0 1,000
目录
  • 1.访问命令行
  • 2 确定参数个数
  • 3 shift-访问多个参数
  • 4 简单应用
  • 5 在Shell函数中使用位置参数
  • 6 批量处理位置参数

1.访问命令行

Shell提供了一组名为位置参数的变了,其中包含了命令行上的各个单词,这些变量按照0-9分别命名,

[sysadmin@ansible bin]$ cat posit-param.sh
#!/bin/bash
echo \”
\\$0 = $0
\\$1 = $1
\\$2 = $2
\\$3 = $3
\\$4 = $4
\\$5 = $5
\\$6 = $6
\\$7 = $7
\\$8 = $8
\\$9 = $9
\”
[sysadmin@ansible bin]$ posit-param.sh
$0 = /home/sysadmin/bin/posit-param.sh
$1 =
$2 =
$3 =
$4 =
$5 =
$6 =
$7 =
$8 =
$9 =

就算没有提供参数值,$0始终出现在命令行中的第一项,表示执行程序的路径。如果提供了参数值,会看到下列执行结果:

[sysadmin@ansible bin]$ posit-param.sh a b c d

$0 = /home/sysadmin/bin/posit-param.sh$1 = a$2 = b$3 = c$4 = d$5 =$6 =$7 =$8 =$9 =

能通过参数扩展访问的位置参数不止9个,要想指定第9个之后的参数,将数字放入花括号中即可。即${10}、${211}等

2 确定参数个数

Shell还提供了变量$#,其中包含了命令行中的参数个数

[sysadmin@ansible bin]$ cat posit-param.sh
#!/bin/bash
echo \”
Number of arguments: $#
\\$0 = $0
\\$1 = $1
\\$2 = $2
\\$3 = $3
\\$4 = $4
\\$5 = $5
\\$6 = $6
\\$7 = $7
\\$8 = $8
\\$9 = $9
\”
[sysadmin@ansible bin]$ posit-param.sh a b c d

Number of arguments: 4
$0 = /home/sysadmin/bin/posit-param.sh
$1 = a
$2 = b
$3 = c
$4 = d
$5 =
$6 =
$7 =
$8 =
$9 =

3 shift-访问多个参数

每执行一次shift命令,就将所有的参数“左移一个位置”。实际上,通过shift命令,我们可以从始至终只和一个参数打交道(除了$0):

[sysadmin@ansible bin]$ cat posit-param2.sh
#!/bin/bash
count=1
while [[ $# -gt 0 ]]; do
echo \”Argument $count = $1\”
count=$((count + 1))
shift
done
[sysadmin@ansible bin]$ posit-param2.sh a b c d
Argument 1 = a
Argument 2 = b
Argument 3 = c
Argument 4 = d

每次执行shift,$2的值就会移入$1,然后$3的值移入$2,依次类推。与此同时,$#的值也会相应减一。

4 简单应用

[sysadmin@ansible bin]$ cat file-info
#!/bin/bash

#file-info

PROGNAME=\”$(basename \”$0\”)\”

if [[ -e \”$1\” ]]; then
        echo -e \”\\nFile Type:\”
        file \”$1\”
        echo -e \”\\nFile Status:\”
        stat \”$1\”
else
        echo \”$PROGNAME: usage: $PROGNAME file\” >&2
        exit 1
fi

5 在Shell函数中使用位置参数

位置参数既可以向Shell脚本传递参数,也可以向Shell函数传递参数。作为演示,我们将file_info脚本改写成Shell函数:

[sysadmin@ansible bin]$ cat file-info
#!/bin/bash

#file-info

file_info () {
        if [[ -e \”$1\” ]]; then
        echo -e \”\\nFile Type:\”
            file \”$1\”
                echo -e \”\\nFile Status:\”
                stat \”$1\”
        else
                echo \”$FUNCNAME: usage: $FUNCNAME file\” >&2
                return 1
        fi
}

file_info \”$1\”

6 批量处理位置参数

有时候批量处理所有位置参数更为实用,Shell为此提供了两个特殊参数*和@,两者均可扩展成完整的位置参数列表,但其区别有些微妙。

参数描述$*扩展成从1开始的位置参数列表。如果它出现在双引号内部,则扩展成由双引号引用的字符串,其中包含了所有的位置参数,彼此之间以Shell变量IFS的第一个字符分割(默认是空格符)$@扩展成从1开始的位置参数列表,如果它出现在双引号内部,则将每个位置参数扩展成独立的单词

[sysadmin@ansible bin]$ cat posit-params3
#!/bin/bash

# posit-params3

print_params () {
        echo \”\\$1 = $1\”
        echo \”\\$2 = $2\”
        echo \”\\$3 = $3\”
        echo \”\\$4 = $4\”
}

pass_params () {
        echo -e \”\\n\” \’$* :\’;print_params $*
        echo -e \”\\n\” \’\”$*\” :\’;print_params \”$*\”
        echo -e \”\\n\” \’$@ :\’;print_params $@
        echo -e \”\\n\” \’\”$@\” :\’;print_params \”$@\”
}

pass_params \”word\” \”words with spaces\”
[sysadmin@ansible bin]$ posit-params3

 $* :
$1 = word
$2 = words
$3 = with
$4 = spaces

 \”$*\” :
$1 = word words with spaces
$2 =
$3 =
$4 =

 $@ :
$1 = word
$2 = words
$3 = with
$4 = spaces

 \”$@\” :
$1 = word
$2 = words with spaces
$3 =
$4 =

到目前为止,“$@”适用于大部分情况,因为其保留了每个位置参数的整体性。为了保证安全性,应该坚持使用这种方法。

到此这篇关于Shell脚本位置参数的具体使用的文章就介绍到这了,更多相关Shell脚本位置参数内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源网!

您可能感兴趣的文章:

  • Shell脚本中的位置变量参数(特殊字符)实例讲解

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 linux shell Shell脚本位置参数的具体使用 https://www.u-9.cn/jiaoben/linuxshell/101233.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务