mercredi 25 février 2015

bash $(printf "%s\n") does not create multiline


I created shell script, that logs into FTP server and renames some directories. I put FTP commands in multiline string like so



# !/bin/sh

ftp -inv $HOST << EOF
user $USER $PASSWORD
$COMMANDS
bye
EOF


the $COMMANDS contains commands separated by new line. This approach works, when I construct command like so



NL=$'\n'
COMMANDS="command one$NL"
COMMANDS+="command two$NL"
COMMANDS+="command three"


I firstly tried populate $COMMANDS with function printf, but even after several trials, I was unsuccessful. The expression



COMMANDS="$(printf "%s\n" "command one" "command two" "command three")"


evaluated to string with blank spaces between commands and when used in script as FTP command, it was processed as single line.


I tried replace \n with \r which results in variable $COMMANDS not beeing populated with any string.


Can someone more educated please explain to me, whats the problem with printf function here?


Update


I've manage how to achieve my goal via rsync, so the FTP approach I've abandoned, but I'm still curious why this happens.


Problem is with storing printf in variable. I made test script:



echo "==== printf directly"
printf "%s\n" "foo" "bar" "baz"

echo "==== stored in variable"
VAR_1=$(printf "%s\n" "foo" "bar" "baz")
echo $VAR_1


running ./test.sh outputs



==== printf directly
foo
bar
baz
==== stored in variable
foo bar baz


setting IFS=$'\n' does not help.


Update 2


I can get multiline from variable if I wrap it in double qotes



echo "$VAR_1"


but I'm unable to get the same result if used inside heredoc



echo `cat <<EOF
$VAR_1
"$VAR_1"
EOF`


I get output



foo bar baz "foo bar baz"


note: there is even no newline between the two variable outputs in heredoc, which is also strange to me.



Aucun commentaire:

Enregistrer un commentaire