2

有这样的脚本:

#! /bin/bash

typeset -i i END
let END=500 i=1
remainder=1
accum="use yola_test;\n"
for ((i=1;i<=END;++i)); do 
#   str1=$( echo "$i" | md5sum | md5sum )
    title="title_$i"
#   "${str1:2:20}"
    accum="${accum}CALL add_new_enterprise(\"$title\");\n"
    let "remainder=$i % 100"
    if [ $remainder -eq 0 ]; then
        accum="${accum}SELECT count(*) as \`enterprises:\` FROM enterprise;\n"
        mysql --host=l --port=0 --user=r --password='!' --execute="$accum" 
        accum="use yola_test;\n"
    fi
done

但是对于每个 \n 它都会给我“Pager set to stdout”,我可以避免这种情况吗,我知道在回显它时我必须使用 -e 选项,但是我阅读了一些关于 ANSI-C 引用的材料,但没有示例如何使用它。

我试着这样做

mysql --host=l --port=0 --user=r --password='!' --execute="$( echo -e "$accum" )"

但它没有效果,我认为调用 echo 会增加运行时间。

4

2 回答 2

3

@pgl 的答案是这种情况下的最佳方法,但如果您确实需要在变量值中嵌入换行符,最简单的做法是使用 bash 的$'...'引用形式:

accum=$'use yola_test;\n'
...
accum="${accum}CALL add_new_enterprise(\"$title\");"$'\n'
...etc

请注意,上面的第二个示例使用了多种引用类型;第一部分的双引号允许变量插值,然后$'...'是需要转义序列解释的部分。

顺便说一句,另一种方法是定义一个变量来保存换行符:

nl=$'\n'
accum="use yola_test;${nl}"
...
accum="${accum}CALL add_new_enterprise(\"$title\");${nl}"
...etc
于 2011-11-08T20:38:58.087 回答
2

“PAGER set to stdout”来自 MySQL - 要停止显示,只需从代码中删除 \n 的实例;你不需要它们。

于 2011-11-08T15:24:10.197 回答