Question:
Did I write this shell script correctly?
Kaymi
2013-06-03 18:41:53 UTC
I have to write a shell script that displays those 6 menu items. I also have to make sure users can continue running the script until they want to exit. Did I do this properly? Also, in line 2, How do I know what number to put behind the =? I just made up a number that was higher than six because I had no clue on how to figure that out.

1 #!/bin/bash
2 CHOICE=9
3 until [ $CHOICE -eq 6 ]
4 do
5 clear
6 echo Please select an menu item
7 echo
8 echo "1) Display a list of files and directories in the current directory"
9 echo "2) Display a long list of files and directories in the current directory"
10 echo "3) Display the last 10 lines of the log file messages (/var/log/messages)"
11 echo "4) Display the contents of this script"
12 echo "5) Display the contents of this script in reverse order"
13 echo "6) Exit the program"
14 echo
15 read CHOICE
16 case $CHOICE in
17 1) ls;;
18 2) ls-l;;
19 3) tail workgroup3;;
20 4) cat workgroup3;;
21 5) tac workgroup3;;
22 6) echo "Have a great day!";;
23 esac
24 done
25
26
Three answers:
anonymous
2013-06-03 19:10:36 UTC
It almost works, but you need to change the case statement lines a little. As you have it, the statements execute correctly but they are never seen by the user because the output disappears immediately as the loop continues.



So you need to add a sleep command to each case statement line so that the output remains on the screen for a few seconds so that the user can see the output.



Here's an example which would work for each of the case statement elements



17 1) ls;sleep 3;;
I R Sonajiso
2013-06-03 19:03:20 UTC
At a quick glance:



For line 2: most programmers would init CHOICE to 0; its more conventional.



3: tail -10 /var/log/messages

The -10 tells tail to display the last 10 lines



4: cat "$0"

The variable "$0" contains the script/application name. $1..$N contain any arguments appended after the executable name when the command $0 was invoked. $0 will always contain the name the app was invoked as. meaning that, if the app was called through a sym-link, the name of the sym-link is in $0



5: tac "$0"

See 4 for explaination
vining
2016-08-10 02:27:37 UTC
All i need to say is girls on UNIX = hot. I am no longer excellent with shell scripts. Check with the Ubuntu aid channel in IRC. Any one in there can support you i am sure, otherwise you might go for the bash channel in the event you wanted. More humans in there'll recognize what you're trying to do than on yahoo answers. I am going there at all times for my little unix missions. Which distro do you might have BTW? =) Edit** its sad that i am so used to answering the "which computer is nice" questions i can not aid the one time a real query will get asked. I do know just a little about UNIX though so if you need aid i would be pleased to try and figure it out with you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...