LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   output to a variable in while loop (https://www.linuxquestions.org/questions/linux-newbie-8/output-to-a-variable-in-while-loop-4175418894/)

Leath 07-27-2012 01:16 AM

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

divyashree 07-27-2012 05:57 AM

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

or change your -eq to -ne and try again.

grail 07-27-2012 10:49 AM

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

Leath 07-27-2012 08:52 PM

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

grail 07-27-2012 11:49 PM

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.

divyashree 07-28-2012 12:18 AM

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 ??

Leath 07-28-2012 01:45 AM

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

grail 07-28-2012 02:45 AM

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??

Leath 07-29-2012 03:35 AM

@ 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.

Leath 07-29-2012 04:48 AM

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.

grail 07-29-2012 10:32 AM

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 05:26 PM.