LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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!

Notices


Reply
  Search this Thread
Old 01-06-2012, 01:22 AM   #1
harishisnow
LQ Newbie
 
Registered: Nov 2010
Location: bangalore, india
Posts: 17

Rep: Reputation: 0
Bash script problem


Hello,

I am writing a bash script which uses the find command and stores the result of the find command to a variable. How do I retreive the values present in the variable one by one?

ie:
#!/bin/sh

VAR=`find /home/par_dir -type f -name "target_file" -print`

(The result of the find command is:
/home/par_dir/abc/target_file
/home/par_dir/xyz/target_file
)

Now, when I use
for linee in VAR
do
echo $linee
done

I am not able to retreive the rows one by one.
Can anybody help me in doing so?

Thanks in advance..!!

Regards,
Harish
 
Old 01-06-2012, 01:31 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Best way to go, IMO, is using an array for this. Have a look here - http://stackoverflow.com/questions/1...o-a-bash-array

Cheers,

Josh
 
Old 01-06-2012, 02:06 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You'll find just about everything you need to know here:

http://mywiki.wooledge.org/BashFAQ/001


I recommend using a loop to set the files into an array instead of a scalar variable.

Code:
while IFS="" read -r -d "" file; do array+=( "$file" ) ; done < <( find . -print0 )
 
Old 01-06-2012, 02:13 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Technically, either way would work. Besides, that's what I meant by giving that link to the OP. Maybe you were looking at something different on that page?
 
Old 01-06-2012, 07:24 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I was just reiterating what you said, really, while adding another link with a slightly broader focus than yours, and a scripting example.

We agree that arrays are the way to go here, certainly. Any time you have a list of related strings, such as file names, you should be using an array for it.

More help on arrays here:
http://mywiki.wooledge.org/BashGuide/Arrays
http://www.tldp.org/LDP/abs/html/arrays.html
 
  


Reply



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
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 05:39 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
Problem with bash script cowardnewbie Programming 1 10-01-2001 06:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:30 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration