温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Shell脚本位置参数如何使用

发布时间:2023-03-23 13:41:44 来源:亿速云 阅读:128 作者:iii 栏目:开发技术

这篇文章主要介绍“Shell脚本位置参数如何使用”,在日常操作中,相信很多人在Shell脚本位置参数如何使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Shell脚本位置参数如何使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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脚本位置参数如何使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI