如何在bash中识别\n
有这样的脚本:
#! /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 它都会给我“寻呼机设置为标准输出”,我可以避免这种情况吗?我知道在回显它时我必须使用 -e 选项,但我读了一些有关 ANSI-C 引用的材料,但无处可去示例如何使用它。
我尝试这样做
mysql --host=l --port=0 --user=r --password='!' --execute="$( echo -e "$accum" )"
,但没有效果,我认为调用 echo 会增加运行时间。
Have such script:
#! /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
But for every \n it gives me "Pager set to stdout", can i avoid this, i know that when echoing it i must use -e option, yet i read some material about ANSI-C quoting, but nowhere examples how to use it.
I tried to do so
mysql --host=l --port=0 --user=r --password='!' --execute="$( echo -e "$accum" )"
but it hasnt effect and i think call for echo will increase runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@pgl 的答案是这种情况下的最佳方法,但如果您确实需要在变量值中嵌入换行符,最简单的方法是使用 bash 的
use yola_test;\n' ... accum="${accum}CALL add_new_enterprise(\"$title\");"$'...'
引用形式:请注意,上面的第二个示例混合使用了引用类型;第一部分使用双引号以允许变量插值,然后使用
$'...'
表示需要转义序列解释的部分。顺便说一句,另一种方法是定义一个变量来保存换行符:
\n' ...etc请注意,上面的第二个示例混合使用了引用类型;第一部分使用双引号以允许变量插值,然后使用
$'...'
表示需要转义序列解释的部分。顺便说一句,另一种方法是定义一个变量来保存换行符:
@pgl's answer is the best approach for this case, but if you actually did need to embed linefeeds in a variable value the simplest thing to do is to use bash's
use yola_test;\n' ... accum="${accum}CALL add_new_enterprise(\"$title\");"$'...'
form of quoting:Note that the second example above uses a mix of quote types; double-quotes for the first part to allow variable interpolation, and then
$'...'
for the part that needs escape sequence interpretation.BTW, another approach would be to define a variable to hold the newline:
\n' ...etcNote that the second example above uses a mix of quote types; double-quotes for the first part to allow variable interpolation, and then
$'...'
for the part that needs escape sequence interpretation.BTW, another approach would be to define a variable to hold the newline:
“PAGER set to stdout”来自 MySQL - 要停止显示,只需从代码中删除 \n 实例即可;你不需要它们。
The "PAGER set to stdout" is from MySQL - to stop it displaying, just remove instances of \n from your code; you don't need them.