Computing.Net > Forums > Unix > IF condition after substr

IF condition after substr

Reply to Message Icon

Original Message
Name: Nish
Date: October 11, 2002 at 18:09:55 Pacific
Subject: IF condition after substr
OS: Solaris
CPU/Ram: 256
Comment:

All,
I have extracted some data using awk substr function.
myvar = substr ($0,300,5)
Then I want to test this myvar whether it is equal to 000.
I tried
if [ myvar = 000]
then print xxx
fi.
It didn't work. Do I have to convert this text into int value ?
Pls Help
Nish.


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: October 12, 2002 at 08:01:14 Pacific
Subject: IF condition after substr
Reply: (edit)

I apologize in advance for the verbose answer, but you will see that there are a lot of things to consider.

Your code, as posted, has some syntax errors ...

While the assignment to myvar is done without the dollar-sign (as you have shown), all other references to it must start with dollar-sign.

The right bracket needs a preceding space.

The "fi" should not be followed by a period.

Also, any time that a variable could be zero length or all spaces, your variable references should be enclosed in double-quotes as a place-holder so as not to produce a syntax error. As an example, the statement:

if [ $myvar = 0 ]

could end up looking like:

if [ = 0 ]

which is now lacking a test argument, but enclosing in quotes keeps it valid syntax:

if [ "" = 0 ]

Now we need to talk about the actual test. You did not show how you captured myvar, and it makes a big difference. I will discuss the following three ways:

1. myvar=`awk ... `
2. awk ... | read myvar
3. awk ... | read myvar morewords

#1 will capture the full 5 characters pulled by substr, including any leading, embedded and trailing spaces. This web page will condense multiple consecutive spaces down to a single space. That first entry is really one zero followed by four spaces:

"0 "
"00000"
"000 "
" 000 "
" 0 0 "

#2 approach will discard any leading or trailing spaces, but keeps embedded spaces, so you would capture:

"0"
"00000"
"000"
"000"
"0 0"

#3 will capture the first space-delimited word only, so you would capture:

"0"
"00000"
"000"
"000"
"0"

Now consider these two types of test:

if [ "$myvar" = 000 ]
if [ "$myvar" -eq 0 ]

The first test is an alphanumeric test, and will test true only if precisely the same, including leading and trailing spaces. For example, if you captured "000 " in $myvar, to test true you would have to do:

if [ "$myvar" = "000 " ]

But a numeric test will test true whether $myvar is 0 or 00 or 000 etc:

if [ "$myvar" -eq 0 ]

But while this gives you the convenience of a numeric equivalence test, this test is more sensitive to syntax. If $myvar is more than one word, such as "0 0", it will see it as:

if [ 0 0 -eq 0 ]

and you will get "syntax error". And if $myvar contains a non-digit, you will get "bad number".

Before testing with -eq, if you want to ensure that $myvar is all digits (one or more), the following expression should show greater than zero (zero would indicate either null or non-digits):

expr "$myvar" : "[0-9]*$"

and if null is OK, following expression should show greater than zero for either null or all digits (zero would indicate non-digits):

expr "9$myvar" : "[0-9]*$"


Report Offensive Follow Up For Removal

Response Number 2
Name: beer
Date: October 12, 2002 at 22:53:03 Pacific
Subject: IF condition after substr
Reply: (edit)

Hi, Nish

I am confused by your code, either your code like awk or sh script, I hope you can post the code, so that I can see that.

beer?


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: IF condition after substr

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software