Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello, first post, long time viewer.
The script I'm working on requires a ls to retrieve the files in the current directory. Then I want the listed files to return as selectable menu items. As I have it now the user must manually type out the name of the file to process. I want the user to be able to just push the number associated with that file. Here is what I have so far..
typeset THIS_FILE=StartofFilename_2008-12-29.csv
echo
echo "${THIS_FILE}, Is this the correct file to process y/n)?"
read YN
#
case $YN in
[yY]*) echo Processing...
;;
[nN]*) echo
echo
echo "Please select a file to process, possible files include:"
echo
ls -al *.csv | grep ^- | nawk '{print $NF}'
echo
read THIS_FILE?"Which file would you like to process? "
wait
if [[ $THIS_FILE != 'StartofFilename_'*'.csv' ]]; then
echo
echo "$THIS_FILE is an invalid filename, exiting..."
echo
exit
fi
echo
echo "Processing $THIS_FILE..."
echo
;;
esac

Found the following code on another site, trying to adapt it to my needs but when it returns the menu options it misses the first line. Hmm...
#!/bin/ksh
# select input file
ls -al *.csv | grep ^- | nawk '{print $NF}' >> temp
file_name=temp
cat temp# define array
set -A line_array# populate the array
i=1while read file_line do line_array[i]=${file_line} let i=${i}+1
done < ${file_name}#Display selection options
i=1
x=10var_select ()
{
while [ ${i} -le ${x} ] && [ ${i} -lt ${#line_array[*]} ]
do
echo
echo $i ${line_array[i]}
let i=$i+1
done#Select an option
echo
echo "Select an option number or press enter: \c"
read numdisp_select
}
disp_select ()
{
if [ ${num} -le ${i} ]
then
echo "You have selected: ${line_array[num]}"
else
x=${x}+10
var_select
fi
}var_select
exit 0
# end

I'll just go ahead and answer my own question.. I forgot to set line_array=1 so it was skipping that line.. cheers..
# populate the array
i=1
line_array=1
while read file_line

Why are you initializing a shell array? I must disagree with you as the main problem is that you aren't looking at the last element in the array.
I changed this line:
while [ ${i} -le ${x} ] && [ ${i} -lt ${#line_array[*]} ]
to:
while [ ${i} -le ${x} ] && [ ${i} -le ${#line_array[*]} ]
I have two other issues. I'll explain then and post my code at the bottom of this reply:
There's no reason to append since this is one command:
ls -al *.csv | grep ^- | nawk '{print $NF}' > temp
Also, I'm running Solaris 9, and the while loop wouldn't work all on one line:
while read file_line
do
line_array[i]=${file_line}
let i=${i}+1
done < ${file_name}Here is my copy of your script:
#!/bin/ksh#rm -f temp
# select input file
ls -al *.csv | grep ^- | nawk '{print $NF}' > temp
file_name=temp
cat temp# define array
set -A line_array# populate the array
i=1while read file_line
do
line_array[i]=${file_line}
let i=${i}+1
done < ${file_name}#Display selection options
i=1
x=10var_select ()
{
#while [ ${i} -le ${x} ] && [ ${i} -lt ${#line_array[*]} ]
while [ ${i} -le ${x} ] && [ ${i} -le ${#line_array[*]} ]
do
#echo
echo $i ${line_array[i]}
let i=$i+1
done#Select an option
echo
echo "Select an option number or press enter: \c"
read numdisp_select
}
disp_select ()
{
if [ ${num} -le ${i} ]
then
echo "You have selected: ${line_array[num]}"
else
x=${x}+10
var_select
fi
}var_select
exit 0
# end

The select command works well for menus.
#!/bin/ksh
print "\nPlease select a file to process, possible files include:"
PS3="Which file would you like to process? "
select RESPONSE in $(ls -al *.csv | grep ^- | nawk '{print $NF}') Exit; do
CHECK=$(print "$REPLY $RESPONSE" | grep -ic "exit")
[[ $CHECK == 1 ]] && exit
case $RESPONSE in
"") print "\tInvalid selection ($REPLY), select option by number.\n" ;;
${RESPONSE}) break ;;
esac
done
print "You have selected: $RESPONSE"

pklinger: Your code is much cleaner than what I was using. It work but when I enter a number then random characters after it the menu continues. Looks like its only concerned with the first number, but thats ok.
ex:
Please select a file to process, possible files include:
1) _2008-12-19.csv
2) _2008-12-22.csv
3) _2008-12-30.csv
4) Exit
Which file would you like to process? df3
Invalid selection (df3), select option by number.Which file would you like to process? 3dff
Processing: _2008-12-30.csv

Your right, try this:
#!/bin/ksh
print "\nPlease select a file to process, possible files include:"
PS3="Which file would you like to process? "
select RESPONSE in $(ls -1 *.csv) Exit; do
[[ $(echo $REPLY | sed 's/[[:digit:]]*//g' | wc -w) -gt 0 ]] && RESPONSE=""
case $RESPONSE in
"") print "\tInvalid selection ($REPLY), select option by number.\n" ;;
${RESPONSE}) break ;;
esac
done
print "You have selected: $RESPONSE"

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |