Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

The following shell script is not inserting the data to the table. How to pass the variable value to insert sql statement in a shell script.

id=0
while true
do
id=`expr $id + 1`;
mysql -u root -ptest --socket=/data/mysql1/mysql.sock -e 'insert into mytest1.mytable2(id,name) values (' $id ',"testing");'
echo $id  >> id.txt
done

I have modified the script as below and tried, and still having the issue

id=0
while true
do
id=`expr $id + 1`;
# mysql -u root -ptest --socket=/data/mysql1/mysql.sock1 -e 'insert into mytest1.mytable1(name) values ("amma");'
mysql -u root -ptest --socket=/data/mysql1/mysql.sock -e 'insert into mytest1.mytable2(id,name) values ( $id ,"testing");'
echo $id  >> id.txt
done

error :

]$ ./insert 
ERROR 1054 (42S22) at line 1: Unknown column '$id' in 'field list'
share|improve this question

1 Answer 1

up vote 0 down vote accepted

This one should work:

id=0
while true
do
    id=`expr $id + 1`;
    mysql -u root -ptest --socket=/data/mysql1/mysql.sock -e "insert into mytest1.mytable2(id,name) values ($id, \"testing\");"
    echo $id  >> id.txt
done

Variables within double quotes are interpolated, while those found between single quotes are not.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.