LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-29-2011, 06:50 PM   #1
sociopathichaze
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?

Code:
#!/bin/bash
p=`cat list.txt | awk '{print$1}'`

select CHOICE in $p
do
echo $CHOICE
done
 
Old 05-29-2011, 11:24 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
Code:
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.
Code:
p=$( cat list.txt )
#or
p=$( <list.txt )
#or
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.
Code:
#!/bin/bash

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

   case $CHOICE in

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

      *)  echo "$CHOICE"
          break ;;

   esac

done

exit 0
 
1 members found this post helpful.
Old 05-30-2011, 09:47 PM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

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


Reply



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 scripting...is 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:06 PM.

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