shell参数的输入与运用

来源:互联网 发布:网络借贷陷阱 编辑:程序博客网 时间:2024/06/05 09:46

在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 2OutputThe file name: Day02.shThe first parameter: 1The 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 $targetdonefor target in "$@";do echo $targetdone

运行的结果:

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# --------123

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

原创粉丝点击