Need help on Insert data to phpMyAdmin mySQL database from Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on Insert data to phpMyAdmin mySQL database from Shell Script
# 1  
Old 07-16-2014
Question Need help on Insert data to phpMyAdmin mySQL database from Shell Script

Sorry to disturb you, I would like to seek help on inserting data whenever the switch is on or off to my phpMyAdmin mySQL database from my Shell Script. I'm using Raspberry PI as my hardware and I have follow this LINK: instructables.com/id/Web-Control-of-Raspberry-Pi-GPIO/?ALLSTEPS to create my own Web Control Interface. I have created a mySQL database and insert into my phpMyAdmin. After I implemented insert method to my GPIOServer.sh and execute it, my shell script can't work at all, my LED never light up and never store into mySQL database.

This is my GPIOServer.sh:
Code:
#!/bin/sh

mysqlusername="USERNAME HERE"
mysqlpassword="PASSWORD HERE"

#Set  Refresh
echo "How long do you want the wait time to be?"
read waitTime

#Invoke GPIO
echo "4" > /sys/class/gpio/export
echo "17" > /sys/class/gpio/export
echo "18" > /sys/class/gpio/export
echo "21" > /sys/class/gpio/export
echo "22" > /sys/class/gpio/export
echo "23" > /sys/class/gpio/export
echo "24" > /sys/class/gpio/export
echo "25" > /sys/class/gpio/export

#Start Loop
while :
do
#Read MySQL Data
#Direction
direction4=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='4'";)
direction17=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='17'";)
direction18=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='18'";)
direction21=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='21'";)
direction22=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='22'";)
direction23=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='23'";)
direction24=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='24'";)
direction25=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='25'";)
#Status
status4=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='4'";)
status17=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='17'";)
status18=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='18'";)
status21=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='21'";)
status22=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='22'";)
status23=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='23'";)
status24=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='24'";)
status25=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='25'";)

#Run Commands
if [ "$direction4" == "out" ]; then
    echo "out" > /sys/class/gpio/gpio4/direction
    if [ "$status4" == "1" ]; then
        echo "1" > /sys/class/gpio/gpio4/value
        $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("1","Red LED", dateTime,"")";)
        echo "GPIO 4 Turned On"
    else
        echo "0" > /sys/class/gpio/gpio4/value
        $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("1","Red LED", "", dateTime)";)
        echo "GPIO 4 Turned Off"
    fi
else
    echo "in" > /sys/class/gpio/gpio4/direction
fi
if [ "$direction17" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio17/direction
    if [ "$status17" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio17/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("2","Blue LED", dateTime,"")";)
                echo "GPIO 17 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio17/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("2","Blue LED", "", dateTime)";)
                echo "GPIO 17 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio17/direction
fi
if [ "$direction18" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio18/direction
    if [ "$status18" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio18/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("3","Green LED", dateTime,"")";)
                echo "GPIO 18 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio18/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("3","Green LED", "", dateTime)";)
                echo "GPIO 18 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio18/direction
fi
if [ "$direction21" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio21/direction
    if [ "$status21" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio21/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("4","Yellow LED", dateTime,"")";)
                echo "GPIO 21 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio21/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("4","Yellow LED", "", dateTime)";)
                echo "GPIO 21 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio21/direction
fi
if [ "$direction22" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio22/direction
    if [ "$status22" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio22/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("5","LED", "","")";)
                echo "GPIO 22 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio22/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("5","LED", "", "")";)
                echo "GPIO 22 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio22/direction
fi
if [ "$direction23" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio23/direction
    if [ "$status23" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio23/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("6","LED", "","")";)
                echo "GPIO 23 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio23/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("6","LED", "", "")";)
                echo "GPIO 23 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio23/direction
fi
if [ "$direction24" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio24/direction
    if [ "$status24" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio24/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("7","LED", "","")";)
                echo "GPIO 24 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio24/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("7","LED", "", "")";)
                echo "GPIO 24 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio24/direction
fi
if [ "$direction25" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio25/direction
    if [ "$status25" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio25/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("8","LED", "","")";)
                echo "GPIO 25 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio25/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values("8","LED", "", "")";)
                echo "GPIO 25 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio25/direction
fi
#Complete Loop
sleep $waitTime
done

This is my error screenshots when I execute my GPIOServer.sh:

Image

Hope you guys could help me out with it as soon as possible, thanks in advance, guys.
# 2  
Old 07-16-2014
try replace [ by [[ and ] by ]] on the errors line (if construction)
# 3  
Old 07-16-2014
Quote:
Originally Posted by protocomm
try replace [ by [[ and ] by ]] on the errors line (if construction)
New error, protocomm
It stated that:
Code:
./GPIOServer.sh: 44: ./GPIOServer.sh: [[out :not found
./GPIOServer.sh: 58: ./GPIOServer.sh: [[out :not found
./GPIOServer.sh: 72: ./GPIOServer.sh: [[out :not found
./GPIOServer.sh: 86: ./GPIOServer.sh: [[out :not found
./GPIOServer.sh: 100: ./GPIOServer.sh: [[out :not found
./GPIOServer.sh: 114: ./GPIOServer.sh: [[out :not found
./GPIOServer.sh: 128: ./GPIOServer.sh: [[out :not found
./GPIOServer.sh: 142: ./GPIOServer.sh: [[out :not found


Last edited by Franklin52; 07-16-2014 at 05:11 PM.. Reason: Code tags
# 4  
Old 07-16-2014
The correct string equality test would be:
Code:
if [ "$direction4" = "out" ]; then

instead of:
Code:
if [ "$direction4" == "out" ]; then

but some shells will accept == as a synonym for =. It looks like you dropped a space when converting [ expression ] to [[ expression ]] (the spaces before and after [[ are crucial in shells that support [[ expression ]]).

It appears that /bin/sh on your system is not an old Bourne shell and is not a link to bash. What OS are you using?
# 5  
Old 07-16-2014
Quote:
Originally Posted by Don Cragun
The correct string equality test would be:
Code:
if [ "$direction4" = "out" ]; then

instead of:
Code:
if [ "$direction4" == "out" ]; then

but some shells will accept == as a synonym for =. It looks like you dropped a space when converting [ expression ] to [[ expression ]] (the spaces before and after [[ are crucial in shells that support [[ expression ]]).

It appears that /bin/sh on your system is not an old Bourne shell and is not a link to bash. What OS are you using?
I'm using:
Raspbian for Operating System
Raspberry PI for Hardware

---------- Post updated at 04:57 PM ---------- Previous update was at 04:43 PM ----------

This is my sensor mySQL database in phpMyAdmin:
Code:
CREATE TABLE IF NOT EXISTS `sensor` (
  `id` int(11) NOT NULL,
  `sensor` text NOT NULL,
  `switchOnLog` datetime NOT NULL,
  `switchOffLog` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Last edited by aoiregion; 07-16-2014 at 07:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Need help on Insert data to mySQL database

Hi guys, I would like to seek help on inserting data whenever the switch is on or off to my sensor mySQL database in phpMyAdmin from my control.php. I'm using Raspberry PI as my hardware and follow a few tutorials to create my own Web Control Interface, it works perfectly without insert method.... (1 Reply)
Discussion started by: aoiregion
1 Replies

2. Shell Programming and Scripting

Shell script for insert multiple records into a Database

I have a table in an Informix DB into which I want to insert multiple records at a time. Data for one of the column should be unique & other column data may be the same for all the records I insert Typical Insert Statement I use to insert one row : insert into employee(empid, country, state)... (5 Replies)
Discussion started by: nisav
5 Replies

3. Web Development

INSERT data to a Database Table from a text file

If you have a text file and if you want to Insert data to your Database Table, You can do it with these queries LOAD DATA LOCAL INFILE '/path/yourTextFile.txt' INTO TABLE yourTableName FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' (0 Replies)
Discussion started by: sitex
0 Replies

4. Shell Programming and Scripting

shell script to insert data from gps.txt to mysql database

Hi, I have gps receiver, by using gpsd data i can read gps log data to my database(my sql). Steps: 1. telenet localhost 2947 > gps.txt (press enter) 2. r (press enter) //then i will get the data like below in gps.txt file Trying 127.0.0.1... Connected to localhost.... (1 Reply)
Discussion started by: gudivada213
1 Replies

5. Shell Programming and Scripting

insert values into sqlplus database using shell script

hello all, I am new to shell scripting and to unix... so the following is my assignment.. here i am trying to insert a values into sqlplus database using shell script. the following is my shell script InsertDelete.sh #! /bin/sh echo "*********The MENU******** 1.Insert The Values... (2 Replies)
Discussion started by: pradeept
2 Replies

6. Shell Programming and Scripting

how to insert data into database by reading it from a text file??

Hi....can you guys help me out in this script?? Below is a text file and it contains these: GEF001 000093625 MKL002510 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL003604 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL005675 000001... (4 Replies)
Discussion started by: pallavishetty
4 Replies

7. Shell Programming and Scripting

Need shell script to extract data from oracle database

shell script (4 Replies)
Discussion started by: frns5
4 Replies

8. Shell Programming and Scripting

How to insert data into MYSql database from a text file

Hi, Need to get help from you guys about this issue. I need to insert data into MySql database from a text file which is located in other server. The text file is something look like below: Date | SubscriberNo | Call Duration 20/7/07 | 123456788 | 20 20/7/07 | 123412344 | 30 The... (4 Replies)
Discussion started by: shirleyeow
4 Replies

9. Shell Programming and Scripting

Shell Script: want to insert values in database when update script runs

Hi , I am new to linux and also also to shell scripting. I have one shell script which unpacks .tgz file and install software on machine. When this script runs I want to insert id,filename,description(which will be in readme file),log(which will be in log file) and name of unpacked folder... (1 Reply)
Discussion started by: ring
1 Replies

10. Shell Programming and Scripting

how to insert data in database based on text file?

Hi....can you guys help me out in this script?? Below is a text file script....called Bukom.txt and it contains these: BUKOM 20060101 2.5 2.6 2.7 2.8 2.9 2.3 2.1 BUKOM 20060102 2.4 2.5 2.6 2.7 2.7 2.6 2.4 BUKOM 20060103 2.1 ... (9 Replies)
Discussion started by: forevercalz
9 Replies
Login or Register to Ask a Question

Featured Tech Videos