首先看一个实例:假设有一个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
运行这个脚本,结果如下
ubuntu@ubuntu:~$ ./test.sh start starting service...... started the service!! ubuntu@ubuntu:~$ ./test.sh stop stopping service...... stopped the service!! ubuntu@ubuntu:~$ ./test.sh restart restarting service...... restarted the service!! ubuntu@ubuntu:~$ ./test.sh status warning: invalid option -> status ubuntu@ubuntu:~$