How to retrieve "case "statement return value ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to retrieve "case "statement return value ?
# 1  
Old 08-07-2018
How to retrieve "case "statement return value ?

How do we retrieve case statement return value at point indicated in the attached snippet



Code:
      case "$FUN" in
          1\ *) do_change_pass ;;
          2\ *) do_network_menu ;;
          3\ *) do_boot_menu ;;
          4\ *) do_internationalisation_menu ;;
          5\ *) do_ssh ;;
          6\ *) do_pixdub ;;
          8\ *) do_update ;;
          9\ *) do_about ;;
          *) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 $
        esac || 

GET CASE RETURN VALUE HERE 

whiptail --msgbox "There was an error running option $FUN" 20 6$
      fi


Thanks
Moderator's Comments:
Mod Comment Please use CODE tags NOT ICODE tags for multiline code
# 2  
Old 08-07-2018
Can you elaborate on return statement from case ?

Do you wish to catch the value of '$FUN' after the case is completed ?
Or from the commands or functions ran such as do_change_pass
Perhaps a *) needs to be processed ?


Regards
Peasant.
# 3  
Old 08-07-2018
I do not understand your questions.

case statement and if statement will return a value other that zero when they fail.
The values are numeric and indicate error codes - pretty normal in many languages.

For example in c you write int function_name (.... return X } Such syntax defines "int " as being passed from the function to the calling code.



Similar syntax must be in bash and that is what I am looking for.
# 4  
Old 08-07-2018
Quote:
Originally Posted by annacreek
I do not understand your questions.

case statement and if statement will return a value other that zero when they fail.
The values are numeric and indicate error codes - pretty normal in many languages.

For example in c you write int function_name (.... return X } Such syntax defines "int " as being passed from the function to the calling code.



Similar syntax must be in bash and that is what I am looking for.
case is not a function, but rather a compound command (man bash for reference) just like while, select, for, if while etc...
There're no return value(s)...
# 5  
Old 08-07-2018
So how does the error message after esac || manages to execute ?

I am too new to get into terminology discussion , but per doc "case" returns value, it does not matter (to me) how it is called.
# 6  
Old 08-07-2018
Code:
       case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
              A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as  for  path‐
              name  expansion  (see  Pathname  Expansion  below).   The word is expanded using tilde expansion, parameter and variable expansion,
              arithmetic expansion, command substitution, process substitution and quote removal.  Each pattern examined is expanded using  tilde
              expansion,  parameter and variable expansion, arithmetic expansion, command substitution, and process substitution.  If the nocase‐
              match shell option is enabled, the match is performed without regard to the case of alphabetic characters.  When a match is  found,
              the corresponding list is executed.  If the ;; operator is used, no subsequent matches are attempted after the first pattern match.
              Using ;& in place of ;; causes execution to continue with the list associated with the next set of patterns.  Using ;;& in place of
              ;;  causes the shell to test the next pattern list in the statement, if any, and execute any associated list on a successful match.
              The exit status is zero if no pattern matches.  Otherwise, it is the exit status of the last command executed in list.

You're right. you can check $? for the returned status:
Code:
case
....
esac
if [ "${?}" -ne 0 ]; then
   echo NONzero
else
   echo zero
fi

# 7  
Old 08-08-2018
FYI update

There are bash system special variables ($x) and this one is called


$? - The exit status of the most recently run process
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Advanced & Expert Users

Should I say "field 8" or "column 8" in this case?

I saw some recent posts where I thought the terms "field" and "column" were being misused. I work with data a lot, and have my opinions. I'm wondering if those opinions are correct. ***** Rows seem clear - I don't think there is any controversy about what a row is, either for database or text... (10 Replies)
Discussion started by: hanson44
10 Replies

3. Shell Programming and Scripting

"if" statement based off "grep"

Hello, I am somewhat new to Linux/Unix. I am currently working on a shell script that is suppose to cat a file, grep the same file for a certain line, if that line is found save the file in a different location, else remove the file. This is a rough example of what I want. $Dating = False... (13 Replies)
Discussion started by: Amzerik
13 Replies

4. UNIX for Dummies Questions & Answers

What is the meaning of "-s" option in "if" statement?

Hi Guys, I'm sorry but I can't find answer for this, what is the meaning of -s option in "if" statement on unix scipting. Please see sample below: opath=/home/output for i in N1 N2 N3 N4 do echo $i if then grep $i $opath/N5_CRAI > $opath/N5_$i.crai chmod 777 $opath/N5_$i.crai ... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

What "-a" operator means in "if" statement

Hi I am trying to figure out what the following line does, I work in ksh88: ] && LIST="$big $LIST" Not sure what "-a" means in that case. Thanks a lot for any advice -A (1 Reply)
Discussion started by: aoussenko
1 Replies

7. Shell Programming and Scripting

Retrieve RAM memory size from "top" command output

Hi, I am trying to get the system RAM size from "top" command's output by the following but it is not working. top | sed "s/^Mem.**\(*\), *//" (10 Replies)
Discussion started by: royalibrahim
10 Replies

8. Red Hat

"if" and "then" statement is not working in RedHat

Dear experts, I'm trying to write a script to calculate the usage of Log Archive in a directory, so if it gets to a point where the directory size is 60%, then send out an FYI.. email. So if then it reaches to 80%, move the logs from that directory. I have written the script as follow but... (10 Replies)
Discussion started by: Afi_Linux
10 Replies

9. Shell Programming and Scripting

ksh "case" statement question

Hi I have the following case statement: case $larg in *_* ) a=${larg%_*}; b=${larg#*_}; ;; *^* ) a=${larg%^*}; b=${larg#*^}; ;; esac I cannot figure out what *_* and *^* stand for... Also what a=${larg%_*}; b=${larg#*_}; and a=${larg%^*}; b=${larg#*^}; ... (1 Reply)
Discussion started by: aoussenko
1 Replies

10. Shell Programming and Scripting

need to retrieve data between the first occurance of "_" and the extension.

hi all, i have a file that contains the following kind of data codeexpert_package_module1.html codeexpert_package_module2.html codeexpert_package_module3_revision2.html and it goes on .. i need to get the following data from it package_module1 package_module2 i know basename... (4 Replies)
Discussion started by: sais
4 Replies
Login or Register to Ask a Question