Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Simple menu script (http://www.programmingforums.org/showthread.php?t=3092)

trufla Apr 4th, 2005 8:28 AM

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

big_k105 Apr 4th, 2005 10:11 AM

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 :)

trufla Apr 4th, 2005 10:17 AM

Brill!


Trufla


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

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