shell变量

 $# 用于计算用户传递给脚本或函数的参数总数

if [ $# -lt 5 ];then
  echo "usage $0 <aaa> <bbb> <ccc> <ddd> <eee>"
  exit
fi

ADM=$1
ADM_PW=$2
DIST_ID=$3
INPUT_FILE=$4
LOG_FILE=$5

$@:所有参数列表。

$*:所有传入的参数作为一个整体。

[root@master-1 ansible]# cat test.sh
#!/bin/bash

echo "hello,ansible~"
ifconfig ens33 |awk '/inet /{print $2}'
script_path="$0"
echo "script_path is $script_path"


for i in "$@";
do
  echo $i;
done

for i in "$*";
do
  echo $i;
done

[root@master-1 ansible]# ./test.sh 1 2 3
hello,ansible~
192.168.43.129
script_path is ./test.sh
1
2
3
1 2 3

$0 是一个特殊变量,用于表示当前脚本的名称或执行的命令名称

如果是在脚本中,$0 表示脚本的文件名。

#!/bin/bash
echo "脚本名称是: $0"

执行

./example.sh
脚本名称是: ./example.sh

如果脚本是通过路径调用的(例如 ./example.sh 或 /path/to/example.sh),$0 会包含路径信息。

[root@master-1 ansible]# bash /opt/ansible/test.sh
hello,ansible~
192.168.43.129
script_path is /opt/ansible/test.sh

如果是在命令行中直接运行命令,$0 表示当前 Shell 的名称(例如 bash、zsh 等)

[root@master-1 prometheus-adapter]# echo "当前 Shell 是: $0"
当前 Shell 是: -bash

 

$? :获取上一次命令执行结果,0正常,非0异常

 

posted @ 2025-02-07 17:46  不会跳舞的胖子  阅读(5)  评论(0)    收藏  举报