阅读背景:

Linux shell编程(四)shell脚本中是如何传入参数的

来源:互联网 
vim /root/test.sh
#!/bin/bash
if [ vim /root/test.sh
#!/bin/bash
if [ $1 == "start" ]
  then   
  echo "do start"
  command groups
  elif [ $1 == "stop" ]
  then
  echo "do stop"
  command group
  else
  echo "Please make sure the positon variable is start or stop."
fi

#sh test.sh stop 

$1取到了stop这个参数,接收来自命令行传入的参数,第一个参数用$1表示,第二个参数$2表示,。。。以此类推。
vim /root/test.sh
#!/bin/bash
for args in $@
do
   echo $args
done

把上面这段代码录入保存为test.sh,授限可执行chmod +x test.sh:
#sh /root/test.sh arg1 arg2 arg3 arg4 xxx
输出参数:
arg1
arg3
arg4
xxx

这个例子中,我们用到了之“$@”,它代表了所有的命令行参数。

== "start" ]
  then    



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: