LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2009, 03:44 PM   #1
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Rep: Reputation: 45
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.

Last edited by manwithaplan; 02-23-2009 at 03:55 PM.
 
Old 02-23-2009, 04:00 PM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
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.

Last edited by H_TeXMeX_H; 02-23-2009 at 04:04 PM.
 
Old 02-23-2009, 04:21 PM   #3
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
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
 
Old 02-23-2009, 04:46 PM   #4
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
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.

Last edited by manwithaplan; 02-23-2009 at 05:08 PM.
 
Old 02-23-2009, 05:26 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
 
Old 02-23-2009, 05:49 PM   #6
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
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

Last edited by manwithaplan; 02-23-2009 at 06:04 PM.
 
Old 02-23-2009, 06:50 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by manwithaplan View Post
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.
 
Old 02-23-2009, 11:02 PM   #8
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Thanks for the help.... I figured out how to break the loop and continue... Using the last posters tutorial link..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie Bash script help Mikecore Programming 3 05-13-2008 12:23 PM
Bash Script newbie Konane Programming 9 11-18-2007 01:46 PM
i need help with some newbie bash commands Alexander.s Programming 14 12-24-2004 03:33 PM
Newbie Bash Problem that_girl Programming 10 08-08-2002 09:58 PM
newbie to bash scripts kballing Programming 1 07-21-2002 12:03 AM


All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration