Shell脚本位置参数的具体使用(shell脚本的使用)怎么可以错过

随心笔谈12个月前发布 admin
92 0



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

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}等

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=

每执行一次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,依次类推。与此同时,$#的值也会相应减一。

[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

位置参数既可以向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”

有时候批量处理所有位置参数更为实用,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脚本中的位置变量参数(特殊字符)实例讲解

© 版权声明

相关文章