Skip to content

Commit d966896

Browse files
committed
feat: use getopts(builtin) to handle options
1 parent f6a4aed commit d966896

File tree

2 files changed

+117
-80
lines changed

2 files changed

+117
-80
lines changed

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ scp CQU_drcom* -r [email protected]:/root/.
3030

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

33+
## 关于运行选项
34+
```bash
35+
-V 或 --dry-run 用于测试脚本(仅运行,不进行任何设置)
36+
-h 或 --help 显示帮助信息
37+
```
38+
3339
## 關於啓動腳本
3440
位置: `/etc/init.d/drcomctl`
3541
```sh
@@ -44,6 +50,10 @@ scp CQU_drcom* -r [email protected]:/root/.
4450
```
4551
所有有關的控制項目可至`Luci` -> `System - Startup` -> `Initscripts`尋找。
4652
## CHANGE LOG
53+
2020.09.06-2
54+
- 使用 `getopts` 来处理选项
55+
- 修正无选项时运行错误的问题
56+
4757
2020.09.06
4858
- 添加了循环判断,用于当信息填写有误时的修改而不用重新运行脚本
4959
- 添加了 `--dry-run` 运行选项以方便进行测试

setup.sh

Lines changed: 107 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ DRCOM_ORIGIN=latest-wired.py
44
DRCOM=drcom
55
DRCOM_PATH=/usr/bin
66
CONFIG_PATH=/etc
7+
# for cli options
8+
# "-" option is to rewrite long options which getopts do not support.
9+
# ":" behind "-" is to undertake option string value of "-"
10+
# like "--debug" option, "-" is a short option undertaking "-debug",
11+
# and "debug" is the actual option handle by getopts
12+
optspec="-:Vh"
13+
714

815
if [ ! -f "/etc/os-release" ];then
916
echo "Recheck your package. You cannot run this script."
@@ -69,7 +76,7 @@ root_pwd_change() {
6976
passwd root;;
7077
N|n)
7178
# shellcheck disable=SC2104
72-
break;;
79+
pass;;
7380
esac
7481
clear
7582
}
@@ -199,7 +206,7 @@ recheck() {
199206
echo "$wifi_password1" ;;
200207
N|n|*)
201208
# shellcheck disable=SC2104
202-
break;;
209+
pass;;
203210
esac
204211

205212
echo "Crontab:"
@@ -447,92 +454,112 @@ setup_done() {
447454
echo " you may find only one ssid of your routine in the list."
448455
echo "--------------------"
449456
}
450-
if [[ $1 == "--dry-run" ]]; then
451-
clear
452-
echo "This flag is for test and will print most of the variables."
453-
hello
454-
network_config
455-
root_pwd_change
456-
inform_gather
457-
wlan_ssid_settings
458-
wlan_passwd_setting
459-
setup_confirm
460-
setup_done
461-
else
462-
# config renew after sys-upgrade
463-
read -p "Is is installation after system upgrade? [y/N]: " ifSet_upgrade
464-
case $ifSet_upgrade in
465-
n|N|"")
457+
458+
setup_done_debug() {
459+
echo "=========="
460+
echo "Student number: "
461+
echo $username
462+
echo "Password: "
463+
echo $password
464+
echo "Wifi ssid (5Ghz) :" "$wifi_ssid0"
465+
echo "Passwd :" "$wifi_password0"
466+
echo "Wifi ssid (2.4Ghz) :" "$wifi_ssid1"
467+
echo "Passwd :" "$wifi_password1"
468+
echo "Crontab: "
469+
echo $ifSet
470+
echo "=========="
471+
uname -a
472+
echo $distro
473+
cat /etc/os-release
474+
}
475+
476+
# Handle actions without options
477+
if [ ! $1 ]; then
478+
# config renew after sys-upgrade
479+
read -p "Is is installation after system upgrade? [y/N]: " ifSet_upgrade
480+
case $ifSet_upgrade in
481+
n|N|"")
482+
clear
483+
hello
484+
network_config
485+
root_pwd_change
486+
inform_gather
487+
wlan_ssid_settings
488+
wlan_passwd_setting
489+
# recheck
490+
setup_confirm
491+
clean_up
492+
setup_packages
493+
setup_drcom
494+
setup_crontab
495+
setup_wlan
496+
setup_done
497+
;;
498+
y|Y)
499+
clear
500+
wifi_ssid0="the one you have set"
501+
wifi_ssid1="the one you have set"
502+
wifi_password0="the one you have set"
503+
wifi_password1="the one you have set"
504+
hello
505+
network_config
506+
inform_gather
507+
# recheck
508+
setup_confirm
509+
setup_packages
510+
setup_drcom
511+
setup_crontab
512+
setup_done
513+
echo "All done!"
514+
;;
515+
esac
516+
else # When running with options
517+
while getopts "$optspec" optchar; do
518+
case $optchar in
519+
-)
520+
case $OPTARG in
521+
dry-run)
466522
clear
467-
hello
523+
echo "This flag is for test and will print most of the variables."
468524
network_config
469525
root_pwd_change
470526
inform_gather
471527
wlan_ssid_settings
472528
wlan_passwd_setting
473-
# recheck
474529
setup_confirm
475-
clean_up
476-
setup_packages
477-
setup_drcom
478-
setup_crontab
479-
setup_wlan
480-
setup_done
530+
setup_done_debug
481531
;;
482-
y|Y)
483-
clear
484-
wifi_ssid0="the one you have set"
485-
wifi_ssid1="the one you have set"
486-
wifi_password0="the one you have set"
487-
wifi_password1="the one you have set"
488-
hello
489-
network_config
490-
inform_gather
491-
# recheck
492-
setup_confirm
493-
setup_packages
494-
setup_drcom
495-
setup_crontab
496-
setup_done
497-
echo "All done!"
532+
help)
533+
echo ""
534+
echo "USAGE: sh ./setup.sh [options]"
535+
echo ""
536+
echo "-V, --dry-run Verbose. Run the scripts without actually setting up."
537+
echo "-h, --help Display this message."
498538
;;
539+
esac
540+
;;
541+
V)
542+
clear
543+
echo "This flag is for test and will print most of the variables."
544+
hello
545+
network_config
546+
root_pwd_change
547+
inform_gather
548+
wlan_ssid_settings
549+
wlan_passwd_setting
550+
setup_confirm
551+
setup_done_debug
552+
;;
553+
h)
554+
echo "USAGE: sh ./setup.sh [options]"
555+
echo "-V, --dry-run Verbose. Run the scripts without actually setting up."
556+
echo "-h, --help Display this message."
557+
;;
558+
*)
559+
if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
560+
echo "Non-option argument: '-${OPTARG}'" >&2
561+
fi
562+
;;
499563
esac
564+
done
500565
fi
501-
# config renew after sys-upgrade
502-
# read -p "Is is installation after system upgrade? [y/N]: " ifSet_upgrade
503-
# case $ifSet_upgrade in
504-
# n|N|"")
505-
# clear
506-
# hello
507-
# network_config
508-
# root_pwd_change
509-
# inform_gather
510-
# wlan_ssid_settings
511-
# wlan_passwd_setting
512-
# # recheck
513-
# setup_confirm
514-
# clean_up
515-
# setup_packages
516-
# setup_drcom
517-
# setup_crontab
518-
# setup_wlan
519-
# setup_done
520-
# ;;
521-
# y|Y)
522-
# clear
523-
# wifi_ssid0="the one you have set"
524-
# wifi_ssid1="the one you have set"
525-
# wifi_password0="the one you have set"
526-
# wifi_password1="the one you have set"
527-
# hello
528-
# network_config
529-
# inform_gather
530-
# # recheck
531-
# setup_confirm
532-
# setup_packages
533-
# setup_drcom
534-
# setup_crontab
535-
# setup_done
536-
# echo "All done!"
537-
# ;;
538-
# esac

0 commit comments

Comments
 (0)