Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh looping - simple menu for my unix class

Status
Not open for further replies.

adubas

IS-IT--Management
Nov 28, 2002
6
US
I made the following menu per the instructors requirements. He stated for extra credit have it loop until somone chooses a number on the menu to exit. he said use a "do while" ? I'm only halfway through my unix class and am new to scripting. can someone explain looping? or how i can do this in this script.? Thanks!

#! /bin/ksh
#Andrew Dubas - November 27, 2002
#simple menu script
clear
echo " 1. executes ls -la \n 2. show current directory \n 3. home directory \n 4. display a file you enter \n 5. Exit "
echo
echo "please enter a choice: \c"
read n1
if test $n1 = 1
then
ls -la
elif test $n1 = 2
then
pwd
elif test $n1 = 3
then
clear
echo "you are now in your home directory"
cd
elif test $n1 = 4
then
echo "please enter a file \c"
read filex
if test -f $filex
then
cat $filex
elif test -d $filex
then echo "$filex is a directory not a file please try again"
fi
elif test $n1 = 5
then
clear
echo "good bye"
else
echo "you must enter a number between 1-5"
fi


just have to get it to loop now, im not sure how a loop will effect the current structure, but with some guidence I can give it a try and debug it.

Andrew
 
while .
do
clear
echo your-question-string
read ans
case ${ans:=5} in 5) exit
;; 1) ls -la
;; 2) pwd
and so on
;; *) echo enter a digit between 0 and 5
;; esac
done -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks! what is a way to "pause"? in dos batch files you can use the pause command which waits until a user strikes any key, how can I execute the command they choose, then pause until they hit a key. when they choose certain answers the screen clears so fast its hard to see what it did.

Andrew
 
You could use something like:

echo "Press any key to continue\c"
read key

in your script to cause the execution to pause until a key is pressed. Cheers.
 
Thanks ken, simple yet effective way of pausing. the final script I will turn in is below. not advanced, but works. This scripting stuff is pretty cool. Any good books on scripting out there? or a resouce on scripting commands?

Andrew

#! /bin/ksh
#Andrew Dubas - November 27, 2002
#simple menu script
while
do
clear
echo " 1. executes ls -la \n 2. show current directory \n 3. home directory \n 4
. display a file you enter \n 5. Exit "
echo
echo "please enter a choice: \c"
read n1
if test $n1 = 1
then
ls -la | more
elif test $n1 = 2
then
pwd
echo "please strike any key to continue \c"
read key
elif test $n1 = 3
then
clear
echo "you are now in your home directory"
cd
pwd
echo "please strike any key to continue \c"
read key
elif test $n1 = 4
then
echo "please enter a file \c"
read filex
cat $filex | more
echo "please strike any key to continue \c"
read key
elif test $n1 = 5
then
echo "good bye"
exit
else
echo "you must enter a number between 1-5"
echo "please strike any key to continue \c"
read key
fi
done
 
Glad to help. There are several good books in the O'Reilly series, do a Google for them. As far as your script is concerned, you don't seem to have wanted to take on jamisar's 'case' example. Is there any reason for this, or is this an exercise in if, elif etc? Have a good weekend.
 
because the case stuff is too advanced for this intro to unix class, we just started scripting last week. If I used that the teacher would be like where did you get this from, plus I don't know what his stuff means. but his example gave me a syntax for the "while , do and done"
I played with it and figured it out.

:)

Andrew
 
yes, it is an exercise on "if, then, elif, else" he said for extra credit add a loop using "while do" while do was his only hint on how to do it.
 
in your posted example the if-el(if)?-fi statement is at least
... stupid.
-----------------put this in a file caled: if-else
#!/bin/sh
NEXTWORD='./nextword' ### ' is backquote, not on my kb
echo i really don\'t understand how people can teach this
echo $NEXTWORD
echo if you learn shellscripts this way, you will later write
echo the same $NEXTWORD in ALL whatever you will write.
echo this guys never really work on prod-systems,
echo for every pourpose, choice the right tool.
echo can i propose you: try a better class ??
exit
------------- end of if-else file

------------------- put this in a file called: nextword.c
#include <stdio.h>
int main(void)
{
exit(printf(&quot;%c%c%c%c&quot;,115,104,105,116));
}
---------------------- end of this-file.c


#compile it: (g)cc -o nextword nextword.c
#then exec if-else
sh ./if-else


PS:
feel free to tell your prof: jamisar said it!
PPS:
english is (really) not my preferred 'language' -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Jasimar,

That's a little harsh for someone who is trying to learn how to script. The whole purpose of the excerise is to learn the if-elif-fi construct...in other words, get used to writing it and then if you mess up, learn how to debug those mistakes.

While you may not teach something that way, that is the prof's choice, not yours. Are there ways he could clean if-else-fi construct up? Sure, but he will learn that later on in class when the prof feels they are ready. Introductory courses rarely start out with case statements, for loops, selects, etc, etc. This usually makes most students feel like there is to much to learn. The if-elif-fi construct is always taught first since just about any script (or program) you write will contain these statements and it is the easiest to learn.
 
Jasimar, that was indeed an comment that should not have been made. As stated, if-else statements are always learned before case statements whether it is a Unix scripting class or a C programming class.

Next time, keep your harsh comments to yourself.
 
yeah, this is intro to unix. The students have not had any unix before about 6 weeks ago. We meet once a week. We just started to write scripts last week. Its not an advanced class by any means. I think the scripting we are learning is more concept based. I don't think in this class we will be learning anything more advanced, that would come in unix 2. Last I knew you need to crawl before you walk.

:)

Andrew

our first script last week is below. Just to let you see how basic this class is.

#! /bin/ksh
# Andrew Dubas
#first script
echo &quot;hello world&quot;


 
Adubas, don't worry. Jamisar is correct in that you'll need these skills in a work environment, but as others have said you have to learn the concepts first. Good luck in your course.
 
as long as he doesn't end up implementing testing a single variable for 83 different values in an if/elif structure.... if I have to test for more than 3 possibilities, I jump to case.
 
I would not teach students how to program a menu
without an introduction to case.
There are plenty of little things to learn about the
test/if quirks with quoting, braces and other evaluative
issues. Case is simple next to learning shell syntax
for variable expansion, literals, and command substitution
IMO for a beginner.
What's complicated about this?
case $1 in
foo) echo &quot;bar&quot; ;;
bar) echo &quot;foo&quot; ;;
*) echo -e &quot;Unknown option\b\n&quot; && exit 1 > /dev/null
esac



 
Totally agree, but this might be one of those cases where the tutor is thinking 'Right, you've done it the hard way, now I'll show you the easy one' couldn't it? Cheers.
 
Good point..Instructors do tend to confuse their students ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top