shell脚本中$#、$*、$@、$?、$0-n等含义一次性搞明白!!!

35 篇文章 0 订阅
28 篇文章 0 订阅

一、Shell脚本变量的含义

1、$#:表示执行脚本传入参数的个数

2、$*:表示执行脚本传入参数的列表(不包括$0)

3、$$:表示进程的id;Shell本身的PID(ProcessID,即脚本运行的当前 进程ID号)

4、$!:Shell最后运行的后台Process的PID(后台运行的最后一个进程的 进程ID号)

5、$@:表示获取执行脚本传入的所有参数

6、$0:表示执行的脚本名称

7、$1:表示第一个参数

8、$2:表示第二个参数

9、$?:表示脚本执行的状态,0表示正常,其他表示错误

二、$*和$@的差异

在shell中,$@和$*都表示命令行所有的参数(不包含$0),但是$*将命令行所有的参数看成一个整体,而$@则区分各个参数

Demo 脚本示例:

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 /]# 

  • 35
    点赞
  • 346
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值