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. |
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) |
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. |
I tried with these testing scripts:
external_script Code:
#!/bin/bash Code:
#!/bin/bash Code:
./main_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. |
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. |
You need to correctly put the escaped quotes:
Code:
awk 'BEGIN {param1="Hello World!"} {param2="Hello Again!"} |
Tou have problem with double quotes.
What you need is that: Code:
#!/bin/bash Code:
./main_script |
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 10:30 AM. |