Shell script 的默认变量($0, $1...)

假设我要运行一个可以携带参数的 script ,运行该脚本后萤幕会显示如下的数据:
  • 程序的档名为何?
  • 共有几个参数?
  • 若参数的个数小於 2 则告知使用者参数数量太少
  • 全部的参数内容为何?
  • 第一个参数为何?
  • 第二个参数为何

#!/bin/bash
#The program shows the script name,and the parameters....

echo "The script name is ==> $0"
echo "The num of the parameters is ==>$#"
[ $# -lt 2 ] && echo "The num is less than 2. Stop here!" && exit 0
echo "The whole parameter is ==>
'$@'"
echo "The 1st parameter is ==>$1"
echo "The 2nd parameter is >$2"

执行脚本:
[oracle@SOR_SYS~]$ sh parameters.sh opt1 oracle 192.168.50.229 8081
The script name is ==> parameters.sh
The num of the parameters is ==>4
The whole parameter is ==> 'opt1 oracle 192.168.50.229 8081'
The 1st parameter is ==>opt1
The 2nd parameter is >oracle
[oracle@SOR_SYS~]$

  • $# :代表后接的参数『个数』,以上表为例这里显示为『 4 』;
  • $@ :代表『 "$1" "$2" "$3" "$4" 』之意,每个变量是独立的(用双引号括起来);
  • $* :代表『 "$1c$2c$3c$4" 』,其中 c 为分隔字节,默认为空白键, 所以本例中代表『 "$1 $2 $3 $4" 』之意。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值