xhutpl
xhutpl

Reputation: 15

How to execute and display output from command cgi script

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

Answers (2)

Gerhard
Gerhard

Reputation: 280

It will work if you set argu like this:

argu=`arp -a | grep "$REMOTE_ADDR" | awk '{print $4}'`

The reasons:

  • Command substitution can be done with `command`
  • $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

l&#39;L&#39;l
l&#39;L&#39;l

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

Related Questions