尚观学习-shell-预定义变量 & 位置变量

预定义变量:
$0 进程名称
$$ PID
$? 命令执行后的返回状态.0 为执行正确,非 0 为执行错误
$# 位置参数的数量
$* 所有位置参数的内容


位置变量:

$1    $2    $3...


例子:

$?  

命令执行后的返回状态    0 为执行正确,非 0 为执行错误
[root@localhost ~]# echo $?  
0
[root@localhost ~]# ca /etc/passwd
bash: ca: command not found
[root@localhost ~]# echo $?
127


$0    

取当前进程的名称
[root@localhost ~]# vim a.sh
  1 #!/bin/bash
  2 echo the name of scripts: $0
[root@localhost ~]# bash a.sh     //执行
the name of scripts: a.sh

[root@localhost ~]# pgrep tail       // pgrep跟关键字
4510
[root@localhost ~]# pidof tail        // pidof跟进程名称
4510

[root@localhost ~]# nc -l 9999
[root@localhost ~]# nc 192.168.100.250 9999


$$  

取当前进程的PID
[root@localhost ~]# vim a.sh
  1 #!/bin/bash
  2 echo the name of scripts: $0
  3 echo the PID: $$
[root@localhost ~]# bash a.sh
the name of scripts: a.sh
the PID: 5464


位置变量
[root@localhost ~]# vim a.sh
  1 #!/bin/bash
  2 echo the name of scripts: $0
  3 echo the PID: $$
  4 echo 你输入的两个数字的和是:$[$1+$2]            //  $1  $2  位置变量
[root@localhost ~]# bash a.sh 3 5
the name of scripts: a.sh
the PID: 5644
你输入的两个数字的和是:8


$#  位置参数的数量
$*  位置参数的内容
[root@localhost ~]# vim a.sh
  1 #!/bin/bash
  2 echo the name of scripts: $0
  3 echo the PID: $$
  4 echo 你输入了$#个参数,他们是:$*
  5 echo 你输入的两个数字的和是:$[$1+$2]        // $(expr $1 + $2)        $`expr $1 + $2`
[root@localhost ~]# bash a.sh 12 15
the name of scripts: a.sh
the PID: 5767
你输入了2个参数,他们是:12 15
你输入的两个数字的和是:27


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值