$@和($@)

$@是传入的所有参数(不是数组)

($@)是数组

$*是传入的所有参数列表(是数组)


数组与非数组

[nat@system1 ~]$ test="a b c"
[nat@system1 ~]$ echo ${test[@]}
a b c
[nat@system1 ~]$ echo ${#test[@]}
1
[nat@system1 ~]$ echo ${!test[@]}
0
[nat@system1 ~]$ test1=(a b c)
[nat@system1 ~]$ echo ${test1[@]}
a b c
[nat@system1 ~]$ echo ${#test1[@]}
3
[nat@system1 ~]$ echo ${!test1[@]}
0 1 2
[nat@system1 ~]$ 



[nat@system1 ~]$ cat testargs.sh

#/bin/bah
set -e
pip_u_install(){
   local args= ($@)
   local envPip=pip
   local tar_pkgs_dirs=$(pwd)/py_pkgs
   echo $args
   echo $args[@]
   echo ${!args[@]}
   for i in "${!args[@]}" 2
   do
        echo $i        
local pkg_name="${args[$i]}"
        echo $pkg_name+
#echo ${args[$i]}
#$envPip install -U $pkg_name --no-index --find-links file://$tar_pkgs_dirs
   done
}

pip_u_install $@


[nat@system1 ~]$ sh testargs.sh 1 2 3 4 5
1
1[@]
0 1 2 3 4
0
1+
1
2+
2
3+
3
4+
4
5+
2
3+

[nat@system1 ~]$ 






[nat@system1 ~]$ sh testargs.sh 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5[@]
0
0
1 2 3 4 5+
2
+
[nat@system1 ~]$ cat testargs.sh 
#/bin/bah
set -e
pip_u_install(){
   local args=$@
   local envPip=pip
   local tar_pkgs_dirs=$(pwd)/py_pkgs
   echo $args
   echo $args[@]
   echo ${!args[@]}
   for i in "${!args[@]}" 2
   do
        echo $i        
local pkg_name="${args[$i]}"
        echo $pkg_name+
#echo ${args[$i]}
#$envPip install -U $pkg_name --no-index --find-links file://$tar_pkgs_dirs
   done
}

pip_u_install $@
[nat@system1 ~]$ 

猜你喜欢

转载自blog.csdn.net/jackliu16/article/details/80474358