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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array element compare in while condition

Status
Not open for further replies.

drewbyh

Programmer
Dec 8, 2004
1
CA
The following is part of a script I'm writing to show releases of software available in a directory and then ask the user to select one. While $answer is not in the avail[] array ask the question again. I need to know how to compare each element in the array to the answer variable in the while loop condition. Any help is greatly appreciated. Thanks

avail=(`ls -C`)
echo "Releases available: ${avail[*]}"

echo -n "What SCSRelease whould you like to run? (x.x.x) "; read answer

# This part does not work

while [ "$answer" != "${avail[*]}" ];
do
echo -n "Release is already running or not valid. Try again: (x.x.x) "
done
 
You'll probably have to write a function that iterates over the array values, comparing the answer to each one and return success when it succeeds.
 
or.... use the 'select' clause: 'man ksh' and search for 'select'.

Not exactly using the arrays, but building a 'menu-driven' interface - which what I think you're trying to do.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
In your example, why not simply test the existence of the file ?
while [ ! -f "$answer" ]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top