一、Shell脚本变量的含义
1、$#:表示执行脚本传入参数的个数
2、*:表示执行脚本传入参数的列表(不包括0)
3、$$:表示进程的id;Shell本身的PID(ProcessID,即脚本运行的当前 进程ID号)
4、$!:Shell最后运行的后台Process的PID(后台运行的最后一个进程的 进程ID号)
5、@:表示执行脚本传入参数的所有个数(不包括0)
6、$0:表示执行的脚本名称
7、$1:表示第一个参数
8、$2:表示第二个参数
9、$?:表示脚本执行的状态,0表示正常,其他表示错误
二 $*和$@的差异
在shell中,KaTeX parse error: Can't use function '' in math mode at position 3: @和̲
cat ./tmp/shll_test/test.sh
#!/bin/sh
MY_SHELL_PATH=`dirname $0`
echo "print shell script location:"
echo ${MY_SHELL_PATH}
echo "===================================="
echo "enter shell script location:${MY_SHELL_PATH}"
cd `dirname $0`
echo "list current directory content:"
ls -lh
echo "===================================="
echo "shell script name=${0}"
echo "===================================="
echo "first args=${1}"
echo "===================================="
echo "second args=${2}"
echo "===================================="
echo "arguments number=$#"
echo "===================================="
echo "arguments list content=$@"
echo "===================================="
echo "arguments list content=$*"
echo "===================================="
echo "the process id is " "$$"
echo "===================================="
echo "the shell execute return value is " "$?"
echo "===================================="
echo "the all paramters:"
for i in "$@"
do
echo $i #循环$#次
done
echo "===================================="
echo "the all paramters:"
for i in "$*"
do
echo $i
done
echo "===================================="
结果:
[root@t4-algo-7 /]# pwd
/
[root@t4-algo-7 /]# ll
total 76
lrwxrwxrwx. 1 root root 7 Nov 29 2018 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Nov 29 2018 boot
drwxr-xr-x 19 root root 3120 Dec 29 10:05 dev
drwxr-xr-x. 84 root root 4096 Jan 12 15:29 etc
drwxr-xr-x. 7 root root 4096 Dec 31 12:00 home
lrwxrwxrwx. 1 root root 7 Nov 29 2018 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Nov 29 2018 lib64 -> usr/lib64
drwx------. 2 root root 16384 Nov 29 2018 lost+found
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
drwxr-xr-x. 4 root root 4096 Dec 31 11:02 mnt
drwxr-xr-x. 6 root root 4096 Jan 12 11:53 opt
dr-xr-xr-x 400 root root 0 Jan 7 2020 proc
dr-xr-x---. 22 root root 4096 Jan 12 18:27 root
drwxr-xr-x 27 root root 840 Jan 12 12:01 run
lrwxrwxrwx. 1 root root 8 Nov 29 2018 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Mar 23 2020 sys
drwxrwxrwt. 12 root root 20480 Jan 12 18:34 tmp
drwxr-xr-x. 13 root root 4096 Nov 29 2018 usr
drwxr-xr-x. 19 root root 4096 Nov 29 2018 var
[root@t4-algo-7 shll_test]# pwd
/tmp/shll_test
[root@t4-algo-7 shll_test]# yum install -y tree
[root@t4-algo-7 shll_test]# tree
.
├── dir1
│ └── dir2
├── file.txt
└── test.sh
2 directories, 2 files
[root@t4-algo-7 shll_test]#
[root@t4-algo-7 /]# ./tmp/shll_test/test.sh 1 2 3,4
print shell script location:
./tmp/shll_test
====================================
enter shell script location:./tmp/shll_test
list current directory content:
total 8.0K
drwxr-xr-x 3 root root 4.0K Jan 12 18:22 dir1
-rw-r--r-- 1 root root 0 Jan 12 18:22 file.txt
-rwxr-xr-x 1 root root 1.2K Jan 12 18:50 test.sh
====================================
shell script name=./tmp/shll_test/test.sh
====================================
first args=1
====================================
second args=2
====================================
arguments number=3
====================================
arguments list content=1 2 3,4
====================================
arguments list content=1 2 3,4
====================================
the process id is 6152
====================================
the shell execute return value is 0
====================================
the all paramters:
1
2
3,4
====================================
the all paramters:
1 2 3,4
====================================
[root@t4-algo-7 /]#