[SOLVED] Searching for files and populate in array
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Hi Guys, I was wondering if someone can assist in the following? I want to search for a filename (with wildcard) and then use the name I found to do some stuff with it. Let me explain... Let say I want to search for a file, I would normally do the following: # find / -iname filename*.dmp. The problem is, there might be more than one filename returned. Therefore, I need to handle each filename as they are returned. How can I put this in a shell script to search for the filename and then one by one, enter it into an array or something to be dealt with later? I know how to pass the search string from the command line to the shell script, I just cannot figure out how to make each filename found equal to a variable, or and array. Can someone give me some pointers? Thanks!
First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Second, it doesn't do much good to say something didn't work, unless you explain in what way it failed. What output or errors did you get? What have you tried doing to diagnose or solve the problem?
You probably don't want to use the filename variable to store the pattern, as the while loop just overwrites it. And you need to use $ when you want to expand it. Don't forget to double-quote it too, so that the shell doesn't expand it before find can use it.
Anyway, to set the output of find, or any command, into an array, you can use the following, a variation on grail's loop:
Awesome, this is exactly what I needed. Thanks "Catkin" and "David the H" it is working now. I added the bash part, set permissions and its all working well. I still don't have a clue how it does it, but it's working. Thanks again for the help. Now I can assign each filename to do some magic... Cheers, J
The done < is redirection. It makes bash redirect the output of the process substitution to the while-do-done loops standard input.
Now we know what the standard input is, here's how it is used
while IFS= read -r -d '' file
while (and the following do and done lines) makes bash loop as long as what follows is true, that is as long as it sets a 0 return code.
What follows is IFS= read -r -d '' file
This begins with a variable assignment (IFS=) followed by a command. The variable assignment differs from one not followed by a command in that it is only in effect during the execution of the command, as explained here.
The string contained in IFS are the characters used by bash when it expands an expression into separate words (word-splitting). By default it is space, tab and newline. Here it is set to an empty string so word-splitting does nothing when bash runs the following read.
read -r -d '' file reads from standard input:
ignoring any "backslash escapes" (-r, raw mode) such as \t for tab.
using the NUL character as the delimiter to separate "lines" (-d ''). The '' is an empty string but bash silently appends the NUL character to empty strings (sorry -- I cannot find any reference for this behaviour).
and reading the entire line into variable file.
When read tries to read another "line" when there are none left from the find output, it fails and sets a non-zero return code so the while terminates.
EDIT: the purpose of setting IFS to the empty string is to ensure that leading and trailing characters in the input "lines" which match characters in the IFS value are not stripped when it is assigned to the file variable (thanks to David the H's post in another thread for explaining this).