LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-25-2011, 06:07 AM   #1
danicobos
LQ Newbie
 
Registered: Oct 2011
Location: Madrid, Spain
Distribution: RHEL
Posts: 4

Rep: Reputation: Disabled
Question Awk variables


Hola everyone,

I am facing the following trouble related to the use of and internal Awk variable:

I have an awk script into a sh script. Into the awk script I assign a local variable:

host_name=substr($1,2,length($1)); (or whatever)

Then (we have not leave the awk script) I would like to use this "host_name" as argument to an external script. Of course, in order to make a system call I have to use a "system" command.

system("./install_software.sh $host_name")

Crash! "host_name" is and internal awk variable and I have no idea how to "export" it, with the purpose that the shell can read it. Someone can say: export it using another system call! I can not pass any parameter to the system command.

Any idea, of how can I solve it

Thanks in advance.
 
Old 10-25-2011, 06:15 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Your code doesn't substitute the host_name value inside the system command, since you used the shell syntax instead of the awk syntax! Try this:
Code:
system("./install_software.sh " host_name)
 
Old 10-26-2011, 08:31 AM   #3
danicobos
LQ Newbie
 
Registered: Oct 2011
Location: Madrid, Spain
Distribution: RHEL
Posts: 4

Original Poster
Rep: Reputation: Disabled
Grazie colucix, but it does not work. With:

system("./install_software.sh " host_name)

my script "install_software.sh" does not detect any argument as input, I imagine because "host_name" is not visible for system command.

Any other idea?

Regards.
 
Old 10-26-2011, 04:26 PM   #4
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
I tried with these testing scripts:
external_script
Code:
#!/bin/bash
echo "I am external script"
echo "String parameter:----$1----"
echo "end of external script"
main_script
Code:
#!/bin/bash
dummy="dummy"
awk 'BEGIN {param="Hello World!"}
    {system("./external_script \"" param "\"")}' <<< "$dummy"
The result of the command:
Code:
./main_script 
I am external script
String parameter:----Hello World!----
end of external script
So the internal param awk variable was passed correctly to the external_script.
Explanation:
in awk, string string string means concatenation, it gives stringstringstring as the result string.
so a blank is necessary at the end of the command to separate it from the parameter.
I included also " around the parameter because it contains a space (\" gives a litteral double quote).
So
"./external_script \"" param "\""
will give
./external_script "Hello World!"
which is a correct command.
 
1 members found this post helpful.
Old 10-27-2011, 05:19 AM   #5
danicobos
LQ Newbie
 
Registered: Oct 2011
Location: Madrid, Spain
Distribution: RHEL
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you berbae.

You are right! It works. But, what about if you are passing to you external script two (or more) arguments?

#!/bin/bash
dummy="dummy"
awk 'BEGIN {param1="Hello World!"} {param2="Hello Again!"}
{system("./external_script \"" param1 "\"" " param2 "\"")}' <<< "$dummy"

??? It does not work!

I'll really appreciate your help!

Regards.
 
Old 10-27-2011, 07:17 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
You need to correctly put the escaped quotes:
Code:
awk 'BEGIN {param1="Hello World!"} {param2="Hello Again!"}
   {system("./external_script \"" param1 "\" \"" param2 "\"") }' <<< "$dummy"

Last edited by colucix; 10-27-2011 at 07:18 AM.
 
Old 10-27-2011, 07:24 AM   #7
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Tou have problem with double quotes.
What you need is that:
Code:
#!/bin/bash
dummy="dummy"
awk 'BEGIN {param1="Hello World!"
            param2="Hello Again!"}
    {system("./external_script \"" param1 "\" \"" param2 "\"")}' <<< "$dummy"
which gives the right output:
Code:
./main_script 
I am external script
String parameter1----Hello World!----
String parameter2----Hello Again!----
end of external script
I will not explain and let you find by yourself.

Last edited by berbae; 10-27-2011 at 07:35 AM.
 
Old 10-27-2011, 09:43 AM   #8
danicobos
LQ Newbie
 
Registered: Oct 2011
Location: Madrid, Spain
Distribution: RHEL
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up

You are right berbae! Your last post works perfectly.

Once again thank you, and also thanks to colucix!
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Using Variables With awk LXer Syndicated Linux News 0 11-17-2010 01:30 AM
[SOLVED] Not able to get external variables in awk vinaytp Linux - Newbie 4 05-25-2010 01:41 AM
Using variables with awk dots Linux - Newbie 4 07-07-2009 03:00 PM
using variables in awk vgr12386 Programming 14 06-24-2009 04:19 AM
Assigning variables by awk(?) bioinformatics_guy Linux - Newbie 2 02-19-2009 12:01 PM


All times are GMT -5. The time now is 06:56 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