LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Awk variables (http://www.linuxquestions.org/questions/linux-general-1/awk-variables-910017/)

danicobos 10-25-2011 07:07 AM

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.

colucix 10-25-2011 07:15 AM

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)

danicobos 10-26-2011 09:31 AM

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.

berbae 10-26-2011 05:26 PM

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.

danicobos 10-27-2011 06:19 AM

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.

colucix 10-27-2011 08:17 AM

You need to correctly put the escaped quotes:
Code:

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


berbae 10-27-2011 08:24 AM

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.

danicobos 10-27-2011 10:43 AM

You are right berbae! Your last post works perfectly.

Once again thank you, and also thanks to colucix!


All times are GMT -5. The time now is 07:32 PM.