LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-14-2009, 02:45 PM   #1
praveen_218
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Rep: Reputation: 0
Bash script is enterpreting $1, $2 values in awk script ...


For the following code:
Code:
awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1'  shadow2 shadow1 > shadow3
I get syntex error as below:
Code:
awk: NR==FNR{a[]=;next}a[]{=a[]}1
awk:           ^ syntax error
awk: fatal: invalid subscript expression
On investigation, I noticed that when I create a bash script using the HERE-TAG(from within another bash script) ; in the following manner:

Code:
#!/usr/bin/env bash
# Bash_script001.sh

   echo " Inside Bash_script001.sh";
   ...
   ...
   ...

cat>>"Bash_script002.sh"<<EOF
#!/usr/bin/env bash
# Bash_script002.sh

   echo " Inside Bash_script002.sh";
   ...

   awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1' shadow2 shadow1 > shadow3;
   ...

   ...

EOF

   ....
   echo " Again inside Bash_script001.sh processing rest of Bash_script001.sh ";
Now when the 'Bash_script002.sh' gets created and as it contains the awk script which inturns contains variables $1, $2 as array a[] indexes; the resultant 'Bash_script002.sh' contains only the following:

Code:
awk -F":" -v OFS=":" 'NR==FNR{a[]=;next}a[]{=a[]}1' shadow2 shadow1 > shadow3;
Thats why I'm receiving the syntex error.

Will you please suggest, what can be done to avoid getting the above erronious 'awk' statement in the resultant 'Bash_script002.sh' ?

Thanks in anticiption.
 
Old 09-14-2009, 02:53 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by praveen_218 View Post
For the following code:
Code:
awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1'  shadow2 shadow1 > shadow3
The problem is the single quotes that make bash treat the whole string as literal and not substitute the values of $1, $2 etc. Try this
Code:
awk -F':' -v OFS=':' "NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1" shadow2 shadow1 > shadow3
I'm a bit rusty on awk so that may not work perfectly but it should move you forward.
 
Old 09-14-2009, 03:07 PM   #3
praveen_218
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by catkin View Post
The problem is the single quotes that make bash treat the whole string as literal and not substitute the values of $1, $2 etc. Try this
Code:
awk -F':' -v OFS=':' "NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1" shadow2 shadow1 > shadow3
I'm a bit rusty on awk so that may not work perfectly but it should move you forward.
The problem I have is that bash is enterpreting for $1 and $2 which I don't want.

What I want is the script Bash_script002.sh should contain the following only:
Code:
awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1' shadow2 shadow1 > shadow3;
So that this script gets executed properly when called for execution.

Your solution still produces the same problem.

Virtuall, I need bash to actually take the string under the single quotes as literal but unfortunately its not for some reason.

I'm still looking out for a working solution to my problem.

Last edited by praveen_218; 09-14-2009 at 03:08 PM.
 
Old 09-14-2009, 03:23 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by praveen_218 View Post
The problem I have is that bash is enterpreting for $1 and $2 which I don't want.

What I want is the script Bash_script002.sh should contain the following only:
Code:
awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1' shadow2 shadow1 > shadow3;
Gotcha! Try
Code:
cat>>"Bash_script002.sh"<<'EOF'
See GNU Bash Reference Manual on Here Documents for details.
 
Old 09-14-2009, 03:38 PM   #5
praveen_218
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by catkin View Post
Gotcha! Try
Code:
cat>>"Bash_script002.sh"<<'EOF'
See GNU Bash Reference Manual on Here Documents for details.

I'm extremely thankful to you catkin.
This worked.
 
  


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
Script: how to assign values to variables using "awk" results? JZL240I-U Linux - Software 3 11-18-2008 12:59 AM
Problem with AWK in a BASH script geek_man Programming 4 01-20-2008 07:16 PM
bash script--variables have unexpected values on invoking mplayer stairwayoflight Programming 5 07-08-2007 11:17 AM
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 06:07 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM


All times are GMT -5. The time now is 08:59 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration