LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-26-2002, 02:16 AM   #1
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Exclamation bash shell program help


I am writting my 1st shell script and it has problems exiting correctly and the else statement is suppose to display an error message if the menu choice entered is invalid but it does not. I have cut and pasted it below, please help:

#!/bin/bash
# MENU DRIVEN BASH SCRIPT CREATED BY ERIC BISHOP (ebishop@cox.net) FOR GWCC LINUX CLASS
# CREATED ON NOVEMBER 25th 2002
# ISSUED UNDER THE GNU GPL (GNU GPL KICKS ASS!!)
# I WOULD LIKE TO GIVE CREDIT TO THE LINUX DOCUMENTATION PROJECT
# BECAUSE THEY HAD SAMPLE CODE FOR ME TO WORK OFF OF!
# (http://en.tldp.org/HOWTO/Bash-Prog-I...O-9.html#ss9.1)
RELEASE=/etc/redhat-release
clear
echo
echo "Hello $USER, Welcome to the CONTROL MENU"
echo "The Date & Time is:"
date
echo
echo "Please enter a selection number below and press the ENTER key"
echo
OPTIONS="Free-Disk-Space View-Kernel-Version View-XFree86-Version View-Distribution-Version View-System-Uptime-Status Test-Network-Card Quit"
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]; then
echo
echo "THANKS FOR USING MY SCRIPT"
echo "FEEL FREE TO MODIFY IT TO SUIT YOUR NEEDS"
echo
echo "TO EXIT, YOU MAY NEED TO PRESS CTRL+Z"
exit
fi
if [ "$opt" = "View-Kernel-Version" ]; then
echo
echo "This System is powered by Kernel Version:"
echo
uname -r
echo
echo "Press the ENTER key to go back to the CONTROL MENU"
read
echo
./assignment.sh
fi
if [ "$opt" = "View-Distribution-Version" ]; then
echo
echo "This System Distribution is:"
echo
cat $RELEASE
echo
echo "Press the ENTER key to go back to the CONTROL MENU"
read
echo
./assignment.sh
fi
if [ "$opt" = "Test-Network-Card" ]; then
echo
echo "Testing results of your network card is:"
echo
ping 127.0.0.1 -c 5
echo
echo "Press the ENTER key to go back to the CONTROL MENU"
read
echo
./assignment.sh
fi
if [ "$opt" = "View-System-Uptime-Status" ]; then
echo
echo "This System Has been running for:"
echo
uptime
echo
echo "Press the ENTER key to go back to the CONTROL MENU"
read
echo
./assignment.sh
fi
if [ "$opt" = "View-XFree86-Version" ]; then
echo
echo "This System has the following XFree86 Version Installed:"
echo "(SHOWS ALL CORRESPONDING XFree86 FILES!)"
echo
rpm -qa |grep XFree86
echo
echo "Press the ENTER key to go back to the CONTROL MENU"
read
echo
./assignment.sh
fi
if [ "$opt" = "Free-Disk-Space" ]; then
echo
echo "The free space on this systems hard disks is:"
echo
df -h
echo
echo "Press the ENTER key to go back to the CONTROL MENU"
read
echo
./assignment.sh
else
clear
echo INVALID SELECTION, PLEASE TRY AGAIN
./assignment.sh
fi
done
 
Old 11-26-2002, 04:55 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
This seems to be homework, which should not be posted on this site. keep reading the howto's and lecture notes and you'll get there.
 
Old 11-26-2002, 10:39 AM   #3
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
Reply to the moderator

Yes it is homework, my instructor told us to use the web as our resource in coding our script because our book is very limited in shell scripting.

I have created this script so far, I spent a few hours last night troubleshooting it, I got some of the bugs out of this but I am having a hard time getting the other 2 out.

I am not telling you that I have an assignment due and could you please do it for me, I have a couple questions, I showed you the script that I wrote, and just asked for some HELP. Read it, HELP HELP HELP HELP, not please do my assignment.

I surely did not expect a moderator to be such a rude unwilling to help person.

Isn't this site called linuxQUESTIONS.com, well I guess I might be the stupid one here, but if I was a moderator at a questions based site, I would answer questions, not list sarcastic remarks.
 
Old 11-26-2002, 11:01 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
yes, but WE are not the web. WE are people. the web is the VAST amount of information that's already out there that you should be using to teach yourself. you might as well just go to your lecturer and get HIM to tell you the answer. I could fix your script, but you wouldn't learn nearly as much from it, than if you were to work it out using existing resources.
 
Old 11-26-2002, 11:13 AM   #5
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
reply #2

all my assignment was supposed to do was clear the screen and greet the user ($USER). He said if we wanted to add more, we would get extra credit.
I have an A in his class as it is, so I don't need the extra credit. I am doing the extra work because I am a geek. LINUX keeps me up til 3 in the morning.
I think this is cool and I wanted to learn more. Below is the url of the howto I am using, as you can see section 13 is troubleshooting and it has no help to offer at all:

http://en.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

if you can suggest something better let me know, i dont understand your point on the matter, if you told me what lines were bad and why and a possible solution, IT WOULD BE A LEARNING EXPERIENCE. I am not anywhere near complete. I would say that it is at 25% of where I want it to end up being.

My instructor issued it last night 11/25, and it is not due until December 9th.
I am not waiting to the last minute, the first thing I did when I got home was feeding my pets, the second was starting this script. I am excited in this stuff.

If you have any better resources to recommend, that would be great.
 
Old 11-26-2002, 03:51 PM   #6
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
You clear the screen at the beginning of your script, and to get back to the main menu you execute your script from within your script. Therefore as soon as you've printed your error message, your script is run again and the screen is cleared, immediately obscuring the message.

Alex

Last edited by llama_meme; 11-26-2002 at 03:55 PM.
 
Old 11-27-2002, 12:41 AM   #7
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
The llama was correct. Your solution to call the script again as your looping construct is not a good one. Use the select statment to do the looping for you. The "Press ENTER... " after each option has got to go. (a good loop and some structure will fix this) Also your script is geared toward RedHat and RedHat wannabies like Mandrake. Some of the options will not work on other distros. And the "test network card" option only pings the loopback device, you can do that without a network card installed.

What you can try for extra credit is to parse out the results of uptime and present them in a table like:

Uptime: 65 days 3:34
Users: 1
Load Average: 0.03 0.04 0.02

I've tinkered with your script a bit and came up with this:
Code:
#!/bin/bash
# MENU DRIVEN BASH SCRIPT CREATED BY ERIC BISHOP 
# (ebishop@cox.net) FOR GWCC LINUX CLASS
# CREATED ON NOVEMBER 25th 2002
# ISSUED UNDER THE GNU GPL (GNU GPL KICKS ASS!!)
# I WOULD LIKE TO GIVE CREDIT TO THE LINUX DOCUMENTATION 
# PROJECT
# BECAUSE THEY HAD SAMPLE CODE FOR ME TO WORK OFF OF!
# (http://en.tldp.org/HOWTO/Bash-Prog-...TO-9.html#ss9.1)

RELEASE=/etc/redhat-release
OPTIONS="Free-Disk-Space \
         View-Kernel-Version \
         View-XFree86-Version \
         View-Distribution-Version \
         View-System-Uptime-Status \
         Test-Network-Card \
         Quit"

header()
{
   clear
   echo
   echo "Hello $USER, Welcome to the CONTROL MENU"
   echo "The Date & Time is:"
   date
   echo
   echo "Please enter a selection number below and press the ENTER key"
   echo
}

header

select opt in $OPTIONS; do

   if [ "$opt" = "Quit" ]; then
      break 
   elif [ "$opt" = "View-Kernel-Version" ]; then
      echo
      echo "This System is powered by Kernel Version:"
      echo
      uname -r
      echo
   elif [ "$opt" = "View-Distribution-Version" ]; then
      echo
      echo "This System Distribution is:"
      echo
      cat $RELEASE
   elif [ "$opt" = "Test-Network-Card" ]; then
      echo
      echo "Testing results of your network card is:"
      echo
      ping 127.0.0.1 -c 5
   elif [ "$opt" = "View-System-Uptime-Status" ]; then
      echo
      echo "This System Has been running for:"
      echo
      uptime
   elif [ "$opt" = "View-XFree86-Version" ]; then
      echo
      echo "This System has the following XFree86 Version Installed:"
      echo "(SHOWS ALL CORRESPONDING XFree86 FILES!)"
      echo
      rpm -qa |grep XFree86
   elif [ "$opt" = "Free-Disk-Space" ]; then
      echo
      echo "The free space on this systems hard disks is:"
      echo
      df -h
   else
      echo
      echo INVALID SELECTION, PLEASE TRY AGAIN
   fi

   echo
   echo "Press the ENTER key to go back to the CONTROL MENU"
   read
   echo
   header

done

echo "THANKS FOR USING MY SCRIPT"
echo "FEEL FREE TO MODIFY IT TO SUIT YOUR NEEDS"
echo 
echo "TO EXIT, YOU MAY NEED TO PRESS CTRL+Z"
exit

Last edited by crabboy; 11-27-2002 at 12:43 AM.
 
Old 11-27-2002, 01:05 AM   #8
embsupafly
Member
 
Registered: Nov 2002
Location: ARIZONA
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
re

Thank you so much crabboy!

That was more than I expected, and I am going to modify and add to it so I learn more. Can you kind of explain what you did to exactly and why. I mean I can see the changes you made but, they are new code to me.

Or could you recommend a good online resource for learning more, I have been through the TLDP site's bash script howtos, but they did not seem to help.

If there is anything I can do for you please let me know.
 
  


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
BASH Shell program Read a configuration File minil Programming 10 01-17-2005 05:37 AM
converting from a bash program to a c program Linh Programming 5 08-01-2003 06:47 PM
Bash shell triplem Linux - Software 1 04-14-2003 09:01 AM
Simple Bash Shell Program frankblack Programming 2 02-15-2003 12:59 AM
Help!!!!! Bash Shell???? DarkHorse Linux - Newbie 7 06-04-2001 01:13 PM


All times are GMT -5. The time now is 09:01 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration