1

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}"
1
  • You should include the output you get, and show what part is incorrect. Commented Feb 26, 2017 at 1:02

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It still doesn't work sir. Can't get the mac address
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.