Shell脚本位置参数如何使用

其他教程   发布日期:2023年10月16日   浏览次数:139

这篇文章主要介绍“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 "
File Type:"
        file "$1"
        echo -e "
File 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 "
File Type:"
            file "$1"
                echo -e "
File 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 "
" '$* :';print_params $*
        echo -e "
" '"$*" :';print_params "$*"
        echo -e "
" '$@ :';print_params $@
        echo -e "
" '"$@" :';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脚本位置参数如何使用的资料请关注九品源码其它相关文章!