1、mysql脚本文件 t.sql
insert into test.t values(@name,@age);
exit
2、shell脚本文件 a.sh (为方便演示,与t.sql文件放在同一目录下)
#!/bin/sh
# fetch user parameters input by user
read -p "please input name value : " name
read -p "please input name age : " age
echo "name:$name age:$age"
# set parameters as sql variables && add them to the first line of t.sql
sed -i "1 i\ set @name=$name;" t.sql
sed -i "1 i\ set @age=$age;" t.sql
# execute sql script
mysql -u root < t.sql
# clear sql script variables
sed -i "1d" t.sql
sed -i "1d" t.sql
3、执行&&结果