LinuxQuestions.org
Visit Jeremy's Blog.
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 07-27-2012, 02:16 AM   #1
Leath
LQ Newbie
 
Registered: May 2012
Posts: 11

Rep: Reputation: Disabled
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
 
Old 07-27-2012, 06:57 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
if [ $? -eq 0 ] - then $line "FOUND" $find_result, I think

or change your -eq to -ne and try again.
 
Old 07-27-2012, 11:49 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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.
Old 07-27-2012, 09:52 PM   #4
Leath
LQ Newbie
 
Registered: May 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-28-2012, 12:49 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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.
 
Old 07-28-2012, 01:18 AM   #6
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
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.
 
Old 07-28-2012, 02:45 AM   #7
Leath
LQ Newbie
 
Registered: May 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-28-2012, 03:45 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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??
 
Old 07-29-2012, 04:35 AM   #9
Leath
LQ Newbie
 
Registered: May 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
@ 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.
 
Old 07-29-2012, 05:48 AM   #10
Leath
LQ Newbie
 
Registered: May 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-29-2012, 11:32 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Hey, at least you got there in the end Please mark as SOLVED once you have a solution
 
  


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
variable incrementation in for loop ++nick++ Linux - General 3 10-20-2011 03:03 PM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 06:50 PM
[SOLVED] variable is gone at the end of while loop ted_chou12 Programming 9 03-28-2011 04:57 AM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 10:57 AM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 05:43 AM

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

All times are GMT -5. The time now is 05:15 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration