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
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.
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.
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.
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.
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 but will keep plugging.
Thanks
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 ; can consist of multiple independent statements (each terminated by ;).
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.
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.
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. Now that I mentioned it seems to go off lots more.
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
Typing at a regular speed and the thing is indeed gone in a blink.
Ofcourse:
Let's prepend your code with: while : ; do , and append: done like this
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"
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 now more reading on its contents