LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newbie bash help (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-bash-help-706931/)

manwithaplan 02-23-2009 02:44 PM

Newbie bash help
 
I'm writing a script for an install and need help with a menu. I'm really new with scripting and I'm still learning syntax and commands.

I'd like for this menu to return after I've edited a particular config file. And also when pressing 0 it will continue the script without exiting bash.

A loop seems in order here still confused with "Do's and While's", and maybe some suggestions or examples on cleaning this up. Thx
Code:

#Time to Edit .conf files ...
echo "Time to edit your files for install and your conf.d files"
echo
echo "1-  /etc/make.conf"
echo "2-  /etc/rc.conf"
echo "3-  /etc/conf.d/net"
echo "4-  /etc/conf.d/hwclock"
echo "5-  /etc/conf.d/modules"
echo "0-  Press to continue install"
echo
echo "Enter selection (0-5), and then press enter"
read CONF

if [ $CONF -eq 1 ]
then
nano /mnt/funtoo/etc/make.conf

elif [ $CONF -eq 2 ]
then
nano /mnt/funtoo/etc/rc.conf

elif [ $CONF -eq 3 ]
then
nano /mnt/funtoo/etc/conf.d/net

elif [ $CONF -eq 4 ]
then
nano /mnt/funtoo/etc/conf.d/hwclock

elif [ $CONF -eq 5 ]
then
nano /mnt/funtoo/etc/conf.d/modules

elif [ $CONF -eq 0 ]
then
echo "Press any key to continue install"
read key
  ????? #This is where I dont know how to continue, Maybe an if & else?


fi

I have similar menus like this, but I never have had to return back to them, or continue on.

H_TeXMeX_H 02-23-2009 03:00 PM

Perhaps you should look into the case statement, this is the absolute perfect place to use one ...
http://www.grymoire.com/Unix/Sh.html#uh-82

So in your case start with:

Code:

case $CONF in
    1)
        nano /mnt/funtoo/etc/make.conf
    break;;
    2)
    ...
   
    0)
    break;;
esac

break breaks out of the loop and continues whatever might be below.

manwithaplan 02-23-2009 03:21 PM

Absolutely perfect... this will help with another menu I'm working on... Though I might have another question... Even though this command is straight forward.

Thx

manwithaplan 02-23-2009 03:46 PM

Would something like this work... Or do i need to 'break' after each edit, for it to return to the menu? I tried just this bit of code and I can edit my files but it breaks back into prompt after exiting nano. I'd like it to return back to the original menu to continue edit


Code:

echo "Time to edit your files for install and your conf.d files"
echo
echo "1-  /etc/make.conf"
echo "2-  /etc/rc.conf"
echo "3-  /etc/conf.d/net"
echo "4-  /etc/conf.d/hwclock"
echo "5-  /etc/conf.d/modules"
echo "0-  To exit & continue"
echo
echo "Enter selection (0-5), and then press enter"
read CONF
case $CONF in
  1)
      nano /mnt/funtoo/etc/make.conf ;;
  2) 
      nano /mnt/funtoo/etc/rc.conf ;;
  3) 
      nano /mnt/funtoo/conf.d/net ;;
  4)
      nano /mnt/funtoo/conf.d/hwclock ;;
  5)
      nano /mnt/funtoo/conf.d/modules ;;
  0)
      break;;
esac


Thanks again for that website.. easier to understand then this other site I'm using.

colucix 02-23-2009 04:26 PM

Nope, you don't need the break statement, since break is meant to exit from within a for, while, until, or select loop. See man bashbuiltins for more details. H_TeXMeX_H maybe put it thinking at C language, where indeed the break statement is mandatory.

If you want to return to the original menu after editing, you have to envelope the code within a while loop, testing for the value of CONF and executing the loop until the user selects 0 (exit). For example:
Code:

#!/bin/bash
CONF=11      # initial dummy value
while [ $CONF -ne 0 ]
do
  echo "Time to edit your files for install and your conf.d files"
  echo
  echo "1-  /etc/make.conf"
  echo "2-  /etc/rc.conf"
  echo "3-  /etc/conf.d/net"
  echo "4-  /etc/conf.d/hwclock"
  echo "5-  /etc/conf.d/modules"
  echo "0-  To exit"
  echo
  read -p "Enter selection (0-5), and then press enter: " CONF
  case $CONF in
    1)
      nano /mnt/funtoo/etc/make.conf
      echo ;;
    2) 
      nano /mnt/funtoo/etc/rc.conf
      echo ;;
    3) 
      nano /mnt/funtoo/conf.d/net
      echo ;;
    4)
      nano /mnt/funtoo/conf.d/hwclock
      echo ;;
    5)
      nano /mnt/funtoo/conf.d/modules
      echo ;;
    0)
      ;;
    *)
      echo
      echo "Not a valid selection, please try again!"
      echo ;;
  esac
done


manwithaplan 02-23-2009 04:49 PM

So... Nice.... I was experimenting with a the while loop before you posted. This example will help immensely with my other menus. Quick question on syntax?

And if I exclude then "done" at the end of the loop, will it continue on... This is just an excerpt of an overall script. I still need the program to continue after I press 0.

Code:

while [ $CONF -ne 0 ]
Just wanted an explanation on those values (-ne 0). For any other while loops I'll be doing.
FYI I get this for the man you posted:
Quote:

No manual entry for bashbuiltins
And earlier in the script you declared the variable CONF=11. Is this because of the menu, and the amount of selections available?

Forgive any novice ignorance...


BTW, this code was most helpful... learned alot from this.

Code:

read -p "Enter selection (0-5), and then press enter: " CONF

colucix 02-23-2009 05:50 PM

Quote:

Originally Posted by manwithaplan (Post 3454938)
And if I exclude then "done" at the end of the loop, will it continue on... This is just an excerpt of an overall script. I still need the program to continue after I press 0.

The done is mandatory to terminate a loop. If you want to perform other actions until the user presses 0, you have to include them within the loop. In my example the loop serves to repeat the display of the menu and let the user do another choice.
Quote:

Just wanted an explanation on those values (-ne 0). For any other while loops I'll be doing.
-ne means not equal. The loop continues while the value of CONF is not equal to 0.
Quote:

FYI I get this for the man you posted: No manual entry for bashbuiltins
Try man break. Maybe on your system that man page has a slightly different name, e.g. bash_builtins. Anyway you will see that the man page is the same listing all the shell built-ins.
Quote:

And earlier in the script you declared the variable CONF=11. Is this because of the menu, and the amount of selections available?
I could choose any value. It is just an initial value, mandatory if you do not want to get a syntax error in the while statement. If you don't initialize the variable, the while statement is interpreted from the shell as
Code:

while [  -ne 0 ]
which is an error, since it misses the left hand side of the comparison.

manwithaplan 02-23-2009 10:02 PM

Thanks for the help.... I figured out how to break the loop and continue... Using the last posters tutorial link..


All times are GMT -5. The time now is 11:31 PM.