variable getting results from a log file; compare it in an IF cycle


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable getting results from a log file; compare it in an IF cycle
# 1  
Old 08-25-2010
variable getting results from a log file; compare it in an IF cycle

Hi All

I am going crazy trying to resolve this easy task....

I have a log file that is created by xcode; and it contains the results of a build (either success or fail); what i am trying to achieve here is to get the result from the log, and check if is a pass or fail in an IF statement, so if it pass i write in anotehr log the build number, the date and the pass, otherwise i write in the log the fail and also send an email to myself.

the part that is causing me troubles is the extrapolation of the results and the comparison in the IF cycle:

-i tried to grep for the word FAIL in the logs, and save the results in a variable, but this does not work all the times, since i get from grep the entire line where the failure happens, while i just would like to have a bool that tells me "yes, the log has the word FAIL" or "no, the log has no FAIL" (in which case i consider it as pass).

-when i do the if statement; the result is not saved in another variable (it gets empty)

This is what i have so far in code:

checking if the logs has FAIL or PASS
Code:
IS_FAILED=`grep -r 'FAILED' ~/build_results.txt`

IF cycle
Code:
if [ -ne $IS_FAILED ]; then
        RESULT='pass'
        echo "is a pass"
else
        RESULT='fail'
        echo "is a fail"
fi

of course this won't work; and i don't get why the RESULT variable is empty, even if the echo prints the pass or fail; and i do not get how can i take just the "FAILED" string and compare it with "FAILED" in the IF cycle, instead of doing just a check to see if is empty or not (prone to errors)

is there any other way to achieve this result? I would really appreciate any suggestion.

Thanks
# 2  
Old 08-25-2010
try

Code:
if [ ! $IS_FAILED ]; then
        RESULT='pass'
        echo "is a pass"
else
        RESULT='fail'
        echo "is a fail"
fi

or

Code:
if [ $IS_FAILED = "FAILED" ]; then
        RESULT='fail'
        echo "is a fail"  
else
       RESULT='pass'
        echo "is a pass"
fi

# 3  
Old 08-25-2010
If all you care about is whether or not there is a match, you do not need to capture the output of grep and then compare it; you can simply test its exit status directly:
Code:
if grep -qr FAILED ~/build_results.txt; then
      RESULT='fail'
else
      RESULT='pass'
fi
echo is a $RESULT

Or, perhaps, more optimistically:
Code:
result=success
grep -qr FAILED ~/build_results.txt && result=fail
echo is a $result

Regards,
Alister

Last edited by alister; 08-25-2010 at 06:39 PM..
# 4  
Old 08-25-2010
Thanks for the reply!

BTW the outcome of the grep is not "FAILED" but "** BUILD FAILED **" so i had to change the comparison string, and it works fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies

2. Shell Programming and Scripting

awk to compare each file in two directores by storing in variable

In the below bash I am trying to read each file from a specific directory into a variable REF or VAL. Then use those variables in an awk to compare each matching file from REF and VAL. The filenames in the REF are different then in the VAL, but have a common id up until the _ I know the awk portion... (15 Replies)
Discussion started by: cmccabe
15 Replies

3. Shell Programming and Scripting

Unable to compare to a previous value of a variable in a while loop for a file

Hello All, I'm working on a script that will actually read a file consisting of data like the below:(ReportID,Sub_reportID,Sub_reportName) 1,1,ABC 1,2,DEF 1,3,GHI 2,1,JKL 2,2,MNO 3,1,PQR I want to read the Sub Report details for a Report_ID using while loop and write these values into... (6 Replies)
Discussion started by: venkat_reddy
6 Replies

4. Shell Programming and Scripting

compare text in log file

Hi all, I am posting the thread similar to previous posts but here my scenario is i need to know what the files size from start date. Here end time file size will be 0. Also need to know how much time does it took to complete in seconds. log file: Name1 START 11:36:45 ... (5 Replies)
Discussion started by: Olivia
5 Replies

5. Shell Programming and Scripting

Adding grep'd results in a variable

Here is one I am baffled with; I have not used unix for a while and now that I am back it has been fun remembering and I have enjoyed it, for the most past. this is in ksh. I need to search in a file for the line with X1 and cut columns 20-25, put them into a variable, added them (dollar... (3 Replies)
Discussion started by: CougarMutt
3 Replies

6. Shell Programming and Scripting

Shell script compare all parameters in two files and display results

Hi , I am not familiar with shell programming. I have a requirement like i have two files .I need to compare the two files by comparing each parameter and i should produce 2 outputs. 1)i have around 35 parameters say i have one parameter name called db_name=dcap in one file and... (7 Replies)
Discussion started by: muraliinfy04
7 Replies

7. Shell Programming and Scripting

KSH: Compare variable to $1 in an input file

Hello, I am working with KSH on AIX and I have 2 files generated from different sources... as seen below: FILE1 FILE2 AAA AAA@ABS0001C BBB BBB@ABS0003D CCC CCC@ABS0023A DDD DDD@ABC0145D EEE EEE@ABS0090A FFF FFF@ABS0002A GGG GGG@ABC0150D HHH FILE1 is main main data source,... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. UNIX for Dummies Questions & Answers

how to - redirect query results to a variable

How can I send the results of a query to a unix variable. I basically want to run a query then do some logic on the results. Trying to redirect the result into a variable I define in the script. select count(*) as counter from table - nut to redirect the "count" returned from the query... (2 Replies)
Discussion started by: rstone
2 Replies

9. Shell Programming and Scripting

Compare Array results

Hi, In a kshell , i need to compare the results of two array . each Non-match value should copy to a new array. For example: ========== Array one contain the following values: A B C Array two contain the following values: C F A E After comparing this arrays , a new array should... (4 Replies)
Discussion started by: yoavbe
4 Replies

10. Windows & DOS: Issues & Discussions

Need solution for Bat FIle using FOR Cycle

Hi people. Don't know if you could help me, but here it goes anyway. I need to search for some directories, and if i for any file founded a must run another Bat file. Can this be done in DOS ? Thanks in advance (1 Reply)
Discussion started by: osramos
1 Replies
Login or Register to Ask a Question