Bash case error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash case error
# 1  
Old 08-05-2018
Bash case error

Please - this is my FIRST ever attempt on scrip and I just want simple answer if possible.

I am modifying existing and working script.

I just want to and more to it so I started with simple insertion of working code into menu.

I am doing OK, but I cannot figure out why I am executing this line of code

Code:
 esac || whiptail --msgbox "DEBUG ERROR There was an error running option

I understand the "end of case" , but || (or?) does not makes sense to me for now.

I am enclosing PART what I feel is an "problem area"

Code:
    RET=$?
    if [ $RET -eq 1 ]; then
      do_finish
    elif [ $RET -eq 0 ]; then
      if is_pi ; then
        case "$FUN" in
          1\ *) do_change_pass ;;
          2\ *) do_network_menu ;;
          3\ *) do_boot_menu ;;
          4\ *) do_internationalisation_menu ;;
          5\ *) do_interface_menu ;;
          6\ *) do_overclock ;;
          7\ *) do_advanced_menu ;;
          8\ *) do_update ;;
          9\ *) do_about ;;
          10\ *) do_about ;;
          *) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
        esac || whiptail --msgbox "DEBUG ERROR There was an error running option $FU$
      else
        case "$FUN" in
          1\ *) do_change_pass ;;
          2\ *) do_network_menu ;;
          3\ *) do_boot_menu ;;
          4\ *) do_internationalisation_menu ;;
          5\ *) do_ssh ;;

I am coding in C/C++ and know about switch / case construct , but I do not understand why duplicating item 9 and inserting it as item 10 is causing this error. It should just run same function as item 9 does.


The "menu" displays OK , but when I select the item 10 I get the DEBUG ERROR message. Is it possible I am getting the "programmer error ..." also but it is getting overridden by the next message?
Help will be much appreciated.
# 2  
Old 08-05-2018
Welcome to the forum.



The || is the "control operator" in a bash "OR list". man bash:

Quote:
An OR list has the form

command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status.
So the construct is there to catch errors of case dealing with the various FUNs.

Be aware that $FU$ will expand to just $ (unless FU is defined somewhere in your script).


Try moving the second msgbox to another position to eliminate your "override suspicion"


Does your case work for other inputs, e.g. 3 , or 9 ? What exactly do you enter in either case, and what in the 10 case?
Are you aware that the shown snippet only has some improvement opportunities?
# 3  
Old 08-05-2018
Thanks for reply.
(URL deleted - not allowed ) It never occurred to me that case returns value.

I'll follow you suggestions after I post this.

I am modifying "standard" configuration file to make sense of WiFi errors I am getting.

I am not too concerned if the file is perfect, I just need to make my modifications for now.



Would it be feasible to implement something like this - in pseudocode ?
Code:
...
esac || if debug 

      print error code 

      else
      just print the original message

When I gain some more experience I would like to make the "|| command " as a function passing the error number (?) and the variable message to it.



Thanks
# 4  
Old 08-05-2018
Quote:
Originally Posted by annacreek
Thanks for reply.
(URL deleted - not allowed ) It never occurred to me that case returns value.

I'll follow you suggestions after I post this.

I am modifying "standard" configuration file to make sense of WiFi errors I am getting.

I am not too concerned if the file is perfect, I just need to make my modifications for now.



Would it be feasible to implement something like this - in pseudocode ?
...
esac || if debug

print error code

else
just print the original message



When I gain some more experience I would like to make the "|| command " as a function passing the error number (?) and the variable message to it.



Thanks

Moderator's Comments:
Mod Comment User Code Tags Around Code. It's in the Rule and Best for All
# 5  
Old 08-05-2018
What code ?

I did not post any such thing.
# 6  
Old 08-05-2018
If you are only seeing the DEBUG ERROR message box when you run do_about, it means that do_about is exiting with a non-zero exit code. The exit status of a case statement is the exit status of the last command executed in the command list for the matched pattern or zero if no pattern matched.

The text you included in post #3 in this thread (that Neo edited for you to contain CODE tags) did indeed include the shell script pseudo-code which without CODE tags displays as:
...
esac || if debug

print error code

else
just print the original message

With CODE tags that pseudo-code appears as:
Code:
...
esac || if debug 

      print error code 

      else
      just print the original message

Please use CODE tags when displaying sample input, sample output, and code (or pseudo-code) segments.
# 7  
Old 08-05-2018
OK, I get it.

I have been thru this before on another forum and will risk repeating my concerns here.



Especially when I do not see HOW to copy the tagged code block.

But it may be there and I just do not readily see it. ( Another favorite of mine - "users fault')



WHY use code tags if mouse "cut and paste " works ?

Is it one of those artifacts "just because "?

Perhaps there are "savings" with code tags.

Personally I like KISS principle better.

It almost seems that code tags are used so post can be scrutinized for format , which is much easier than analyzing them for contents.

(Sorry) for venting, but I grew up when peoples ideas written "on napkin" were acceptable...


Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Special case to skip function in bash menu

In the bash menu below if the variant that is inputted is in the format NM_004004.3:c.274G>T the below works perfectly. My question is if the variant inputted isNM_004004.3:-c.274G>T or NM_004004.3:+c.274G>T then the code as is will throw an error due to a biological issue. Is it possible to to... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

3. Shell Programming and Scripting

Bash Case Issues..

Hi, I'm having some trouble with using "case...esac" in Bash. I've googled it and am stuggling to understand the syntax and how to do certain things. Firstly, I want to be able to choose a case based on a variable number For example, I have in my code a place where a user can enter... (2 Replies)
Discussion started by: Ste_Moore01
2 Replies

4. Shell Programming and Scripting

Bash case Statement and Using Line Anchors?

Hello All, I am writing a script that is to be placed on multiple servers, and of course I've started running into some compatibility issues for certain shell commands. The code below worked just fine on most of my machines except for a couple. Here I had 4 separate lines in my script that... (3 Replies)
Discussion started by: mrm5102
3 Replies

5. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

Can you use logical operators in a case statement (bash)?

I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

7. UNIX for Dummies Questions & Answers

How to change Case of a string(BASH Scripting)?

Is there any inbuilt functionality in Unix shell script so that i can able to convert lower case string input to an upper case? I dont want to use high level languages like java,python or perl for doing the job. (2 Replies)
Discussion started by: pinga123
2 Replies

8. Shell Programming and Scripting

bash case statement output help

greetings, I have a script that is taking input like this: a b c d aa bb aaa bbb ccc ddd and formating it to be like this: a b c d aa bb aaa bbb ccc ddd (4 Replies)
Discussion started by: adambot
4 Replies

9. Shell Programming and Scripting

Example of switch case in Bash

can anyone post a sample code for a switch case in shell (1 Reply)
Discussion started by: sumit the cool
1 Replies

10. Shell Programming and Scripting

bash regex =~ case insensetive, possible?

It can get very annoying that bash regex =~ is case-sensetive, is there a way to set it to be case-insensetive? if ]; then echo match else echo no match fi (8 Replies)
Discussion started by: TehOne
8 Replies
Login or Register to Ask a Question