位置变量参数

1、脚本中使用位置参数 

#!/bin/bash
#param
echo "this is script name    : $0"
echo "this is the first name : $1"
echo "this is the second name: $2"
echo "this is the three name : $3"

这里传递3个参数,$0是脚本名自身;

注意:$0返回的是当前目录路径,如下

#!/bin/sh
#param2
echo "hello world this is $0 calling"

test@linux_NGIN:~/script> ./param2
hello world this is ./param2 calling               ------当前目录路径

#!/bin/sh
#param2
echo "hello world this is `basename $0` calling"        ------使用basename获取脚本名称

test@linux_NGIN:~/script> ./param2
hello world this is param2 calling

2、向系统命令传递参数

#!/bin/sh
#findfile
find / -name $1 -print   -------查找文件名

#!/bin/sh
#who_is
grep $1 /etc/passwd | awk -F: '{print $1 "\t" $6}'  ---------查找当前用户名并输出其home目录

 

3、特定变量参数

$#    传递到脚本的参数个数

$*     以一个单字符串显示所有向脚本传递的参数。与位置变量不同,此选项参数可超过9个

$$    脚本运行的当前进程ID号

$!     后台运行的最后一个进程的进程ID号

$@  与$#相同,但使用时加引号,并在引号中返回每个参数

$-     显示SHELL当前选项,与set命令相同

$?    显示最后命令的退出状态,0为没有错误,其他任何值表明有错误


 

 


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值