Reputation: 15
I want to execute command from variable and display the output. code look like this but it doesn't work I don't know exactly why?
#!/bin/sh
echo "Content-type: text/html"
echo
argu="arp -a | grep $REMOTE_ADDR | awk '{print $4}'"
echo '<html> <head> <title> CGI script </title> </head> <body>'
echo "<h1>HELLO $REMOTE_ADDR</h1>"
echo "Mac is ${argu}"
Upvotes: 1
Views: 2154
Reputation: 280
It will work if you set argu
like this:
argu=`arp -a | grep "$REMOTE_ADDR" | awk '{print $4}'`
The reasons:
$4
must not be enclosed by double quotation marks, otherwise it will be substituted by the shell instead of awk. Make sure $REMOTE_ADDR
is set.
Upvotes: 0
Reputation: 47169
Your script has a few issues, mainly the argu
command should run in a sub-shell:
#!/bin/sh
echo "Content-type: text/html"
echo
argu="$(arp -a | grep "$REMOTE_ADDR" | awk '{print $4}')"
echo '<html> <head> <title> CGI script </title> </head> <body>'
echo "<h1>HELLO $REMOTE_ADDR</h1>"
echo "Mac is $argu"
In addition, the variable you grep
should be double-quoted. You can always check the syntax of scripts such as this @ shellcheck.net.
Upvotes: 1