Search code examples
htmlbashcgi

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}"

Solution

  • 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.