这是 case 语句的替代方法。
使用bash/shell 中的if 语句启动/停止/重新启动/卸载您的程序。
#!/bin/bash
start_module() {
# Your start Code
}
stop_module() {
# Your stop Code
}
restart_module() {
# Your restart Code
}
uninstall_module() {
# Your uninstall Code
}
if [ $# != 1 ]; then # If Argument is not exactly one
echo "Some message"
exit 1 # Exit the program
fi
ARGUMENT=$(echo "$1" | awk '{print tolower($0)}') # Converts Argument in lower case. This is to make user Argument case independent.
if [[ $ARGUMENT == start ]]; then
start_module
elif [[ $ARGUMENT == stop ]]; then
stop_module
elif [[ $ARGUMENT == uninstall ]]; then
uninstall_module
elif [[ $ARGUMENT == restart ]]; then
restart_module
else
echo "Only one valid argument accepted: START | STOP | RESTART | UNINSTALL
case doesn't matter. "
fi
将此代码保存到 myScript.sh
Usage:
./myScript.sh Start
./myScript.sh Stop
./myScript.sh check
./myScript.sh uninstall
这是体现这种执行方式的真实程序示例。
旁注:省略任何现有的模块/功能或添加另一个可以轻松完成。
如何取出/抑制特定模块的运行(例如维护)?
从if 语句块中取出特定模块将禁用它,因为在运行时不会调用该模块。