首先看一个实例:假设有一个test的服务,可以通过命令对test进行启动、关闭或者重启,下面这个脚本就模拟这个功能:
#!/bin/bash #test.sh case $1 in start) echo "starting service......" sleep 1 echo "started the service!!" ;; stop) echo "stopping service......" sleep 1 echo "stopped the service!!" ;; restart) echo "restarting service......" sleep 1 echo "restarted the service!!" ;; *) echo "warning: invalid option -> ${1}" ;; esac
运行这个脚本,结果如下
[email protected]:~$ ./test.sh start starting service...... started the service!! [email protected]:~$ ./test.sh stop stopping service...... stopped the service!! [email protected]:~$ ./test.sh restart restarting service...... restarted the service!! [email protected]:~$ ./test.sh status warning: invalid option -> status [email protected]:~$