![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 6
Rep Power: 0
![]() |
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
doneWould anyone be able to show me where I am going wrong? Cheers, Trufla |
|
|
|
|
|
#2 | |
|
PFO Founder
![]() ![]() |
Quote:
#!/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
donelook 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. |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2005
Posts: 6
Rep Power: 0
![]() |
Brill!
Trufla |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|