Question Bash Script Case Exit Status

Hi, have a question on bash exit status as shown in 2. I tried both the one on color at
https://phoenixnap.com/kb/bash-case-statement and my script. Both if entering the commands in terminal pop up the message but go to command prompt without allowing entering another choice. If I launch either as script an incorrect entry dumps the window. Maybe misunderstanding but have another script (modified y/n) that allows for another try.
#3 is just information on how to exit, and script works same if I delete #1 line 27.
Aside from that the script works great and is totally usable and better than having a bunch of scripts. Have a theme reset script and added fixed size and color but for certain things this is quick and easy. Thanks. Flow chart is just playing with Inkscape and spent last night correcting all my .svg's to open with the page centered. Fortunately that was quick.
Gladly accept any criticism, alternate methods


or different ideas. Have learned alot to this point.

So, yes it would end no matter what. Can you point out which line of code tells the script to loop and ask again?

So maybe put the menu and the asking inside a function(), then put the case statements inside an if-then-else. or add a default to the case statement that calls the asking function again.

askfunction () {

}

if { choice is valid } then case {} else askfunction {}

OR

case

*) askfunction()

Suggest you also should put a controlled way to exit the menu choices or else you will have to make some valid choice to exist the script. (or use Ctrl-c).

The second way is most like your existing code and probably simpler.

One thing, I'm no bash guru at all, but though this might put you on the right track. Would need to check on whether bash uses global or local variables.

2 Likes

I'm not even a bash baby rookie. The script does exit if any key stroke is entered that is not in the case even spacebar/enter. Image is my script based on y/n I found and modified to copy a .conf file to a backup folder or vice versa. In terminal shown anything but a 1 or 2 just keeps going down the window. In terminal I pressed 8, x and it waits, exit with ctrl-c for no change.
I am happy lots of reading and slowly progressing.

I may have misread at website link but it seemed to indicate that a wrong choice in the color script would pop up the error message and allow entering a new number? Website is Dec 15 2021, try to stay with later answers versus a lot that have 10 year or so old answers.

1 Like

Well, your Y/N script uses the "select" statement, which has included logic to build and display a menu, then do it again for an invalid entry. "select" already "knows" which are valid and invalid choices because the choices are part of the statement.

Your other script could be reworked with:

select choice in 1 11 2 22 3 33 4 44 5 55; do
    case $choice
         <your code>
     esac
done

or something like that. Otherwise you have to get the code execution point back to the "read" statement manually somehow.

The reason you're exiting is because an invalid choice runs you out the end of the case statement, then bash hits the end of the script and exits.
You probably should terminate each of the case items with break or exit, as your example script does, understanding the difference between the two.

2 Likes

Oh, yes, I think the echo prompt statement about entering again in the color script is just misleading. You could check by cutting/pasting that code directly, save and make it executable, then see if that script really loops back to re-enter and invalid entry. I'll bet it doesn't.

OK, I just tested. It doesn't loop. It exits no matter which you choose.

The echo prompt does not determine the internal logic of the script. Simply a misleading way to phrase things.

2 Likes

It is strange if I copy and paste the code to a terminal it does show message but dumps to command prompt If I run script as website invalid input dumps and closes terminal. Guess the code is wrong on the site or some change in Bash from that date. Downloaded a few Case pages and will have to go deeper. My script was from another site I modified. Worst case delete line 27 and not worry about it :grinning: but will keep plugging.
Thanks

t

1 Like

I'm not following you. I just cut and pasted the sample code into a text file, saved it as color.sh, did chmod 0755, and ./color.sh and it works just like it should: whether you enter a valid or invalid entry the script displays the message in the chosen case statement line to std_out then exits to a command prompt.

What are you meaning by "if I run script as website"? Websites are HTML, not Bash, so I'm clueless on what you mean.

Also, remember that each line in the case statement (terminated by ;:wink: can consist of multiple independent statements (each terminated by ;).

OK, that wasn't supposed to be a smiley. I really meant semicolon close parenthesis.

mean the code on website, saved as .sh file, chmod +x. When executing as script it closes terminal on wrong input. Copying code from script to terminal on wrong input brings up message and goes to command prompt without closing terminal window.

Hi,

If you want me to fix your script, paste it here as text, not as picture :slightly_smiling_face:

In the meantime I have a variant that you might like better :wink:

#!/bin/bash

CurSize()
{
	dconf write /org/mate/desktop/peripherals/mouse/cursor-size "$1"
}
CurTheme()
{
	dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'$1'"
}

while :
do
	clear
	cat<<-MENU
	Cursor Size: 

	[1] 24 px
	[2] 32 px
	[3] 48 px
	[4] 64 px
	[5] 96 px

	Color:

	[W]hite	[B]lack

	Invalid choice exits
MENU

	read -r -s -n1 choice
	case "$choice" in 
	1) CurSize 24				;;
	2) CurSize 32				;;
	3) CurSize 48				;;
	4) CurSize 64				;;
	5) CurSize 96				;;
	W|w) CurTheme 'mate'		;;
	B|b) CurTheme 'mate-black'	;;
	*) exit 					;;
	esac
done
2 Likes

Tried and just blinks. Also tried with my shebang. Also copy paste code into terminal window. Example can type 1,2,3,4,5,w,b and nothing happens. Any other key dumps terminal window. Pardon description.
Here is script, hope I placed correctly.

#! /usr/bin/bash
echo "Choose Cursor Size and Color"
echo "Invalid Choice Exits"
echo " 1 - 24px White"
echo "11 - 24px Black"
echo " 2 - 32px White"
echo "22 - 32px Black"
echo " 3 - 48px White"
echo "33 - 48px Black"
echo " 4 - 64px White"
echo "44 - 64px Black"
echo " 5 - 96px White"
echo "55 - 96px Black"
#lines 4-13 using single digit for white cursor and double digit for black cursor
read choice;
case $choice in
1) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 24 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
11) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 24 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
2) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 32 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
22) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 32 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
3) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 48 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
33) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 48 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
4) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 64 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
44) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 64 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
5) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 96 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
55) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 96 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
*) echo "This choice $choice is not valid. Please choose again.";;
esac

Mainly script works great enter number and both color and size changed. Not critical but is it possible to enter invalid choice and be allowed to enter again without exiting. Now wrong answer dumps terminal. Off topic always on ? found issues dating back to 2018 on various programs. :grinning: Now that I mentioned it seems to go off lots more. :disappointed:

1 Like

Ah yes, my script does not need the use of [enter] after input.
You probably typed a number, which executed and you followed up with enter, which is an invald choice and it therefore exited :joy:

Typing at a regular speed and the thing is indeed gone in a blink. :rofl:

Ofcourse:
Let's prepend your code with: while : ; do , and append: done like this

#! /usr/bin/bash

while :
do
echo "Choose Cursor Size and Color"
echo "Invalid Choice Exits"
echo " 1 - 24px White"
echo "11 - 24px Black"
echo " 2 - 32px White"
echo "22 - 32px Black"
echo " 3 - 48px White"
echo "33 - 48px Black"
echo " 4 - 64px White"
echo "44 - 64px Black"
echo " 5 - 96px White"
echo "55 - 96px Black"

#lines 4-13 using single digit for white cursor and double digit for black cursor
read choice;
case $choice in
1) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 24 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
11) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 24 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
2) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 32 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
22) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 32 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
3) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 48 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
33) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 48 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
4) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 64 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
44) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 64 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
5) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 96 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
55) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 96 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
*) echo "This choice $choice is not valid. Please choose again.";;
esac
done

Your next questions will be:

  1. Q: how do I escape from this endless loop
    A: Add an extra menuchoice that calls exit

  2. Q: can I clear the screen in between iterations.
    A: Let while : ; do be followed by clear (on the next line preferrably) :wink:

Oh, there is also another way if you can't use while : ; do
Append exec "$0" at the end of the script so it will call itself
Fragment:

case $choice in
1) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 24 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
11) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 24 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
2) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 32 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
22) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 32 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
3) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 48 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
33) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 48 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
4) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 64 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
44) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 64 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
5) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 96 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate'";;
55) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 96 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";;
0 ) exit ;;
*) echo "This choice $choice is not valid. Please choose again.";;
esac
exec "$0"
2 Likes

Solved in post 12
Minor other changes had to add exit at end of choices to close terminal window and changed text of invalid entry.


#changed line 3 in my code
echo "Invalid Choice Exits"
#to
echo "Enter 0 to Exit"
#after adding your command which gives an exit choice
0 ) exit ;;

#just showing last lines of script
#had to add
;exit
#to all my choices as in 55) below
#which solved closing terminal window after correct choice
55) dconf write /org/mate/desktop/peripherals/mouse/cursor-size 96 ; dconf write /org/mate/desktop/peripherals/mouse/cursor-theme "'mate-black'";exit ;;
0 ) exit ;;
*) echo "This choice $choice is not valid. Please choose again.";;
esac
#the exec "$0" solved my problem
#doesn't work properly without it
exec "$0"

Thank you and note choices :grinning:

Edit did not get while version working but will look further into it.

2 Likes

Cool! :slight_smile:

Just wondering why you can't get "while" working.

Try this, just to test:

while true
do
    echo "type 0 to exit"
    read -s -n1 choice
    [ "$choice" = "0" ] && exit
done
2 Likes

Stand corrected both ways work, was not fully understanding what I was seeing on screen. Was not looking at cursor as typed say 5 it changed size and if I typed B it changed to black and mind was not registering correctly that had to press enter (just fixated on blinking prompt). Geared to one choice press 5 and enter it changes both. It is more interactive as can see the changes if watching cursor, just changed Invalid choice exits (line) to Press Cursor Size choice, Color choice and then enter to exit
Invalid choice exits window (lines) or something similar.
Thanks again :slightly_frowning_face: now more reading on its contents :grinning: :grinning:

t

2 Likes