INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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!

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

(OP)
Oh, I should add, I have tried variations of this too:

CODE -->

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

All same issue.

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

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.

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

Shouldn't it be...

CODE

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

CODE

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

Please post actual code. It really does help.


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

(OP)
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!

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

(OP)
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

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

Also look for a utility named something like "dos2unix". That will clean up files like that with a single command.



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

(OP)
@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! smile ... 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close