Menu with working functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Menu with working functions
# 1  
Old 05-14-2014
Menu with working functions

I am creating a menu currently that makes use of functions to complete each selected option but running into a bit of trouble calling them into action still fairly new with using functions correctly so any insight is appreciated. here is my code so far

Code:
 #!/bin/bash
#Last updated on 05/14/14

########################################

 

Function1 () {  
  grep 'who | sed -e 's/ *.//' | sort -u' addressfile.txt | cut -d: -f2,6 }

 

Function2 () {

Ps -u $usrname | awk ‘{print $2}' }             

 

Function3 () {

cut -d: -f1 addressfile.txt | uniq -d }

 

Function4 () {

awk - f`:` ‘{print $2”,”$3“,”$4”,”$6”,”}`addressfile.txt }


Function5 () {

for x in `ls /export/home`

do

     echo “$x `ls /export/home/$x | wc -l` ” >>violators

 

done

     sort -r -k2 violators | head -5

     rm violators }

 

ans="y"
while [ $ans != "x" ]
do
clear
echo "            Admin Menu"
echo "            ----------"
      echo "A: Get active user phone numbers?" 
      echo "B: Stop user processes?" 
      echo "C: Check for double IDs?" 
      echo "D: Print Users?" 
      echo "E: Warn power users?" 
      echo "Type x to exit" 
      echo -n "Enter a selection: "
read letter
case "$letter" in
a|A) 
echo "Option A: "; Function1;;
echo "Hit ENTER to continue"
read more
;;

b|B)
echo "Option B "  Function2;;
echo "Hit ENTER to continue"
read more
;;

c|C)
echo "Option C " Function3;;
echo "Hit ENTER to continue"
read more
;;

d|D)
echo "Option D " Function4;;
echo "Hit ENTER to continue"
read more
;;

e|E)
echo "Option E" Function5;;
echo "Hit ENTER to continue"
read more
;;

x)
exit
;;

*) echo " You did not enter valid command "
echo "Hit ENTER to continue"
read more
esac
done

# 2  
Old 05-14-2014
Is this a homework assignment?
# 3  
Old 05-14-2014
Nope its a practice exercise to get my feet wet. Next semester i'm taking a unix class so I've been doing some soft scripting to prepare myself.
# 4  
Old 05-14-2014
You have a few issues, first:

Code:
Function1 () {  
  grep 'who | sed -e 's/ *.//' | sort -u' addressfile.txt | cut -d: -f2,6 }

the Close } need to be on a separate line. All function definitions have this issue

Code:
a|A) 
echo "Option A: "; Function1;;
echo "Hit ENTER to continue"
read more
;;

You only want one ;; for each case. You've done this on all the case tests

Code:
b|B)
echo "Option B "  Function2;;
echo "Hit ENTER to continue"
read more
;;

You forgot a semicolon before Function2 call - this just passes the string "Function2" to echo. You've done this on the Function 2,3,4 and 5 calls.
# 5  
Old 05-15-2014
In addition to what has already been said by others, here are a few points to
consider.
  1. #!/bin/bash must appear starting in column 1 if you intend for the system to use this line to select the shell to be used to run this script.
  2. In a function definition command, there should not be a space between the function name and the ().
  3. Develop a coding style that makes it easy to see the structure of your code using indentation.
  4. There are three different types of quotes used in shell programming: double-quotes ("), single-quotes ('), and back-quotes (`). You need to review the bash man page and learn when to use each type of quotes. (Also note that some editors will give you pretty printing opening and closing single- and double-quotes. They can't be used for quoting purposes in the shell command language.)
  5. There is no need to set up a variable to terminate a loop when nothing inside the loop ever changes the value of that variable.
  6. When you perform the same actino as the last step in every selection in a case statement; move that code after the case statement and only do it once.
  7. Although echo -n may work on your system to print a prompt with no trailing <newline> character, any use of echo with options or with any argument that contains a backslash character may behave differently on different systems. If you want your code to be portable to other UNIX and Linux systems, use printf instead of echo for these cases.
  8. Although back quotes can be used for command substitutions (i.e., `command`), that form is deprecated. You should use the form $(command) instead.
  9. Choose function names that give some indication of what that function is supposed to do.

The following restructuring of your code sample covers some of these issues, but does not fix the code in the functions even though it does correct some of the uses of various types of quotes. (I also modified the BRE in the sed substitute command to be something more likely to do what I think you're trying to do.)
Code:
#!/bin/bash
#Last updated on 05/14/14
########################################
Function1() {
        grep $(who | sed -e 's/ .*//' | sort -u) addressfile.txt | cut -d: -f2,6
}

Function2() {
        ps -u $usrname | awk '{print $2}'
}

Function3() {
        cut -d: -f1 addressfile.txt | uniq -d
}

Function4() {
        awk - f':' '{print $2","$3","$4","$6}' addressfile.txt
}

Function5() {
        for x in $(ls /export/home)
        do
                echo "$x $(ls /export/home/$x | wc -l)" >> violators
        done
        sort -r -k2 violators | head -5
        rm violators
}

while true
do
        clear
        echo "            Admin Menu"
        echo "            ----------"
        echo "A: Get active user phone numbers?"
        echo "B: Stop user processes?"
        echo "C: Check for double IDs?"
        echo "D: Print Users?"
        echo "E: Warn power users?"
        echo "x: Exit."
        printf "Enter a selection: "

        read letter
        case "$letter" in
        (a|A)   echo "Option A:"
                Function1;;

        (b|B)   echo "Option B:"
                Function2;;

        (c|C)   echo "Option C:"
                Function3;;

        (d|D)   echo "Option D:"
                Function4;;

        (e|E)   echo "Option E:"
                Function5;;

        (x)     exit;;

        (*)     echo "You did not enter valid command.";;
        esac
        printf "Hit ENTER to continue: "
        read more
done

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. UNIX for Dummies Questions & Answers

Working of exec family of functions

Hi, I am studying exec family of functions.Its man page says,it replaces the current process image with a new process image. If it replaces the binary,then after returning back,how does it get the previous parameters of the process which called exec?As replacing process image means replacing... (5 Replies)
Discussion started by: Radha.Krishna
5 Replies

3. Programming

Working of exec family of functions

Hi, I am studying exec family of functions.Its man page says,it replaces the current process image with a new process image. If it replaces the binary,then after returning back,how does it get the previous parameters of the process which called exec?As replacing process image means replacing all... (1 Reply)
Discussion started by: Radha.Krishna
1 Replies

4. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

5. Shell Programming and Scripting

Need help in create menu with 3 sub menu using the case command

hi all i am a newbie to this is there any examples on creating a main menu with 3 sub menu main menu -> option a , b and c a menu -> option 1 ,2 and 3 b menu -> option 1 ,2 c menu -> option 1 ,2 i am getting headache as my code kept getting unexpected EOF ---------- Post... (0 Replies)
Discussion started by: chercm
0 Replies

6. Shell Programming and Scripting

Menu with sub-menu options

Hi! I have created on script which is working fine with menu options and with a sub-menu. I want to enhance it by using sub-options under menu options. Like. option 1) will give the list only. option 1.1) should give the option to user to choose one file, whose content user wanna see. ... (3 Replies)
Discussion started by: sukhdip
3 Replies

7. Shell Programming and Scripting

Menu in Menu script issue

Problem: I am trying to create a menu in a menu script and I am running into an issue with the calculator portion of the script. I am first presented with the ==Options Menu== which all 5 options working correctly. Now comes the fun part. I select option 1 which takes me to my ==Calculator... (1 Reply)
Discussion started by: iDdraig
1 Replies

8. UNIX for Dummies Questions & Answers

functions not working

I have the following functions but when I run SelectQmgr from a Menu Selection it doesn't do anything. SelectQmgr () { qmgrlist=`ls /var/mqm/qmgrs/ | grep -v @SYSTEM` qmgrcount=`ls /var/mqm/qmgrs/ | grep -v @SYSTEM | wc -l` if then echo "$qmgrlist QManager will be used " ... (7 Replies)
Discussion started by: darthur
7 Replies
Login or Register to Ask a Question