# Creating a menu within a script file

**URL:** <https://community.unix.com/t/creating-a-menu-within-a-script-file/178765>\
**Category:** Shell Programming and Scripting\
**Created:** [December 9, 2007, 10:20pm UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765 "2007-12-09T22:20:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sinjin](https://community.unix.com/letter_avatar/sinjin/32/6_449c533f586939b6e5f246632a46421f.png) [@sinjin](https://community.unix.com/u/sinjin)\
**Post date:** [December 9, 2007, 10:20pm UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/1 "2007-12-09T22:20:47Z")

</div>

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

1. Display all files in a user's home directory.
2. Welcome yourself to the program.
3. Display System Information
4. 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:

1. 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.
2. 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.

---

<div class="post-metadata">

**Author:** ![robsonde](https://community.unix.com/letter_avatar/robsonde/32/6_449c533f586939b6e5f246632a46421f.png) [@robsonde](https://community.unix.com/u/robsonde)\
**Post date:** [December 9, 2007, 10:28pm UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/2 "2007-12-09T22:28:40Z")

</div>

have a look at this

#!/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

---

<div class="post-metadata">

**Author:** ![sinjin](https://community.unix.com/letter_avatar/sinjin/32/6_449c533f586939b6e5f246632a46421f.png) [@sinjin](https://community.unix.com/u/sinjin)\
**Post date:** [December 9, 2007, 10:51pm UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/3 "2007-12-09T22:51:16Z")

</div>

Robs,

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.

---

<div class="post-metadata">

**Author:** ![Smiling\_Dragon](https://community.unix.com/user_avatar/community.unix.com/smiling_dragon/32/722_2.png) [@Smiling\_Dragon](https://community.unix.com/u/Smiling_Dragon)\
**Post date:** [December 9, 2007, 10:55pm UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/4 "2007-12-09T22:55:34Z")

</div>

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.

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![robsonde](https://community.unix.com/letter_avatar/robsonde/32/6_449c533f586939b6e5f246632a46421f.png) [@robsonde](https://community.unix.com/u/robsonde)\
**Post date:** [December 9, 2007, 11:06pm UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/5 "2007-12-09T23:06:55Z")

</div>

#!/bin/ksh  
while true  
do  
print "What do you need to do today?"

cat \<\< ENDINPUT # this is the start of a "here document"

1. Display all files in a user's home directory.
2. Welcome yourself to the program.
3. Display System Information
4. 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 🙂

---

<div class="post-metadata">

**Author:** ![sinjin](https://community.unix.com/letter_avatar/sinjin/32/6_449c533f586939b6e5f246632a46421f.png) [@sinjin](https://community.unix.com/u/sinjin)\
**Post date:** [December 9, 2007, 11:58pm UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/6 "2007-12-09T23:58:25Z")

</div>

Robs,

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:

print: Command not found.

I'd appreciate your help with this.

---

<div class="post-metadata">

**Author:** ![robsonde](https://community.unix.com/letter_avatar/robsonde/32/6_449c533f586939b6e5f246632a46421f.png) [@robsonde](https://community.unix.com/u/robsonde)\
**Post date:** [December 10, 2007, 12:05am UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/7 "2007-12-10T00:05:13Z")

</div>

> [@sinjin](#):
>
> print: Command not found.
> 
> I'd appreciate your help with this.

try echo insted of print.

---

<div class="post-metadata">

**Author:** ![sinjin](https://community.unix.com/letter_avatar/sinjin/32/6_449c533f586939b6e5f246632a46421f.png) [@sinjin](https://community.unix.com/u/sinjin)\
**Post date:** [December 10, 2007, 12:16am UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/8 "2007-12-10T00:16:16Z")

</div>

Robs,

Echo produces the same result:

Echo: command not found

---

<div class="post-metadata">

**Author:** ![sinjin](https://community.unix.com/letter_avatar/sinjin/32/6_449c533f586939b6e5f246632a46421f.png) [@sinjin](https://community.unix.com/u/sinjin)\
**Post date:** [December 10, 2007, 12:18am UTC](https://community.unix.com/t/creating-a-menu-within-a-script-file/178765/9 "2007-12-10T00:18:21Z")

</div>

Robs,

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.🙂
