Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2005, 8:28 AM   #1
trufla
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 trufla is on a distinguished road
Simple menu script

Hi! Just started the old bash programming...I have got a bit stuck.

I wanted to create a menu where by a user can type the name of a file they wish to view.

The problem I have is that the file doesn't seem to either get saved to the variable, isn't retrieved from the variable, or there is a problem with the file's location. The file I would want users to open is located in the same directory as the script itself.

The code is as follows:-

#!/bin/bash
#Filename: quit_example

quit=n
while [ "$quit" = "n" ]
do
   clear
   echo
   echo "1. Show date"
   echo "2. Show hostname"
   echo "3. Display text file"
   echo "Q. Quit"
   echo
   echo "Enter choice"
   read choice
case $choice in
   1) date
      echo "Hit Enter to carry on"
      read junk;;
   2) hostname
      echo "Hit Enter to carry on"
      read junk;;
   3) echo "Enter file name to be displayed"
      read $filename
           if [ -r "$filename" -a -f "$filename" ]
             then
               clear
             cat $filename
            else
      echo "Cannot display $filename"
           fi
     echo "Hit Enter to carry on"
     read junk;;
   Q|q)quit=y;;
esac
done

Would anyone be able to show me where I am going wrong?

Cheers,


Trufla
trufla is offline   Reply With Quote
Old Apr 4th, 2005, 10:11 AM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,623
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Quote:
Originally Posted by trufla

  #!/bin/bash
  #Filename: quit_example
  
  quit=n
  while [ "$quit" = "n" ]
  do
     clear
     echo
     echo "1. Show date"
     echo "2. Show hostname"
     echo "3. Display text file"
     echo "Q. Quit"
     echo
     echo "Enter choice"
     read choice
  case $choice in
     1) date
        echo "Hit Enter to carry on"
        read junk;;
     2) hostname
        echo "Hit Enter to carry on"
        read junk;;
     3) echo "Enter file name to be displayed"
        read $filename
             if [ -r "$filename" -a -f "$filename" ]
               then
                 clear
               cat $filename
              else
        echo "Cannot display $filename"
             fi
       echo "Hit Enter to carry on"
       read junk;;
     Q|q)quit=y;;
  esac
  done
   #!/bin/bash
   #Filename: quit_example
   
   quit=n
   while [ "$quit" = "n" ]
   do
      clear
      echo
      echo "1. Show date"
      echo "2. Show hostname"
      echo "3. Display text file"
      echo "Q. Quit"
      echo
      echo "Enter choice"
      read choice
   case $choice in
      1) date
         echo "Hit Enter to carry on"
         read junk;;
      2) hostname
         echo "Hit Enter to carry on"
         read junk;;
      3) echo "Enter file name to be displayed"
 read filename // <<<==== the problem was that you had read $filename and you dont need the $ in the variable name when reading into it :)
              if [ -r "$filename" -a -f "$filename" ]
                then
                  clear
                cat $filename
               else
         echo "Cannot display $filename"
              fi
        echo "Hit Enter to carry on"
        read junk;;
      Q|q)quit=y;;
   esac
   done


look for the //<<<<<===== in my code to show you what i did and it works fine now
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Apr 4th, 2005, 10:17 AM   #3
trufla
Newbie
 
Join Date: Apr 2005
Posts: 6
Rep Power: 0 trufla is on a distinguished road
Brill!


Trufla
trufla is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC