当我想在bash脚本中运行这个sqllite3命令时,我会收到错误消息。会很感激你的帮助。
DB_CMD="ssh -X node-11 \"cd ~/test/emane/gvine/node-10/ && sqlite3 -header -csv emane_data.db \"select * from rxfile;\" >>./out.csv\\"""
eval $DB_CMD
eval: line 56: unexpected EOF while looking for matching `"' eval: line 57: syntax error: unexpected end of file
sqlite3: Error: too many options:发布于 2015-08-12 01:15:35
你已经有相当复杂的情况了。不要通过尝试将eval放入混合中而变得更加困难--或者至少让shell自己来完成构建引用的工作。
# put a shell-quoted version of your sqlite command into sqlite_cmd_str
printf -v sqlite_cmd_str '%q ' sqlite3 -header -csv emane_data.db "select * from rxfile;"
# substitute that into remote_cmd_str
remote_command_str="cd ~/test/emane/gvine/node-10/ && ${sqlite_cmd_str} >>out.csv"
# ...now, run that remote command...
ssh -X node-11 "$remote_command_str"
# ...or, if you **really** want to use eval:
printf -v local_cmd_str '%q ' ssh -X node-11 "$remote_command_str"
eval "$local_cmd_str"也就是说--在字符串中保存代码并对其进行eval‘eval’是而不是的最佳实践。
run_remote_sql() {
local sqlite_cmd_str remote_cd_cmd_str remote_cmd_str
local node_num=$1
printf -v sqlite_cmd_str '%q ' \
sqlite3 -header -csv emane_data.db "select * from rxfile;"
printf -v remote_cd_cmd_str '%q ' \
cd "test/emane/gvine/node-$1"
remote_cmd_str="$remote_cd_cmd_str && $sqlite_cmd_str >>out.csv"
ssh -X "node-$1" "$remote_cmd_str"
}...to定义了一个函数,该函数可以被调用为:
run_remote_sql 10或者,在远程端执行参数管理:
run_remote_sql() {
printf -v extra_args '%q ' "$@"
ssh -X node-"$1" "bash -s $extra_args" <<'EOF'
cd ~/test/emane/gvine/node-"$1"/ || exit
sqlite3 -header -csv emane_data.db "select * from rxfile;" >>./out.csv
EOF
}...for,再次..。
run_remote_sql 10现在,为什么所有的printf %q?首先,这意味着您不需要自己做所有引用(正如您可能已经注意到的,正确引用多个嵌套计算级别是困难的!)。第二:安全原因。
想想看,如果你的程序是用'$(rm -rf /)'的参数运行的--你永远不希望你的脚本能够运行cd ~/test/emane/gvine/node-$(rm -rf /);使用printf %q来构建安全的字符串可以确保内容总是被安全地转义。
下一个问题:为什么把一些东西(如>>out.csv,, of printf %q )作为常量放置是安全的?因为:
printf %q将其转换为数据,使它们不再受shell的指令所尊重。out.csv是$2.csv,那么我们希望使用printf %q来转义"$2"部分。发布于 2015-08-12 01:07:44
命令的这一部分:./out.csv\"""
看起来它在末尾有一个额外的引号",如果是的话,解释器认为它是一个新字符串(但未终止)的开始。
https://stackoverflow.com/questions/31952970
复制相似问题