[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Selecting out of an array
From: |
DennisW |
Subject: |
Re: Selecting out of an array |
Date: |
Mon, 25 Jan 2010 22:19:20 -0800 (PST) |
User-agent: |
G2/1.0 |
On Jan 25, 7:28 pm, Jon_R <j...@reynoldsgc.com> wrote:
> Hello List,
>
> I am new to shell scripting and programming in general, so please excuse my
> ignorance. :)
>
> I am trying to setup a menu for some videos that I have so that you can
> select one and it will play it using xine or mplayer. My first attempt was
> to assign each video a variable name and then I could select which video and
> then play it. This is not very efficient because I always have to assign a
> new variable to a new video.
>
> I am now trying to populate an array using the 'ls' command. I have attached
> the script below.
>
> This gives me this as output to the screen:
> 0: video1
> 1: video10
> 2: video11
> 3: video2
> 4: video3
> 5: video4
> 6: video5
> 7: video6
> 8: video7
> 9: video8
> 10: video9
> 1) video9
> #?
>
> I get the prompt waiting for me to make a selection "#?". I then have a
> 'printf' statement that should print out my selection using the index number
> but it is not working. When I choose '1' I get video9 even though it is
> index number 10.
>
> Why does it even print "1) video9" when it should be waiting for input from
> the user?
>
> http://old.nabble.com/file/p27316649/newsh2.shnewsh2.sh
>
> Thanks for any help,
>
> Jon
> --
> View this message in
> context:http://old.nabble.com/Selecting-out-of-an-array-tp27316649p27316649.html
> Sent from the Gnu - Bash mailing list archive at Nabble.com.
It's not necessary to print the array, select will do that for you.
The way you're using select makes it only have access to one value
which is the last one in the array because the "for index" loop leaves
index pointing to the last array element. Also, don't use ls like this
- it's eyes-only.
Try this (or something similar):
if [ $# -eq 0 ]; then
ARRAY=( quit /home/jonr/Download/videoDriver/video/* ) # file
globbing instead of ls
select ITEM in "${ARRAY[@]}" ; do #
using @ as the subscript
if [[ $ITEM == "quit" ]]
then
break
fi
printf "%s\n" "$ITEM" # this
is where you'd process the selection
done
else
echo "Nothing to see here"
fi
The @ sign gives select access to the whole array.