0

我想在这里替换“cat”,以便源中的行项目可以有空格,以使这个 Rsync 脚本更便携(在 OSX 中)。

各位高手能不能给点建议的方法呢?其他改善这一点的建议也很感激!谢谢!

#!/bin/bash       
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH

LOG="/Users/jdoe/Desktop/rsync-`/bin/date +%m%d%Y`.log"
EXCLUDE="/tmp/exclude.dat"


{
echo "-----------------------------------------------"
echo "Start:" $0
echo "-----------------------------------------------"
date "+%a %m-%d-%y %H:%M:%S" 
echo "-----------------------------------------------"

options="-aNHxEh --delete --fileflags --exclude-from=$EXCLUDE --delete-excluded --force-change --progress --stats --protect-args"

destin='/Users/jdoe/Desktop/DEST/'

for source in $(cat /tmp/list.dat)
  do

   echo "Source:      " $source
   echo "Destination: " $destin

   echo "rsync" $options $source $destin
   /opt/local/bin/rsync $options $source $destin

   retcode=$?
      if [ $retcode -gt 0 ]
              then
                 echo "ERROR: rsync " $source "failed with code " $retcode
              fi

echo "-----------------------------------------------"
date "+%a %m-%d-%y %H:%M:%S"
echo "-----------------------------------------------"

done

echo "-----------------------------------------------"
echo "End:" $0
echo "-----------------------------------------------"

} >> $LOG 2>&1


exit 0
4

3 回答 3

2

将文件名用引号括起来:

cat "/tmp/path/file with spaces.txt"

同样,当您执行 rsync 时:

rsync $options "$source" "$destin"

报价总是一个好主意。

于 2013-08-06T17:26:34.533 回答
2

不要使用cat; 使用while循环read

while read -r source; do
    echo "Source:      $source"
    echo "Destination: $destin"
    echo "rsync" $options $source $destin
    if ! /opt/local/bin/rsync $options "$source" "$destin"; then
        echo "ERROR: rsync $source failed with code $?"
    fi

    echo "-----------------------------------------------"
    date "+%a %m-%d-%y %H:%M:%S"
    echo "-----------------------------------------------"

done < /tmp/list.dat
于 2013-08-06T17:32:24.353 回答
0

这是为那些感兴趣的人完成的工作脚本:

#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH

EMAILSUBJ="Rsync Results"
EMAILRECIP="admin@example.com"
EMAILSENDER="admin@example.com"
LOG="/Users/jdoe/Desktop/rsync-`/bin/date +%m%d%Y`.log"
EXCLUDELIST="/tmp/exclude.dat"
SOURCEFILE="/tmp/list.dat"
DESTINATION='/Users/jdoe/Desktop/DEST/'
RSYNC=/opt/local/bin/rsync
# Rsync options below:
ROPTIONS=(
    -aNHxEh
    --delete 
    --fileflags 
    --exclude-from=$EXCLUDELIST 
    --delete-excluded 
    --force-change 
    --stats 
    --protect-args
    )
# Get started
{
echo "-----------------------------------------------"
echo "Start:" $0
echo "-----------------------------------------------"
date "+%a %m-%d-%y %H:%M:%S" 
echo "-----------------------------------------------"

while IFS= read -r SOURCE; do


   echo "SOURCE: $SOURCE"
   echo "DESTINATION: $DESTINATION"

   echo "rsync" "${ROPTIONS[@]}" $SOURCE $DESTINATION
   $RSYNC "${ROPTIONS[@]}" "$SOURCE" "$DESTINATION"

  RETCODE=$?
      if [ $RETCODE -gt 0 ]
              then
                 echo " "
                 echo "##########################################################"
                 echo "ERROR: rsync " $SOURCE "FAILED with code " $RETCODE
                 echo "##########################################################"
                 echo " "
              fi

echo "-----------------------------------------------"
date "+%a %m-%d-%y %H:%M:%S"
echo "-----------------------------------------------"

done < $SOURCEFILE                                       

echo "-----------------------------------------------"
echo "End:" $0
echo "-----------------------------------------------"



} >> $LOG 2>&1

# Send an email
(
echo "From: $EMAILSENDER"
echo "To: $EMAILRECIP"
echo "Subject: $EMAILSUBJ"
cat $LOG

) | /usr/sbin/sendmail -it 

exit 0
于 2013-08-06T22:46:28.767 回答