开发者

Splitting string into array upon token

开发者 https://www.devze.com 2022-12-24 03:08 出处:网络
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 t

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 unavailab开发者_如何学Pythonle

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]


IFS=$'\n' read -a foo <<< $'12\n345\n67'
echo "${foo[@]}"


^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


IFS=$'\r'
set -- $result
echo $1
echo $2


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消