You are not logged in.

#1 2021-07-24 21:18:52

Tx86
Member
Registered: 2021-07-24
Posts: 4

[SOLVED] Select command, unknown error

I have a script that I'm working on to use systemctl with ease, and can't seem to find the issue with why certain commands aren't working properly.

#!/bin/bash

 # System Control Script, a lazy way to use systemctl

 # Updated: 23-07-21, Version 1.0

 # You must be a sudoer or root to use this script


sctl="sudo systemctl"


echo; echo "System Control Script"; echo

PS3="Control Command: "
echo "Control Menu:"; echo; select command in "System Status" "Restart systemd manager" "List Installed Units" "List Running Units" "List Failed Units" \
"Unit Status" "Start Unit" "Stop Unit" "Restart Unit" "Restart Unit with configuration" "Check if Unit is Enabled upon boot" "Enable Unit upon boot" \
"Enable Unit upon boot with Immediate Start" "Disable Unit upon boot" "Reenable Unit" "Mask Unit" "Unmask Unit" "Exit"
do
case $command in
"System Status") echo; $sctl status; echo;;

"Restart systemd manager") echo; $sctl daemon-reload; echo;;

"List Installed Units") echo; $sctl list-unit-files; echo;;

"List Running Units") echo; $sctl list-units --all; echo;;

"List Failed Units") echo; $sctl --failed; echo;;

"Unit Status") echo; read -p "Unit: " statunit; $sctl status "$statunit";;

"Start Unit") echo; read -p "Unit: " stunit; $sctl start "$stunit"; $sctl status "$stunit";;

"Stop Unit") echo; read -p "Unit: " spunit; $sctl stop "$spunit"; $sctl status "$spunit";;

"Restart Unit") echo; read -p "Unit: " rstunit; $sctl restart "$rstunit"; $sctl status "$rstunit;;

"Restart Unit with configuration") echo; read -p "Unit: " rlunit; $sctl reload "$rlunit"; $sctl status "$rlunit";;

"Check if Unit is Enabled upon boot") echo; read -p "Unit: " chunit; $sctl is-enabled "$chunit";;

"Enable Unit upon boot") echo; read -p "Unit: " eunit; $sctl enable "$eunit"; $sctl is-enabled "$eunit";;

"Enable Unit upon boot with Immediate Start") echo; read -p "Unit: " ecunit; $sctl enable --now "$ecunit"; $sctl is-enabled "$ecunit";;

"Disable Unit upon boot") echo; read -p "Unit: " dunit; $sctl disable "$dunit"; $sctl is-enabled "$dunit";;

"Reenable Unit") echo; read -p "Unit: " reunit; $sctl reenable "$reunit"; $sctl is-enabled "$reunit;;

"Mask Unit") echo; echo "Warning: Masked Units cannot be used as dependency"; echo; read -p "Unit: " munit; $sctl mask "$munit"; $sctl status "$munit";;

"Unmask Unit") echo; read -p "Unit: " umunit; $sctl unmask "$umunit"; $sctl status "$umunit";;

"Exit") echo; echo "Goodbye."; echo; exit;;
*) echo; echo "Command not recognized.";;
esac
echo; echo "Press [Enter] to view Control Menu again."; echo;
done

For some reason every opton between "Restart Unit" and "Mask Unit" (options 10-15 when the script is executed) won't seem to work.

The output of these options are all:

Command not recognized.

Which is because of the line:

*) echo; echo "Command not recognized.";;

Which serves to let me know that I didn't type the option made available by the select command properly, and not that the code attached to that option failed.

What I can't figure out is why it's not working. I have two other scripts set up like this and have never ran into this issue before. I really don't understand why it doesn't recognize options 10-15, but will execute all the others as it's supposed to. I'm 99% sure that this is an error within the script itself, but I can't seem to find any typos. Any help or input would be greatly appreciated.

Last edited by Tx86 (2021-07-24 23:55:15)

Offline

#2 2021-07-24 21:42:15

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,340

Re: [SOLVED] Select command, unknown error

You need to fix quoting, some variables lack quotes.

Offline

#3 2021-07-24 21:53:17

Tx86
Member
Registered: 2021-07-24
Posts: 4

Re: [SOLVED] Select command, unknown error

Holy cow, man. I swear I looked over this thing 100 times, I don't know how I missed those two little quotes. Thank you, you just cured two days of frustration and tunnel vision.

Fixed and working code:

#!/bin/bash

 # System Control Script, a lazy way to use systemctl

 # Updated: 23-07-21, Version 1.0

 # You must be a sudoer or root to use this script


sctl="sudo systemctl"


echo; echo "System Control Script"; echo

PS3="Control Command: "
echo "Control Menu:"; echo; select command in "System Status" "Restart systemd manager" "List Installed Units" "List Running Units" "List Failed Units" \
"Unit Status" "Start Unit" "Stop Unit" "Restart Unit" "Restart Unit with configuration" "Check if Unit is Enabled upon boot" "Enable Unit upon boot" \
"Enable Unit upon boot with Immediate Start" "Disable Unit upon boot" "Reenable Unit" "Mask Unit" "Unmask Unit" "Exit"
do
case $command in
"System Status") echo; $sctl status; echo;;

"Restart systemd manager") echo; $sctl daemon-reload; echo;;

"List Installed Units") echo; $sctl list-unit-files; echo;;

"List Running Units") echo; $sctl list-units --all; echo;;

"List Failed Units") echo; $sctl --failed; echo;;

"Unit Status") echo; read -p "Unit: " statunit; $sctl status "$statunit";;

"Start Unit") echo; read -p "Unit: " stunit; $sctl start "$stunit"; $sctl status "$stunit";;

"Stop Unit") echo; read -p "Unit: " spunit; $sctl stop "$spunit"; $sctl status "$spunit";;

"Restart Unit") echo; read -p "Unit: " rstunit; $sctl restart "$rstunit"; $sctl status "$rstunit";;

"Restart Unit with configuration") echo; read -p "Unit: " rlunit; $sctl reload "$rlunit"; $sctl status "$rlunit";;

"Check if Unit is Enabled upon boot") echo; read -p "Unit: " chunit; $sctl is-enabled "$chunit";;

"Enable Unit upon boot") echo; read -p "Unit: " eunit; $sctl enable "$eunit"; $sctl is-enabled "$eunit";;

"Enable Unit upon boot with Immediate Start") echo; read -p "Unit: " ecunit; $sctl enable --now "$ecunit"; $sctl is-enabled "$ecunit";;

"Disable Unit upon boot") echo; read -p "Unit: " dunit; $sctl disable "$dunit"; $sctl is-enabled "$dunit";;

"Reenable Unit") echo; read -p "Unit: " reunit; $sctl reenable "$reunit"; $sctl is-enabled "$reunit";;

"Mask Unit") echo; echo "Warning: Masked Units cannot be used as dependency"; echo; read -p "Unit: " munit; $sctl mask "$munit"; $sctl status "$munit";;

"Unmask Unit") echo; read -p "Unit: " umunit; $sctl unmask "$umunit"; $sctl status "$umunit";;

"Exit") echo; echo "Goodbye."; echo; exit;;
*) echo; echo "Command not recognized.";;
esac
echo; echo "Press [Enter] to view Control Menu again."; echo;
done

Again, thank you for pointing out such a silly little mistake. Who knows how long it would've taken me to notice that.

Offline

#4 2021-07-24 22:39:06

2ManyDogs
Forum Moderator
Registered: 2012-01-15
Posts: 3,735

Re: [SOLVED] Select command, unknown error

Please remember to mark your thread [SOLVED] (edit the title of your first post).

Offline

#5 2021-07-24 23:22:03

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,340

Re: [SOLVED] Select command, unknown error

Tx86 wrote:

Who knows how long it would've taken me to notice that.

Use an editor with syntax highlighting like vim and use shellcheck to warn you about scripting issues.

Offline

Board footer

Powered by FluxBB