Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BASH Scripting Help Needed: Scripting MySQL
#1
I'm here once again to consult the great knowledge present.

I'm working on writing a web app thingy, and would like to input stuff into a database, it has to be by hand and I'd like to be able to do it easily and quickly. Currently my code is:


#!/bin/bash

#Ask if you are in enter mode. -e By the way

if
[ "$1" == -e ] ; then

echo "Hello, welcome to Life."
echo --------------------------
echo "You have chosen to enter information into the database."
echo "-------------------------"
echo -n "Please enter the title of the information --> "
read $title
echo "Please enter the description: "
read $description
echo -n "Please enter the type --> "
read $type
echo "Please enter the location"
read $location
echo "Please enter any notes"
read $notes
echo "Please enter the physical location if it applies"
read $physlocation
echo "Please enter any keywords you would like associated with this item."
read $keywords


/Applications/MAMP/Library/bin/mysql --user=microchip -p Life --execute=< "INSERT INTO `items` ( `title` , `description` , `type` , `keywords` , `location` , `notes` , `physlocation` )
VALUES (
'$title', '$description', '$type', '$keywords', '$location', '$notes', '$physlocation'
);"
EOF

else

echo "That's not valid"

fi


What I get is:

./Life.sh: line 1: items: command not found
./Life.sh: line 1: title: command not found
./Life.sh: line 1: description: command not found
./Life.sh: line 1: keywords: command not found
./Life.sh: line 1: location: command not found
./Life.sh: line 1: notes: command not found
./Life.sh: line 1: physlocation: command not found
/Applications/MAMP/Library/bin/mysql: ambiguous option '--p' (pager, protocol)


Meaning it's trying to execute the stuff in the backtics. I've tried putting \ infront of all the backticks but there's no luck there.

Advice?

TiA!
Reply
#2
i'm sure you have good reason - but what is wrong with a web interface?
Reply
#3
Ultimately it will have a web interface, but for mostly searching and light duty entering.


I'm creating this so have a database of everything, all of my files, physical objects and so fourth with then the web interface to search through it all.

I'd prefer to have a rather streamlined way of entering information for physical objects right now opposed to a web interface.
Reply
#4
I know its not what you're looking for but I'd consider this -

http://www.phpmyadmin.net/home_page/index.php
Reply
#5
[quote microchip13]Meaning it's trying to execute the stuff in the backtics. I've tried putting \ infront of all the backticks but there's no luck there.

Advice?
Use regulartics instead of backtics in that SQL statement (i.e. single quotes). You use the normal ones in the VALUES part. I don't understand why you chose to use backtics in the INSERT INTO part.
Reply
#6
Try this version:

#!/bin/bash
#Ask if you are in enter mode. -e By the way

if [ "$1" = -e ] ; then

echo "Hello, welcome to Life."
echo --------------------------
echo "You have chosen to enter information into the database."
echo "-------------------------"
echo -n "Please enter the title of the information --> "
read title
echo "Please enter the description: "
read description
echo -n "Please enter the type --> "
read type
echo "Please enter the location"
read location
echo "Please enter any notes"
read notes
echo "Please enter the physical location if it applies"
read physlocation
echo "Please enter any keywords you would like associated with this item."
read keywords

/Applications/MAMP/Library/bin/mysql --user=microchip -p Life --execute=< INSERT INTO 'items' ( 'title' , 'description' , 'type' , 'keywords' , 'location' , 'notes' , 'physlocation' )
VALUES (
"$title", "$description", "$type", "$keywords", "$location", "$notes", "$physlocation"
);
EOF

else

echo "That's not valid"

fi
# end of chipscript

The following changes were made:

1. The 'read' command takes the NAME of a variable as its argument, into which the line of standard input is placed. Putting a '$' in front of the name tells the shell to substitute the VALUE of the variable at the time of evaluation.

2. Use single quotes rather than backticks. backticks are 'execute this command and replace it with the standard output' . See "Command Substitution" in the bash man page.

3. Use double quotes around variables to get their values, not single quotes.

I don't know what the issue with MySQL is about the --p or -p argument:
/Applications/MAMP/Library/bin/mysql: ambiguous option '--p' (pager, protocol)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)