What"s wrong with this "IF" statement

What"s wrong with this "IF" statement

am 09.10.2007 20:02:08 von mcbalagtas

I can't figure out why the "IF" statement is not "True".




conf.tmp has the following:

Digital Signature is consistent with file.

=============================
echo Digital Signature verification:
DIGITALSIGNATURE.UTIL -VERIFY=$config > conf.tmp
cat conf.tmp
pass="$(awk '{ print $4 ;}' conf.tmp)"
echo $pass

if [ "$pass" = "consistent" ]; then
echo "Digital Signature is good and being sent"
echo "restart via SYMOP to activate changes"
# SUXEQ PUTCFG
else
echo "Digital Signature has failed"
fi
===================================

Output is:


Digital Signature is consistent with file.

consistent

Digital Signature has failed

Re: What"s wrong with this "IF" statement

am 09.10.2007 20:32:49 von cfajohnson

On 2007-10-09, mcbalagtas@yahoo.com wrote:
>
> I can't figure out why the "IF" statement is not "True".
>
> conf.tmp has the following:
>
> Digital Signature is consistent with file.
>
> =============================
> echo Digital Signature verification:
> DIGITALSIGNATURE.UTIL -VERIFY=$config > conf.tmp
> cat conf.tmp
> pass="$(awk '{ print $4 ;}' conf.tmp)"

You don't need awk:

read a b c pass out < conf.tmp

> echo $pass
>
> if [ "$pass" = "consistent" ]; then
> echo "Digital Signature is good and being sent"
> echo "restart via SYMOP to activate changes"
> # SUXEQ PUTCFG
> else
> echo "Digital Signature has failed"
> fi
> ===================================
>
> Output is:
>
>
> Digital Signature is consistent with file.
>
> consistent
>
> Digital Signature has failed

It works correctly for me. Is that _exactly_ what you ran?

Is there more than one line in conf.tmp? If so, the replacement
line I posted above will solve the problem.

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: What"s wrong with this "IF" statement

am 09.10.2007 20:44:10 von Bill Marcum

On 2007-10-09, mcbalagtas@yahoo.com wrote:
> I can't figure out why the "IF" statement is not "True".
>
>
>
>
> conf.tmp has the following:
>
> Digital Signature is consistent with file.
>
>=============================
> echo Digital Signature verification:
> DIGITALSIGNATURE.UTIL -VERIFY=$config > conf.tmp
> cat conf.tmp
> pass="$(awk '{ print $4 ;}' conf.tmp)"
> echo $pass
>
> if [ "$pass" = "consistent" ]; then
> echo "Digital Signature is good and being sent"
> echo "restart via SYMOP to activate changes"
> # SUXEQ PUTCFG
> else
> echo "Digital Signature has failed"
> fi
>===================================
>
> Output is:
>
>
> Digital Signature is consistent with file.
>
> consistent
>
> Digital Signature has failed
>
If conf.tmp contains blank lines, "$pass" could contain one or more
newlines. You could change it to
pass="$(awk 'NF>=4 {print $4; exit}' conf.tmp)"

Re: What"s wrong with this "IF" statement

am 09.10.2007 21:03:58 von Martin Krischik

mcbalagtas@yahoo.com wrote:

> echo $pass

For starters I would suggest that you use

echo "-${pass}-"

to see if extra spaces are in ${pass}

Martin

--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com

Re: What"s wrong with this "IF" statement

am 09.10.2007 22:47:39 von mcbalagtas

On Oct 9, 11:44 am, Bill Marcum wrote:
> On 2007-10-09, mcbalag...@yahoo.com wrote:
>
> > I can't figure out why the "IF" statement is not "True".
>
> > conf.tmp has the following:
>
> > Digital Signature is consistent with file.
>
> >=============================
> > echo Digital Signature verification:
> > DIGITALSIGNATURE.UTIL -VERIFY=$config > conf.tmp
> > cat conf.tmp
> > pass="$(awk '{ print $4 ;}' conf.tmp)"
> > echo $pass
>
> > if [ "$pass" = "consistent" ]; then
> > echo "Digital Signature is good and being sent"
> > echo "restart via SYMOP to activate changes"
> > # SUXEQ PUTCFG
> > else
> > echo "Digital Signature has failed"
> > fi
> >===================================
>
> > Output is:
>
> > Digital Signature is consistent with file.
>
> > consistent
>
> > Digital Signature has failed
>
> If conf.tmp contains blank lines, "$pass" could contain one or more
> newlines. You could change it to
> pass="$(awk 'NF>=4 {print $4; exit}' conf.tmp)"

Bill,

It worked, but I'm not sure why.

NF=is the number of fields in the current line...right? I don't
understand how if NF is >= to 4 removes the spaces.

Thanks.

Re: What"s wrong with this "IF" statement

am 09.10.2007 23:13:13 von Bill Marcum

On 2007-10-09, mcbalagtas@yahoo.com wrote:
> On Oct 9, 11:44 am, Bill Marcum wrote:
>> On 2007-10-09, mcbalag...@yahoo.com wrote:
>>
>> > I can't figure out why the "IF" statement is not "True".
>>
>> > conf.tmp has the following:
>>
>> > Digital Signature is consistent with file.
>>
>> >=============================
>> > echo Digital Signature verification:
>> > DIGITALSIGNATURE.UTIL -VERIFY=$config > conf.tmp
>> > cat conf.tmp
>> > pass="$(awk '{ print $4 ;}' conf.tmp)"
>> > echo $pass
>>
>> > if [ "$pass" = "consistent" ]; then
>> > echo "Digital Signature is good and being sent"
>> > echo "restart via SYMOP to activate changes"
>> > # SUXEQ PUTCFG
>> > else
>> > echo "Digital Signature has failed"
>> > fi
>> >===================================
>>
>> > Output is:
>>
>> > Digital Signature is consistent with file.
>>
>> > consistent
>>
>> > Digital Signature has failed
>>
>> If conf.tmp contains blank lines, "$pass" could contain one or more
>> newlines. You could change it to
>> pass="$(awk 'NF>=4 {print $4; exit}' conf.tmp)"
>
> Bill,
>
> It worked, but I'm not sure why.
>
> NF=is the number of fields in the current line...right? I don't
> understand how if NF is >= to 4 removes the spaces.
>
'NF>=4{print $4}' prints the fourth field only if there is a fourth
field. Without the NF>=4, if the input file contains blank lines,
'{print $4}' would be the same as '{print ""}' which would print a
newline.

Re: What"s wrong with this "IF" statement

am 10.10.2007 12:17:06 von gazelle

In article <1191962859.076225.97740@g4g2000hsf.googlegroups.com>,
mcbalagtas@yahoo.com wrote:
....
>NF=is the number of fields in the current line...right? I don't
>understand how if NF is >= to 4 removes the spaces.

It is the printing of $4 (i.e., just the 4th field) that "removes the spaces".
The NF is to choose only lines that *have* a fourth field.

Note that you can shave a few strokes off your game thusly:

$4 { print $4 }

Re: What"s wrong with this "IF" statement

am 10.10.2007 12:35:09 von Stephane CHAZELAS

2007-10-10, 10:17(+00), Kenny McCormack:
> In article <1191962859.076225.97740@g4g2000hsf.googlegroups.com>,
> mcbalagtas@yahoo.com wrote:
> ...
>>NF=is the number of fields in the current line...right? I don't
>>understand how if NF is >= to 4 removes the spaces.
>
> It is the printing of $4 (i.e., just the 4th field) that "removes the spaces".
> The NF is to choose only lines that *have* a fourth field.
>
> Note that you can shave a few strokes off your game thusly:
>
> $4 { print $4 }

Which does not do the same thing if $4 is "0" or "-0", or "00"...

--
Stéphane

Re: What"s wrong with this "IF" statement

am 12.10.2007 02:11:29 von gazelle

In article ,
Stephane CHAZELAS wrote:
>2007-10-10, 10:17(+00), Kenny McCormack:
>> In article <1191962859.076225.97740@g4g2000hsf.googlegroups.com>,
>> mcbalagtas@yahoo.com wrote:
>> ...
>>>NF=is the number of fields in the current line...right? I don't
>>>understand how if NF is >= to 4 removes the spaces.
>>
>> It is the printing of $4 (i.e., just the 4th field) that "removes the
>> spaces". The NF is to choose only lines that *have* a fourth field.
>>
>> Note that you can shave a few strokes off your game thusly:
>>
>> $4 { print $4 }
>
>Which does not do the same thing if $4 is "0" or "-0", or "00"...

And, in other news, water is wet and Francisco Franco - still dead.

But, back to the subject, its close enough.