Comparing two strings those are outputs of echo


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing two strings those are outputs of echo
# 1  
Old 03-20-2009
Question Comparing two strings those are outputs of echo

Hello,

I have two strings those are outputs of two echo commands:

Code:
$ logslist=`ls -1rt vp_lb_indv*.log | tail -1`; tail -7 $logslist | head -1          
ERROR: Errors printed on pages 5,22.
$ logslist=`ls -1rt vp_lb_indv*.log | tail -1`; errpages=`tail -7 $logslist | head -1`; echo $errpages
ERROR: Errors printed on pages 5,22.
$

Now, I expect following command returns 1:

Code:
$ logslist=`ls -1rt vp_lb_indv*.log | tail -1`; errpages=`tail -7 $logslist | head -1`; if [ '`echo $errpages`' = '`echo ERROR: Errors printed on pages 5,22.`' ]; then expr 1 / 1; else expr 1 / 0; fi
expr: division by zero
$

But it produces the devision by zero error, which I expect only when two strings are not equal.

I need a command/script to produce an OS error if the line I purged from a specific log file is not equal to a specific string.

Thank you very much,

Last edited by Yogesh Sawant; 03-25-2009 at 05:28 AM.. Reason: added code tags
# 2  
Old 03-20-2009
You dont need all the extra "echo" commands for one thing. Also, you need to use double quotes. The only time you should not use double quotes in the shell, are when yo uhave special characters that you mean to interpret literally. Such as if i want to say:

Code:
$ echo '$var is not set'
$var is not set

Your code:

Code:
if [ '`echo $errpages`' = '`echo ERROR: Errors printed on pages 5,22.`' ]

SHOULD be:

Code:
if [ "$errpages" = "ERROR: Errors printed on pages 5,22." ]

notice how we got rid of the "echo" commands, and put everything in DOUBLE quotes....the variable, and the string you are comparing the variable to.

This should work for you now.
# 3  
Old 03-20-2009
I modified the code, but it still gives the devision by zero error Smilie
# 4  
Old 03-23-2009
MySQL Purging Log Files

I couldn't find why the above comparision fails, but I found the way that I need:

Code:
logslist=`ls -1rt vp_lb_indv*.log | tail -1`; errpages=`grep -c 'ERROR:' $logslist`; if [ $errpages = 5 ]; then expr 1 / 1; else expr 1 / 0; fi

If someone find why, please share with us Smilie

Last edited by Yogesh Sawant; 03-25-2009 at 05:25 AM.. Reason: added code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Comparing wc outputs using array

Hi All, Im trying to compare the wc -l output with another set of rowcount outputs which returned from sql... For Eg : Im storing the first outputs as below <srccnt=`wc -l $HOME/*.csv | awk {'print $1'}` and comparing this with the another set of outputs. descnt=`seclect count(*)... (7 Replies)
Discussion started by: Deena1984
7 Replies

2. Shell Programming and Scripting

"echo" outputs not appear into Log file

Hi, - From Script : echo "************************************" echo "NEW_ORACLE_SID : " ${NEW_ORACLE_SID} echo "************************************" echo "" >> /auto_clone/${NEW_ORACLE_SID}/logs/clone_PERP12R.log echo... (4 Replies)
Discussion started by: htaieb1
4 Replies

3. Shell Programming and Scripting

comparing strings as ints

Hi, So I got his code below. $year is a string of 2010,2011 etc. I guess I want to convert $year to an integer so I can do my if statement to see if the year string is greater than 2010? Or how could I do this? Right now I get a syntax error doing this. if; then do stuff fi (2 Replies)
Discussion started by: vsekvsek
2 Replies

4. Shell Programming and Scripting

Help with Comparing 2 strings from text

Hey guys how do I compare 2 strings from the text file, and check for duplication? For example, I add an item call Laptop, it will record to the textfile call file. If it detects duplicate it will say the record record exist? file.txt contains Laptop:Sony:1000 Phone:Apple:30 A head... (4 Replies)
Discussion started by: aLHaNz
4 Replies

5. Shell Programming and Scripting

comparing two strings

hi All i am facing prob in comparing two strings that have two word. below is the code snippet. checkValidates="file validates" file3_name="file" if then echo "file" $file3_name "is validated successfully" fi when i run this i get the error as -bash: [: too many arguments ... (1 Reply)
Discussion started by: infyanurag
1 Replies

6. Shell Programming and Scripting

comparing 2 strings

hi i have 2 strings. i want to compare the strings. please help (2 Replies)
Discussion started by: satish@123
2 Replies

7. Shell Programming and Scripting

comparing strings

i have a string in a file which gets repeated number of times like below: rpttxt("abc") . . rpttxt("REP_TITLE") rpttxt("BOS_TITLE") . . . . and so on using awk or grep how can i comapre the string( as the second half keeps varying) and store it in a temporary variable? I am using the... (3 Replies)
Discussion started by: agarwal
3 Replies

8. Shell Programming and Scripting

Comparing Two Strings

Hi All, While I am trying to run below code I Am getting the exception like ./abs.sh: line 102: syntax error near unexpected token `then' ./abs.sh: line 102: ` then' The Code Snippet is: if then cat $file1 | sed -e... (8 Replies)
Discussion started by: Anji
8 Replies

9. UNIX for Advanced & Expert Users

Comparing strings

I have two strings a=Mar22 b=may21 how can I compare them Is this fine if then; . ... else .... fi or if then (2 Replies)
Discussion started by: yakyaj
2 Replies

10. Shell Programming and Scripting

comparing two strings

Hi How do i compare two strings in shell script. Below is an example but I am not getting the desired output, plz help if then echo success fi I am not getting the desired output if I do this. plz help (24 Replies)
Discussion started by: ragha81
24 Replies
Login or Register to Ask a Question