编写脚本:
[root@mysql-node1 ~]# cat vartest.sh
#!/bin/sh
bingo() {
echo "Bingo! Below is the output of a defined function."
echo "filelname: $0"
echo "argument List: $@"
echo "1st argument: $1"
echo '############## Loop of $@ ############'
for gee in $@
do
echo $gee
done
}
echo "PID: $$"
echo `ps -ef | grep $0`
echo "filelname: $0"
echo " "
echo "returnValue of Last Shell Cmd: $?"
echo "argument List: $*"
echo "argument List: $@"
echo "number of arguments: $#"
echo "1st argument: $1"
echo "2nd argument: $2"
echo " "
bingo Apple Amazon HBO Netflix
[root@mysql-node1 ~]#
测试:
[root@mysql-node1 ~]# ./vartest.sh Arthur Morgan Freeman
PID: 1742
root 1742 1469 0 04:33 pts/0 00:00:00 /bin/sh ./vartest.sh Arthur Morgan Freeman root 1743 1742 0 04:33 pts/0 00:00:00 /bin/sh ./vartest.sh Arthur Morgan Freeman root 1745 1743 0 04:33 pts/0 00:00:00 grep ./vartest.sh
filelname: ./vartest.sh
returnValue of Last Shell Cmd: 0
argument List: Arthur Morgan Freeman
argument List: Arthur Morgan Freeman
number of arguments: 3
1st argument: Arthur
2nd argument: Morgan
Bingo! Below is the output of a defined function.
filelname: ./vartest.sh
argument List: Apple Amazon HBO Netflix
1st argument: Apple
############## Loop of $@ ############
Apple
Amazon
HBO
Netflix
[root@mysql-node1 ~]#
总结:
$$
:当前shell脚本运行时的PID;$0
:当前运行shell脚本的文件名;$?
:上一次运行shell命令的返回值,0表示正常;$#
:当前运行shell脚本传入的参数个数;$*
:当前运行shell脚本传入的参数列表;$@
:当前运行shell脚本传入的参数列表;$1
:当前运行shell脚本传入的第一个参数;$2
:当前运行shell脚本传入的第二个参数,以此类推。