shell参数的输入与运用

在shell中参数的传递,也就是参数的输入使用$n的格式进行输入。

$n,其中,n代表数字,1就是在执行脚本时输入的第一个参数,2就是在执行脚本时输入的第一个参数,以此类推。

#!/bin/bash
#Filename: Day02.sh
#Introduction:It's file is to learning the basic knowledge of shell 

echo "Output"
echo "The file name: $0"
echo "The first parameter: $1"
echo "The second parameter: $1"
echo "The number of paramter is $#"
leeguo@leeguo-PC:~/Nutstore/Shell_code$ bash Day02.sh 1 2
Output
The file name: Day02.sh
The first parameter: 1
The second of paramter is 2

这个符号$,除了参数的传输外,还有其他的功能。
- 0 #:当前脚本输入参数的个数
- $*:以字符串的形式显示所有输入的参数,引号"$*"是一一个字符串的形式输出形式进行输出
- $@:和上面"$*"的功能基本相同,都是在引号"$@"是一单个的输出形式进行输出
- $$:该脚本进程的ID号
- $?:脚本在运行完成后,退出的状态,若为0则说明没有错误

#!/bin/bash
#Filename: Day02.sh
#Introduction:It's file is to learning the basic knowledge of shell 

echo "$*"
echo "$@"

for target in "$*";
do 
echo $target
done


for target in "$@";
do 
echo $target
done

运行的结果:

leeguo@leeguo-PC:~/Nutstore/Shell_code$ bash Day02.sh 1 2 3
# The output of $*
1 2 3
# The output of $@
1 2 3
# --------
1 2 3
# --------
1
2
3

本文参考:http://www.runoob.com/linux/linux-shell-passing-arguments.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值