使用'cat'来为其中包含空格的文件名

时间:2013-08-06 17:24:51

标签: macos shell rsync

我想在这里替换'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

3 个答案:

答案 0 :(得分:2)

用引号括起文件名:

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

同样,当你做rsync时:

rsync $options "$source" "$destin"

行情总是一个好主意。

答案 1 :(得分: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

答案 2 :(得分: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