LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2014, 04:31 AM   #1
sdhanawade
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Rep: Reputation: Disabled
#awk If else loop not working in ksh


Hi,

I have code where awk is piped to a clearcase command where If else loop is not working.

code is below :
====================================================
#!/bin/ksh

export dst_region=$1

cleartool lsview -l -region "DEF" | gawk -F":" \
'{
if ($0 ~ /Global path:/) {
if($dst_region == "ABC" || $dst_region -eq "ABC")
{
system("echo dest_region is ABC");
}
else
{
system("echo dest_region is not ABC");
}

};
}'

====================================================

but when I execute the above script the I get incorrect output,
====================================================
$ ksh script.sh ABCD
dest_region is ABC

$ ksh script.sh ABC
dest_region is ABC
====================================================

Could anyone please help on this issue ?

thank in advance !

regards,
Shreedhar
 
Old 09-10-2014, 05:19 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
I do not think it is related to ksh. You are not allowed to use shell variables inside the awk script. awk cannot see, cannot handle the variables of the shell:
Code:
#!/bin/ksh

export dst_region=$1

cleartool lsview -l -region "DEF" | gawk -F":" \
'{
  if ($0 ~ /Global path:/)
  {
    if($dst_region == "ABC" || $dst_region -eq "ABC")
    {
      system("echo dest_region is ABC");
    }
    else
    {
      system("echo dest_region is not ABC");
    }
  };
}'
Code:
-- You can do the following:
gawk -v dst_region="$dst_region" ' { ....
-- also there is no -eq in awk
if(dst_region == "ABC")
-- also do not use system("echo ...") but print:
print "echo dest_region is not ABC"
 
1 members found this post helpful.
Old 09-19-2014, 04:17 AM   #3
sdhanawade
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
hi Pan64,

Yes, It was the variable issue.
As you said the shell variables are not available in "gawk" command. That solved my issue.

thank you ![COLOR="Silver"]

---------- Post added 09-19-14 at 03:17 AM ----------

Last edited by sdhanawade; 09-19-2014 at 04:19 AM. Reason: ..
 
Old 09-19-2014, 04:22 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
glad to help you. please mark the thread solved if you think your problem is now solved. thanks.
 
Old 09-19-2014, 11:19 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Also, since dst_region is in the environment, you could use
Code:
if(ENVIRON["dst_region"] == "ABC")
Note also the "-eq" has no special meaning to awk and will be taken as the negative of the value of variable "eq" (which will be created and set to zero if not previously used). What that actually does in the context of your expression is an interesting study, but suffice to say it makes your overall expression always true.
 
Old 09-20-2014, 05:16 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by rknichols View Post
Also, since dst_region is in the environment, you could use
Code:
if(ENVIRON["dst_region"] == "ABC")
Note also the "-eq" has no special meaning to awk and will be taken as the negative of the value of variable "eq" (which will be created and set to zero if not previously used). What that actually does in the context of your expression is an interesting study, but suffice to say it makes your overall expression always true.
Only works if the variable is exported...
 
Old 09-20-2014, 07:50 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by jpollard View Post
Only works if the variable is exported...
The original post contained the line,
Code:
export dst_region=$1
at the beginning of the script.

[Remark suppressed.]
 
Old 09-20-2014, 09:01 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
sorry - missed it.
 
Old 09-21-2014, 02:28 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 805

Rep: Reputation: 186Reputation: 186
Hi shreedhar. (OP).

awk has a -v option (AFAIR) that allows you to pass in shell environment to awk.

So if you changed
Quote:
cleartool lsview -l -region "DEF" | gawk -F":" \
to
Quote:
cleartool lsview -l -region "DEF" | gawk -F":" -v dst_region=$dst_region \
, you could run the rest of the awk code unchanged.

Note that -v dst_region=$dst_region could well become -v regn=$dst_region but you would have to change your awk code suitably.

OK
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] using for loop in awk ilukacevic Programming 6 03-29-2011 03:48 PM
Need help stripping statement from text file, ksh: sed awk? austin881 Programming 7 07-13-2009 05:31 PM
ksh assignment with awk ShaqDiesel Programming 4 01-15-2007 08:28 AM
awk in loop How to Nkunzis Linux - Newbie 3 12-10-2006 02:34 PM
ksh and while..do loop problem Harry Seldon Programming 1 04-17-2006 05:18 PM


All times are GMT -5. The time now is 08:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration