这是我的问题。我有一组SQL数据库查询,这些查询是使用Shell脚本和具有.sql扩展名的相应批处理文件触发的。我的某些查询需要一些手动用户输入(根据请求),我在脚本中没有实现过的运气。
到目前为止,我通过shell脚本将用户输入作为日期输入:
echo "Please specify the date in yyyy-mm-dd format: 'input;"
read input
mysql -u user -ppass < batch_file.sql
我想将变量
$input
传递给批处理文件。正在执行sql查询的批处理文件如下所示:
select count(*) as count from subscriptions
where date_add(optin_date, interval 1 year) between "$input"
and date(curdate())
and active = 1
and stype = "domain";
其中
"$input"
是从Shell脚本向下传递的变量。任何想法将不胜感激,谢谢。
最佳答案
尝试在脚本中使用here-doc进行此操作:
#!/bin/sh
echo "Please specify the date in yyyy-mm-dd format: 'input;"
read input
mysql -u user -ppass <<EOF
select count(*) as count from subscriptions
where date_add(optin_date, interval 1 year) between "$input"
and date(curdatE())
and active = 1
and stype = "domain";
EOF
如果由于某种原因无法使用第一种解决方案,则可以使用另一种解决方案:
echo "Please specify the date in yyyy-mm-dd format: 'input;"
read input
sed -i "s/\"\$input\"/$input/g" batch_file.sql
mysql -u user -ppass < batch_file.sql
如果您喜欢第二种解决方案,则最好在SQL文件中使用类似
%%PATTERN%%
的内容代替$input
。关于mysql - 接受MYSQL中的用户输入? Shell脚本/批处理文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14753721/