Skip to content

Commit

Permalink
feat: use getopts(builtin) to handle options
Browse files Browse the repository at this point in the history
  • Loading branch information
weearc committed Sep 6, 2020
1 parent f6a4aed commit d966896
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 80 deletions.
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ scp CQU_drcom* -r [email protected]:/root/.

5. 如果返回联网失败,可稍后再次检查网络连通情况

## 关于运行选项
```bash
-V 或 --dry-run 用于测试脚本(仅运行,不进行任何设置)
-h 或 --help 显示帮助信息
```

## 關於啓動腳本
位置: `/etc/init.d/drcomctl`
```sh
Expand All @@ -44,6 +50,10 @@ scp CQU_drcom* -r [email protected]:/root/.
```
所有有關的控制項目可至`Luci` -> `System - Startup` -> `Initscripts`尋找。
## CHANGE LOG
2020.09.06-2
- 使用 `getopts` 来处理选项
- 修正无选项时运行错误的问题

2020.09.06
- 添加了循环判断,用于当信息填写有误时的修改而不用重新运行脚本
- 添加了 `--dry-run` 运行选项以方便进行测试
Expand Down
187 changes: 107 additions & 80 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ DRCOM_ORIGIN=latest-wired.py
DRCOM=drcom
DRCOM_PATH=/usr/bin
CONFIG_PATH=/etc
# for cli options
# "-" option is to rewrite long options which getopts do not support.
# ":" behind "-" is to undertake option string value of "-"
# like "--debug" option, "-" is a short option undertaking "-debug",
# and "debug" is the actual option handle by getopts
optspec="-:Vh"


if [ ! -f "/etc/os-release" ];then
echo "Recheck your package. You cannot run this script."
Expand Down Expand Up @@ -69,7 +76,7 @@ root_pwd_change() {
passwd root;;
N|n)
# shellcheck disable=SC2104
break;;
pass;;
esac
clear
}
Expand Down Expand Up @@ -199,7 +206,7 @@ recheck() {
echo "$wifi_password1" ;;
N|n|*)
# shellcheck disable=SC2104
break;;
pass;;
esac

echo "Crontab:"
Expand Down Expand Up @@ -447,92 +454,112 @@ setup_done() {
echo " you may find only one ssid of your routine in the list."
echo "--------------------"
}
if [[ $1 == "--dry-run" ]]; then
clear
echo "This flag is for test and will print most of the variables."
hello
network_config
root_pwd_change
inform_gather
wlan_ssid_settings
wlan_passwd_setting
setup_confirm
setup_done
else
# config renew after sys-upgrade
read -p "Is is installation after system upgrade? [y/N]: " ifSet_upgrade
case $ifSet_upgrade in
n|N|"")

setup_done_debug() {
echo "=========="
echo "Student number: "
echo $username
echo "Password: "
echo $password
echo "Wifi ssid (5Ghz) :" "$wifi_ssid0"
echo "Passwd :" "$wifi_password0"
echo "Wifi ssid (2.4Ghz) :" "$wifi_ssid1"
echo "Passwd :" "$wifi_password1"
echo "Crontab: "
echo $ifSet
echo "=========="
uname -a
echo $distro
cat /etc/os-release
}

# Handle actions without options
if [ ! $1 ]; then
# config renew after sys-upgrade
read -p "Is is installation after system upgrade? [y/N]: " ifSet_upgrade
case $ifSet_upgrade in
n|N|"")
clear
hello
network_config
root_pwd_change
inform_gather
wlan_ssid_settings
wlan_passwd_setting
# recheck
setup_confirm
clean_up
setup_packages
setup_drcom
setup_crontab
setup_wlan
setup_done
;;
y|Y)
clear
wifi_ssid0="the one you have set"
wifi_ssid1="the one you have set"
wifi_password0="the one you have set"
wifi_password1="the one you have set"
hello
network_config
inform_gather
# recheck
setup_confirm
setup_packages
setup_drcom
setup_crontab
setup_done
echo "All done!"
;;
esac
else # When running with options
while getopts "$optspec" optchar; do
case $optchar in
-)
case $OPTARG in
dry-run)
clear
hello
echo "This flag is for test and will print most of the variables."
network_config
root_pwd_change
inform_gather
wlan_ssid_settings
wlan_passwd_setting
# recheck
setup_confirm
clean_up
setup_packages
setup_drcom
setup_crontab
setup_wlan
setup_done
setup_done_debug
;;
y|Y)
clear
wifi_ssid0="the one you have set"
wifi_ssid1="the one you have set"
wifi_password0="the one you have set"
wifi_password1="the one you have set"
hello
network_config
inform_gather
# recheck
setup_confirm
setup_packages
setup_drcom
setup_crontab
setup_done
echo "All done!"
help)
echo ""
echo "USAGE: sh ./setup.sh [options]"
echo ""
echo "-V, --dry-run Verbose. Run the scripts without actually setting up."
echo "-h, --help Display this message."
;;
esac
;;
V)
clear
echo "This flag is for test and will print most of the variables."
hello
network_config
root_pwd_change
inform_gather
wlan_ssid_settings
wlan_passwd_setting
setup_confirm
setup_done_debug
;;
h)
echo "USAGE: sh ./setup.sh [options]"
echo "-V, --dry-run Verbose. Run the scripts without actually setting up."
echo "-h, --help Display this message."
;;
*)
if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
echo "Non-option argument: '-${OPTARG}'" >&2
fi
;;
esac
done
fi
# config renew after sys-upgrade
# read -p "Is is installation after system upgrade? [y/N]: " ifSet_upgrade
# case $ifSet_upgrade in
# n|N|"")
# clear
# hello
# network_config
# root_pwd_change
# inform_gather
# wlan_ssid_settings
# wlan_passwd_setting
# # recheck
# setup_confirm
# clean_up
# setup_packages
# setup_drcom
# setup_crontab
# setup_wlan
# setup_done
# ;;
# y|Y)
# clear
# wifi_ssid0="the one you have set"
# wifi_ssid1="the one you have set"
# wifi_password0="the one you have set"
# wifi_password1="the one you have set"
# hello
# network_config
# inform_gather
# # recheck
# setup_confirm
# setup_packages
# setup_drcom
# setup_crontab
# setup_done
# echo "All done!"
# ;;
# esac

0 comments on commit d966896

Please sign in to comment.