Computing.Net > Forums > Unix > Variable menu script from ls files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Variable menu script from ls files

Reply to Message Icon

Name: tkendall
Date: December 30, 2008 at 11:20:42 Pacific
OS: Windows XP
CPU/Ram: Intel Core2, 2gb
Product: Dell / INSPIRON
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: tkendall
Date: December 30, 2008 at 12:41:11 Pacific
Reply:

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=1

while read file_line do line_array[i]=${file_line} let i=${i}+1
done < ${file_name}

#Display selection options
i=1
x=10

var_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 num

disp_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


0

Response Number 2
Name: tkendall
Date: December 30, 2008 at 13:17:17 Pacific
Reply:

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


0

Response Number 3
Name: nails
Date: December 30, 2008 at 13:38:56 Pacific
Reply:

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=1

while read file_line
do
line_array[i]=${file_line}
let i=${i}+1
done < ${file_name}

#Display selection options
i=1
x=10

var_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 num

disp_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


0

Response Number 4
Name: tkendall
Date: December 30, 2008 at 13:47:03 Pacific
Reply:

OH, I see now. Still new to shell scripting but that makes more sense.. .']


0

Response Number 5
Name: pklinger
Date: December 30, 2008 at 14:29:44 Pacific
Reply:

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"


0

Related Posts

See More



Response Number 6
Name: tkendall
Date: December 30, 2008 at 14:50:57 Pacific
Reply:

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


0

Response Number 7
Name: pklinger
Date: December 31, 2008 at 09:13:34 Pacific
Reply:

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"


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Variable menu script from ls files

variables from one file to another www.computing.net/answers/unix/variables-from-one-file-to-another/7378.html

Shell Script: Get value from a file www.computing.net/answers/unix/shell-script-get-value-from-a-file/5760.html

input to a program from a file www.computing.net/answers/unix/input-to-a-program-from-a-file/5021.html