Gustavo Costa
Gustavo Costa

Reputation: 21

CGI (shell script) not write in the file with echo command

I'm having problems to write in the file with my CGI.

I make a simple HTML formular to press a button and write a phrase in the .txt file, but it doesn't work.

The server is an Apache2.

The scrips works if executed in the terminal.

ps. The final line of CGI works, updating a HTML page URL.

file index.html

<html>
<title>BeagleBone</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<div class="w3-container w3-green">
  <h1>BeagleBone GPIO Control</h1> 
  <p>Acesso remoto a GPIO's em rede local</p> 
</div>

<div class="w3-row-padding">
  <div class="w3-third">
    <h2>GPIO x</h2>
    <p>Led blue</p>
    <form method="POST" action="/cgi-bin/led.cgi">
  <input type="submit" name="led" value="ON">
  <input type="submit" name="led" value="OFF">
</form>
  </div>

</body>
</html>

file led.gci

#!/bin/bash

LED_FILE="/home/gpcosta/Desktop/data.txt"

read CONTENT

if [ "$CONTENT" == "led=ON" ]; then
    echo "Apertou botao ON" >> $LED_FILE
elif [ "$CONTENT" == "led=OFF" ]; then
    echo "Apertou botao OFF" >> $LED_FILE
fi

echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<meta http-equiv="refresh" CONTENT="0;url=http://192.168.15.20/index.html">'
echo '</html>'

The cgi permissions below

enter image description here

Upvotes: 2

Views: 865

Answers (1)

mmendescortes
mmendescortes

Reputation: 129

It's because the web server don't have permissions on the directory.

To solve this, run the following script on your cgi server:

echo “content-type: text/html”
echo “”
echo “<html>
        <head>
                <meta charset='utf-8'/>
                <title>What permission?</title>
        </head>
        <body>
                <p>Run this on your server terminal:</p>
                <pre>chown $(whoami) ${PWD}
chmod 755 ${PWD}</pre>
  </body>
</html>”

Then simply copy the script your web browser will display and paste it on the CGI Server terminal, remember to run it with a priviledged user.

Upvotes: 1

Related Questions