I am very new to Unix and know the basic commands. I have to write a script file and I'm completely lost. The script file is to show the following at the beginning:
Menu of Options
Display all files in a user's home directory.
Welcome yourself to the program.
Display System Information
Exit back to Windows
I have been able to get this to display by use of the echo command. Beyond that, I have no idea what to do. I have search endlessly online for a way to create a menu in Unix and this is all I have to show for it. I also need a line that says "Please enter your choice." Once the user enters a choice, I have no idea how to write the various commands to perform what needs to be done.
Options 3 and 4 seem straightforward if I could get started. However, in option 1 the following criteria have to be met:
Display all files in a user's home directory. This should allow the user to look at any user's home directory, provided they enter the correct user name.
Put in a safeguard that if the user does not enter a name, that the current user's home directories display.
I have no idea how to work with Option 2.
I'd greatly appreciate it if anyone would please help me with this. Thank you.
#!/bin/ksh
print "Select a terminal type"
cat << ENDINPUT
sun
ansi
wyse50
ENDINPUT
print -n "Which would you prefer? "
read termchoice
# this kind of code will let you display a menu and get input.
# and then use a case statmet to do each menu item selected.
case $termchoice
sun)
print "do stuff for a sun terminal"
;;
ansi)
print "do other stuff for a ansi terminal"
;;
wyse50)
print "do stuff for a wyse terminal"
;;
*)
print "Invalid option, Please try again. Use �1�-�3�"
;;
esac
Thank you for the reply, but I'm still confused after reading it. For example, I have no idea what the "cat << ENDINPUT" means. What do "sun," "ansi," and "wyse50" have to do with anything? It would be much easier if you could use the information I provided to include with your response.
I can kinda figure out that "termchoice" relates to the selection that the user makes from the menu. However, you then have "sun", ansi, and wyse50 followed by a single parentheses on the following line. I don't get that at all either. Please provide an example that has to do with the information I gave. I could understand that much better. Thank you.
The example he's given is just that - an example. Start by adapting that to get you close to what you need then we can help make the changes to get it just right.
cat << ENDINPUT
sun
ansi
wyse50
ENDINPUT
Just means to output to the screen everything up until 'ENDINPUT'. It's just an easier way to print multiple lines.
the ';;' within each case tells the script to stop checking the rest of the case options once it gets a match.
#!/bin/ksh
while true
do
print "What do you need to do today?"
cat << ENDINPUT # this is the start of a "here document"
Display all files in a user's home directory.
Welcome yourself to the program.
Display System Information
Exit back to Windows
ENDINPUT # this marks the end of the "here document"
print -n "Please enter your choice."
read option
case $option # this is a case statment , much like many if/then statments
1)
print "here we do all the code for option 1"
;;
2)
print "here we do all the code for option 2"
;;
3)
print "here we do all the code for option 3"
;;
4)
print "here we do all the code for option 4"
exit 0 # this will exit back to the shell
;;
*) # this line will catch anything other than 1-4 options.
print "Invalid option, Please try again. Use �1�-�4�"
;;
esac # this ugly bit is the end marker of the case statment.
done
for more infomation on the "here document" stuff have a read on google.
BTW this code is untested and so probly has bugs
Thanks again. I'm now able to get the menu to display and I'm starting to slowly understand what you're posting. However, after the second "endinput", I have print -n "Please make a choice:" The problem is that when I run the program it says this:
Please ignore that last post. I tried echo and it worked. I must've done something wrong the first time I tried it. Now it's on to doing the code. I'll keep this thread going with the code I use and any problems I run into. Thank you very, very much for your help.