Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple thing .. just can't get it to work! 'if statement' 2

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi All,

Wasted enough time on this so hopefully someone can tell where I'm going wrong here. Such a simple problem, but it's doing my head in!
I'm catching some data into variable VAR, looking to see if it contains (fc1) .. if so echo string and do something else if matches it.

I've tried both a 'for SRCH in file', and a 'while read SRCH' method here, both giving me headaches.
Main code:

Code:
VAR=`grep file.txt $SRCH | awk '{print $NF}'`

 echo "var - $VAR"         # Test line
 #I even tried the bellow to strip the () and any blanks I can't see off in case that was the problem:
 BVAR=`echo "$VAR" | sed -s 's/(//' | sed -s 's/)//' | sed -s 's/ //' `
 echo "bvar - $BVAR"       # Test line
  if [ $BVAR = "fc1" ]     # Or without the sed stripping above, if it sees (fc1) then do whatever next
   then
    echo "Working!"
    VAR=`dosomething_else`
  fi
 echo "$SRCH,$VAR"

Output:
Code:
var - (fc1)
bvar - fc1
LINE1,(fc1)
var - (fc1)
bvar - fc1
LINE2,(fc1)
var - (fc1)
bvar - fc1
LINE3,(fc1)
var - (fc1)
bvar - fc1
LINE4,(fc1)

Hope the above is clear enough. Will try pick up any advise you offer on the weekend or Monday even.

Thanks!
 
Oh, I should add, I have tried variations of this too:

Code:
if [ $BVAR = "fc1" ]
if [ "$BVAR" = "fc1" ]
if [ $BVAR = fc1 ]

All same issue.
 
What shell are you using? It can make a difference.

Those "if" commands seem to work in bash, ksh, and sh for me.

Also, your "output" doesn't match the code sample, so I assume there's more to it. It could help to see everything. Or at least a larger functioning snippet.

Maybe this?

Code:
#!/bin/ksh

VAR=$(grep file.txt $SRCH | awk '{print $NF}')

if [[ ${VAR} == $(*fc1*) ]]
then
    print "Working!"
    VAR=$(do_something)
fi

print "$SRCH,$VAR"

The structure in the "if" is a Korn shell pattern match. It's testing if "fc1" is anywhere in VAR.

 
Shouldn't it be...
Code:
VAR=`grep [u]$SRCH file.txt[/u] | awk '{print $NF}'`
...and not...
Code:
VAR=`grep [u]file.txt $SRCH[/u] | awk '{print $NF}'`

Please post actual code. It really does help.


 
Sorry SamBones, but you're quite right in spotting that .. I was typing it out to keep it simple instead of copy and paste the mess I've chucked together [tongue]

I'm using bash, got 100's of similar scripts working fine .. just this one has me stumped. I'll get back on the system this weekend and have a look as you may have a point .. I might not have included the #!/bin/bash in it.

Thanks!
 
UPDATE: Thought I best come back and mention that I only found time to work on this one again today. Took me a few minutes to see the problem, only once I vi'd my source data and noticed that at every line with a (fc1) ended with the mangey (ctrl-v M) ^M character. [evil]
A quick sed replace on the source build script fixed everything! Doh! [dazed]
 
Also look for a utility named something like "[tt]dos2unix[/tt]". That will clean up files like that with a single command.



 
Anyway, why using grep ?
Code:
VAR=`awk "/$SRCH/{print \$NF}" file.txt`

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
@SamBones yeah, I've used the dos2unix on occasion, but 99% of the time I never need it. This one caught me out once again lol

@PHV, man, you're still kicking about! :) ... Nice one.. I didn't know about that awk search feature, thanks for that. I'll give it a try and probably find it improves the speed of all my scripts at the same time. Wish I had 10% the knowledge you have stored up there hehe [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top