Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-29-2011, 06:50 PM   #1
LQ Newbie
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
Bash Scripting: Menu from file

I have a file called list.txt with on word on each line that changes in length. I'd like to make a menu, each line being its own choice. I pieced together most of it the only thing missing is a failsafe for typing a number out of range. Any ideas?

p=`cat list.txt | awk '{print$1}'`

select CHOICE in $p
echo $CHOICE
Old 05-29-2011, 11:24 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
First of all, $(..) is highly recommended over `..`.

Second, awk can take a filename as an input argument, so you don't need to use cat and a pipe. And if there's only one word per line, then you don't need $1.
awk '{print}' list.txt
Third, using awk here is truly overkill anyway. cat alone does exactly the same thing as the above command. Even better, just use a simple < shell redirection. You can even put it directly in the select command and skip the variable.
p=$( cat list.txt )
p=$( <list.txt )
select CHOICE in $( <list.txt ); do
Fourth, an invalid choice in select results in an empty variable. So simply add a test of some kind after the read to check whether it contains anything. If empty, then just let it loop again (or do whatever you want it to do), else issue a break command to exit the loop (or again, whatever). case statements are used most often here, but any conditional construct will do.

select CHOICE in $( <list.txt ) ; do

   case $CHOICE in

      "") echo "Not a valid choice."
          echo "Try again." ;;

      *)  echo "$CHOICE"
          break ;;



exit 0
1 members found this post helpful.
Old 05-30-2011, 09:47 PM   #3
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 891

Rep: Reputation: 208Reputation: 208Reputation: 208
Pseudo code:
a=wc -l filename
while choice < 1 and > $a
display menu and choose
if choice > 0 and <= a then
do <what you got to do>
error message
next choice


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting with file date comparison coop59 Programming 3 03-31-2011 08:02 AM
bash a file also a string? Daravon Linux - Newbie 3 09-03-2008 03:38 AM
Bash Scripting Empty file john8675309 Linux - Software 2 08-08-2007 04:05 PM
bash scripting testing for file exvor Programming 4 08-08-2007 03:42 PM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration