I need to use sh -c "command"
where command is an awk
with variables, but I haven't been able to, anyone can help?
Here is an example:
I have a file with several lines, and want to print the number of the lines that match a mark. In this case I use sh
, but I will need to use ssh
also.
$ file=file.txt
$ mark="xxxx"
$ cat $file
name
surname
xxxx
phone
xxxx
more data
more more data
xxxx
continue data
$ sh -c "awk -v line=\"$mark\" '$0 == line {print NR}' $file"
$ awk -v line="$mark" '$0 == line {print NR}' $file
3
5
8
The first run with sh
returns nothing. The second run without sh
runs ok.
==== more clarification
I have a function "execmd" that receives two parameters, first argument is a server name, the second is a string with a command
execmd ()
{
P_SERV="$1"
P_CMD="$2"
X_SERV=`hostname`
if [ "$P_SERV" = "$X_SERV" ] ; then
sh -c "$P_CMD"
else
ssh -n "$P_SERV" "$P_CMD"
fi
}
if i run:
xx=`execmd earth "awk -v line=\"$mark\" '\$0 == line {print NR}' $file"`; echo $xx
xx is empty
i put a echo inside execmd to see the command, and inside de function it translates the \$0 to bash is there any other thing i have to add? This is part of a mega script, where i have from one place monitor several ones, so if i have to run somthing locally i use sh, else ssh