views:

162

answers:

4

I'm writing a script to perform an offsite rsync backup, and whenever the rsyncline recieves some output it goes into a single variable. I then want to split that variable into an array upon the ^M token, so that I can send them to two different logger-sessions (so I get them on seperate lines in the log).

My current line to perform the rsync

result=`rsync --del -az -e "ssh -i $cert" $source $destination 2>&1`

Result in the log, when the server is unavailable

ssh: connect to host offsite port 22: Connection timed out^M rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at io.c(601) [sender=3.0.7]

A: 
IFS=$'\n' read -a foo <<< $'12\n345\n67'
echo "${foo[@]}"
Ignacio Vazquez-Abrams
I get an> Syntax error: redirection unexpectedon that.
Gnutt
The herestring redirection (`<<<`) is only valid in bash, not sh.
Ignacio Vazquez-Abrams
Ah, using bash to interpret instead of sh, made it better... But I still could not get it to work.
Gnutt
A: 
IFS=$'\r'
set -- $result
echo $1
echo $2
ghostdog74
I get weird break on this logger only gets `ssh: co`
Gnutt
also, the scripts appear to hang after that.
Gnutt
use `\r` and see how the result go.
ghostdog74
About the same response, instead, ut breaked on the first r... I don't know why, but I've gotten the whole script working. Solution is posted.
Gnutt
Try escaping the backslash: `\\n` or `\\r`
Dennis Williamson
+1  A: 

^M is $'\r' so you might try that as your delimiter:

IFS=$'\r' read -ar result <<< $(rsync --del -az -e "ssh -i $cert" $source $destination 2>&1)
echo ${result[0]}   # first part
echo ${result[1]}   # second part
Dennis Williamson
Almost right, the ^M is sent out by my SSH-client, when it throws this error, trought rsync. So it still comes out as a ^M, but I've gotten it working!
Gnutt
A: 

Thanks guys, trough a mix of your answers, I came up with a working solution. Here it is.

result=`rsync --del -az -e "ssh -i $cert" $source $destination 2>&1`
IFS=$'\n'
for variable in $result; do
    logger $variable
done
unset IFS

Now the entire script works!

Gnutt