Pass variable FROM awk TO shell

October 20, 2010 at 09:21:56
Specs: Red Hat Enterprise Linux Server release 5.3 (Tikanga)
Hi!


I've got some piece of program that needs to use a AWK variable INTO SHELL command.

Here it is:

#Somewhere before, shell variable "linea" got some value.
# Pay attention on the double quote-single quote-double quote to run the shell command.
echo $linea | awk '{if($1 == 0) print $2,$3; else print $2,$3,"FALLIDO CON SC "$1" => " "'"$(bperror -S $1|head -1)"'"}'


The part that isn't working is the last one: bperror -S $1|head -1

The bperror is a good command, is in $PATH and everything... but I'm unable to tell him that $1 is the first parameter of awk. $1 always must be a number, and always is a number.

If I change $1 by a number it works:

#This line works fine
echo $linea | awk '{if($1 == 0) print $2,$3; else print $2,$3,"FALLIDO CON SC "$1" => " "'"$(bperror -S 3|head -1)"'"}'

Anybody has an idea?


Thanks in advance...


Magius


See More: Pass variable FROM awk TO shell

Report •

#1
October 21, 2010 at 07:48:24
You have a logic problem because everything between the double quote-single quote-double quote is considered a shell variable.

Instead of using awk, consider letting the bash shell do this. The shell can parse the variable:

#!/bin/bash

# Untested
linea="0 4 5"

set - $(echo $linea)
if [ $1 -eq 0 ]]
then 
   .
   .
# end script

If you insist on using awk, you might try embedding an external variable within the variable:

extvar="3"

.... "'"$(bperror -S "'"${extvar}"'"|head -1)"'" ....

but I doubt it will work


Report •
Related Solutions


Ask Question