LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-14-2005, 10:24 AM   #1
d00bid00b
Member
 
Registered: Aug 2005
Location: London, UK
Distribution: Debian Testing
Posts: 157

Rep: Reputation: 30
Generating iteratable arrays in bash


I am wanting a command to populate an array with its output, for example:

slocate newfile > An_Array

Which would then allow a second command to iterate through An_Array, such as:
Code:
for x in An_Array
do
    rm -ir
done
This should pass the output of slocate to a list which then when iterated through will allow the user to opt to remove a specified file. At least, that's the theory!!

Any thoughts?

Last edited by d00bid00b; 08-14-2005 at 10:27 AM.
 
Old 08-14-2005, 10:58 AM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
So your looking for something like?

Code:
#!/bin/sh

until [ -z $1 ]; do
   An_Array[i++]=$1
   shift
done

for(( j = 0; j < i; ++j )); do
    echo "Element $j = ${An_Array[j]}"
done
This would produce:
Code:
$ ./test One two three
Element 0 = One
Element 1 = two
Element 2 = three
 
Old 08-14-2005, 11:24 AM   #3
d00bid00b
Member
 
Registered: Aug 2005
Location: London, UK
Distribution: Debian Testing
Posts: 157

Original Poster
Rep: Reputation: 30
Hmmm ... looks about right. Let me test it out and get back to you.

Thanx
 
Old 08-15-2005, 12:25 PM   #4
d00bid00b
Member
 
Registered: Aug 2005
Location: London, UK
Distribution: Debian Testing
Posts: 157

Original Poster
Rep: Reputation: 30
Thanks for the script leonscape. The mechanics seem okay, but now I need to figure out how to get the output of slocate to be loaded into An_Array. I tried slocate myfile_name > An_Array which gave me a text file of the output of slocate ... which was not what I had in mind.

How could I:
(a) accept input from a user (i.e. what file they want to slocate), and
(b) dynamically populate an array with the output of slocate so that it can be iterated over?

Thanks
 
Old 08-15-2005, 04:54 PM   #5
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
$1 works for command line inputs. So that is what the user could enter to the command. i think the easiest way of iterating the slocate return is to push it out to a temp file and read it into an array. Not being a Bash guru, i couldn't think of a way to redirect a command output to an array. So what I would end up with is:

Code:
#!/bin/sh

# The command name, So it doesn't matter if the script gets renamed.
CMD=`basename $0`

# the help screen, done like this so you can expand the explanation 
# between cat and end.
help_screen ()
{
cat <<END
    Usage: $CMD files..."
END
exit
}

# check theres something to process
if [ $# -eq 0 ]; then
    help_screen
fi

echo > tmpfile #Truncate the file

# doesn't matter how many they request appends the lot
# to one file.
until [ -z $1 ]; do
   slocate $1 >> tmpfile
   shift
done

# Now reads that file into an array
An_Array=( $(cat tmpfile) )

#now prints out the whole file
for i in $(seq 0 $((${#An_Array[@]} - 1))); do
  echo "${An_Array[$i]}"
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
bash scripting: problem with generating filenames mad_juno Programming 2 07-22-2005 10:57 AM
Bash Script- Finding/Generating unique UserIDs pheasand Linux - General 2 12-11-2004 09:44 AM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 09:57 AM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM


All times are GMT -5. The time now is 09:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration