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 Code:
temp=/var/tmp/file_ferret.tmp Thanks heaps! Leath |
if [ $? -eq 0 ] - then $line "FOUND" $find_result, I think
or change your -eq to -ne and try again. |
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 |
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 |
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.
|
Try like this
e.g. Quote:
The name of the matched files or the matched lines in the files ?? |
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 |
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 Code:
#!/bin/bash |
@ 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:
eg, Quote:
|
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. |
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:41 PM. |