cómo reconocer \ n en bash

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.

preguntado el 08 de noviembre de 11 a las 15:11

2 Respuestas

@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 $'...' form of quoting:

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

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:

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

respondido 09 nov., 11:00

and then i do "echo -e $accum" no line feeds in the output(( - Yola

Trata echo -e "$accum" -- the double-quotes are necessary to keep the shell from turning linefeeds (and tabs and...) into word breaks, which echo then turns into spaces. - Gordon Davisson

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.

respondido 08 nov., 11:19

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.