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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-27-2012, 02:16 AM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 11
Rep:
|
output to a variable in while loop
Hi everyone,
I'm trying to make this loop a little more verbose, but can't seem to work it out.
I'd like it to display the output of the find command on screen when it hits the else statement. The loop is essentially reading each $line in a text file and searching a $directory to see if it is there.
Code:
temp=/var/tmp/file_ferret.tmp
while read line
do
find $directory -type f | grep $line
if
[ $? -eq 0 ]
then
echo "FILE NOT FOUND" $line
else
echo $line "FOUND"
fi
done < $temp
What I want to do is add a variable to find like this, but I think I'm stuffing up the logic somewhere.
Code:
temp=/var/tmp/file_ferret.tmp
while read line
do
find_result=$(find $directory -type f | grep $line)
if
[ $? -eq 0 ]
then
echo "FILE NOT FOUND" $line
else
echo $line "FOUND" $find_result
fi
done < $temp
Would anyone be able to suggest how to do this properly?
Thanks heaps!
Leath
|
|
|
07-27-2012, 06:57 AM
|
#2
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep:
|
if [ $? -eq 0 ] - then $line "FOUND" $find_result, I think
or change your -eq to -ne and try again.
|
|
|
07-27-2012, 11:49 AM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
If testing the return code I would just use the 'if' command itself, however, as you are now storing the returned value in a variable it would seem
to make more sense to test if the variable has a value or not, ie use -n or -z tests
|
|
1 members found this post helpful.
|
07-27-2012, 09:52 PM
|
#4
|
LQ Newbie
Registered: May 2012
Posts: 11
Original Poster
Rep:
|
Thanks a lot for your help with this guys,
It looks like the $find_result variable isn't storing any info. When I echo $find_result it echos a blank line and continues on with the rest of the script. If I run that find command on it's own,it does actually print the results to screen.
Does anyone know if there's another way I can pipe the output to a variable? Maybe with something the other way around like this (only something that works)?
find $directory -type f | grep $line >> find_result
thanks
|
|
|
07-28-2012, 12:49 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
Can you show us an example from the command line and also some of the data from the $temp file? Mainly looking to see if there are unusual characters or white space.
|
|
|
07-28-2012, 01:18 AM
|
#6
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep:
|
Try like this
e.g.
Quote:
temp=/var/tmp/file_ferret.tmp
while read line
do
find_result=($(find $directory -type f | grep $line))
if
[ $? -ne 0 ]
then
echo "FILE NOT FOUND" $line
else
echo $line "FOUND" ${find_result[@]}
fi
done < $temp
|
And what do you want to store in the final_result ??
The name of the matched files or the matched lines in the files ??
Last edited by divyashree; 07-28-2012 at 01:39 AM.
|
|
|
07-28-2012, 02:45 AM
|
#7
|
LQ Newbie
Registered: May 2012
Posts: 11
Original Poster
Rep:
|
No access to computer right now to test the post above sorry.
Temp file eg:
G8908-7766
A047_B1298
A047_C1098
V0988-3489
Example of my ideal output to screen after a line of the temp file has gone through find:
G8908-7766 FOUND IN /mnt/media/mov/G8908-7766.mov
If the file is not found, then display the else statement on screen:
FILE NOT FOUND G8908-7766
|
|
|
07-28-2012, 03:45 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
As the 'if' part is a no brainer, I would concentrate on your find. Are we able to assume that prior to storing in a variable the find, from within a script, is returning the desired values?
Code:
#!/bin/bash
directory=/path/to/search
temp=/var/tmp/file_ferret.tmp
while read line
do
find $directory -type f | grep $line
done < $temp
If we are saying the above works 100% of the time, then we can add:
Code:
#!/bin/bash
directory=/path/to/search
temp=/var/tmp/file_ferret.tmp
while read line
do
find_result=$(find $directory -type f | grep $line)
echo "$find_result"
done < $temp
Does this work??
|
|
|
07-29-2012, 04:35 AM
|
#9
|
LQ Newbie
Registered: May 2012
Posts: 11
Original Poster
Rep:
|
@ grail - I tested the $find_result on it's own and it returned just blank lines - it echoes to the screen ok, it's just that the contents of what it echoes is blank. It even does the carriage return after each time through the loop. When executed the script prints this to screen:
Quote:
# script start #
[root@dfcio1 File_ferret_V2]# ./File_Ferret_08_characters.sh
Please enter the EDL with path:
./TEST_EDL.edl
Please enter directory to search:
/mnt/ol03_nfs/media/
Searching...
[root@dfcio1 File_ferret_V2]#
# script end (where I terminated the command) #
|
@ divyashree - your example returned the same result as the original script.
eg,
Quote:
Searching...
A066C017 FOUND
|
thanks.
|
|
|
07-29-2012, 05:48 AM
|
#10
|
LQ Newbie
Registered: May 2012
Posts: 11
Original Poster
Rep:
|
MY APOLOGIES!! I think I may have wasted your time.
* Please ignore my previous post.*
Both the double and single bracketed ways are setting the variable perfectly. Although, the double brackets were not needed.
I'm now getting exactly what I was after:
A066C017 FOUND IN /mnt/ol03_nfs/media/A066C017_test.file
Embarrassing to say, but I was interpreting things in reverse. When it was saying "FOUND" it should have been saying "NOT FOUND" - hence there was no result in the $find variable to display on screen.
...I have more than earned the title noob.
Thanks a bunch for the help though - apreciated.
|
|
|
07-29-2012, 11:32 AM
|
#11
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
Hey, at least you got there in the end Please mark as SOLVED once you have a solution
|
|
|
All times are GMT -5. The time now is 10:11 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|