Output find to array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output find to array
# 1  
Old 08-13-2012
Output find to array

Hi

I'm trying to write a shell script which finds all the .zip files in a given directory then lists them on the screen and prompts the user to select one by entering a number e.g.

The available files are:
1. HaveANiceDay.zip
2. LinuxHelp.zip
3. Arrays.zip
Please enter the number of the
archive you would like to select:


What I want to do is store the results of the find command in an array so once the user enters '3' for examaple, I can get the filename by looking up the 3rd array element.
I'm not sure of the syntax for arrays in shell scripts but I want something like this:

Code:
array=$(find command)
i=1
while $i -le (array elements)
   do
      echo "$arrayelement '. ' $arrayelementvalue"
      i + 1
   done
 
echo "Please enter the number of the "
echo "archive you would like to select:"
read x
 
file=arrayvalue(array element $x)

Any help would be much appreciated.

Thanks!

Last edited by Scott; 08-13-2012 at 09:30 AM.. Reason: Please use code tags
# 2  
Old 08-13-2012
What OS and which shell? Linux / bash?

---------- Post updated at 08:52 AM ---------- Previous update was at 08:48 AM ----------

If so, this uses GNU extention -print0 so it works with filenames containing even newlines safely.

Code:
#!/bin/bash

while IFS= read -rd '' file; do
        zipfiles+=( "$file" )
done < <(find . -name '*.zip' -print0)

for e in "${!zipfiles[@]}"; do
        echo "$e) ${zipfiles[e]}"
done

# 3  
Old 08-13-2012
Thanks for the reply. I'm using Linux shell script so I'll give this a try when I get home.
Please could you explain what each step is doing? I'm new to Linux and shell script so I'd like to learn and understand exactly what each bit is doing rather than just copying and pasting someone elses code.
Thanks.

Last edited by zX TheRipper Xz; 08-13-2012 at 12:41 PM..
# 4  
Old 08-13-2012
You can do solution using select
Code:
PS3="Your choice (ENTER=list again, e=end):"
select f in *.zip
do
        [ "$REPLY" = "e" ] && break
        echo "Choice: ($REPLY) $f"
        echo $f
        # if like to exit after 1st selection, then add line break
        # break
done

This User Gave Thanks to kshji For This Post:
# 5  
Old 08-13-2012
Quote:
Originally Posted by kshji
You can do solution using select
Code:
PS3="Your choice (ENTER=list again, e=end):"
select f in *.zip
do
        [ "$REPLY" = "e" ] && break
        echo "Choice: ($REPLY) $f"
        echo $f
        # if like to exit after 1st selection, then add line break
        # break
done

That's exactly what I need! Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Looping through the contents of array for output name

Hi all, I am trying to loop through the string contents of an array, to add it during the saving of the output files. I am trying this code to print each column and save it to unique file name, but it doesn't work. Thanks for any help. fnam=(japan usa uk) alldata.dat contained sample data... (1 Reply)
Discussion started by: ida1215
1 Replies

2. UNIX for Dummies Questions & Answers

Formatting Array Output

Hello All, Q1) I have the below code but when the email was sent out all lines are merged and coming out as a single line though my printf statement has newline "\n", How do i avoid that? Q2) In my second IF statement when i introduced the backslash "\" for continuation of a line or command, i... (10 Replies)
Discussion started by: Ariean
10 Replies

3. Shell Programming and Scripting

Filter output as an array element

I am filtering the value of Server status from a file and am storing it in a temp file which I compare later to exit with appropriate status. I am wondering if I can directly output the value of Server status as an array element and then compare the value of elements to get the right exit status ... (2 Replies)
Discussion started by: paslas
2 Replies

4. Shell Programming and Scripting

Output file list to array?

Hey, guys, scripting newb here. I'm trying to get a list of all .dmg files in a folder and save the output into an array. My first attempt was ARRAY= ( `ls $REIMAGEPATH | grep \.dmg$` ) However, I understand why that doesn't work now (at least I think I do). But I don't know what the... (5 Replies)
Discussion started by: nextyoyoma
5 Replies

5. Shell Programming and Scripting

How to store output of command to an array

Hello Guys, I am trying to store output of command to an array and then want to print, but it showing an error that "bad substitution". I am not getting why it's happening. Can anyone help me? #!/usr/bin/sh hr=0 min=0 i=1 last $1 | grep -w `date "+%b"` | grep -v '\(0:.*\)' | grep -vw sshd... (8 Replies)
Discussion started by: kasparov
8 Replies

6. Shell Programming and Scripting

Output of shell in array

Hi, i have a file which reads, arun/manager/200000 pradeep/engineer/10000 karthik/teamlead/30000 ..... i want an output to show, name role salary ======================= arun manager 200000 pradeep engineer 10000 and so on.. i want to do... (9 Replies)
Discussion started by: pradebban
9 Replies

7. Filesystems, Disks and Memory

iostat output vs TPC output (array layer)

Hi Guys, I've been having some arguments with my colleagues about one thing. Always my thought was that as as far as disk performance is concern by looking at the output of the iostat command (AIX) you would be able to identify if you have a hot disk and then by moving some files out that disk... (3 Replies)
Discussion started by: arizah
3 Replies

8. Shell Programming and Scripting

Put output into an array

I'm trying to take the output of an environment that has multiple strings ex. # echo $SSH_CLIENT 192.168.1.1 57039 22 I need that IP... so I can set it to another environment. Thank you (3 Replies)
Discussion started by: adelsin
3 Replies

9. Shell Programming and Scripting

bash:awk output into an array

Hi, I have a file 1:apple orange:one 2:banana:two 3:cherry:3 When I do awk -F: ' { print $2 } ' file apple orange banana cherry Now, when i redirect awk output to the file it has issue with strings #!/bin/bash FILEA=file A=(`awk -F: ' { print $2 } ' $FILEA `) echo ${A} (2 Replies)
Discussion started by: phamp008
2 Replies

10. Shell Programming and Scripting

output of an array

Hi gurus, I need to set up an array like this set - A arr 'A', 'B' The output of this array should be like this 'A','B' Right now, I get the output like this 'A B' Can anyone suggest me on how to achieve this. thanks (3 Replies)
Discussion started by: ragha81
3 Replies
Login or Register to Ask a Question