在shell脚本中需要帮助for循环

时间:2016-06-17 05:06:16

标签: bash shell rsync

我是一个更新鲜的shell脚本。我有一个包含文件名的文件。我想rsync使用该文件名到目的地。我习惯但不工作。以下是剧本:

basedir=/appshare/customerdata/

backupdir=/backup/

rsync=/usr/bin/rsync

ls -ltrh $basedir | awk {'print $9'} | grep .[0-9] > /tmp/include.txt

for line in /tmp/include.txt
do
    rsync -auvH $basedir$line --exclude-from /tmp/exclude.txt $backupdir
done

运行时此脚本会出现以下错误: -

[root@practice Script]# ./customerdata 
sending incremental file list
rsync: change_dir "/appshare/customerdata//tmp" failed: No such file or directory (2)

sent 12 bytes  received 12 bytes  48.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]

1 个答案:

答案 0 :(得分:1)

行:

for line in /tmp/include.txt

应该是:

for line in `cat /tmp/include.txt`

另外,不要解析ls输出,你可以使用来自shell的globing:

for line in $basedir/?[0-9]*;
do
    rsync -auvH $basedir$line --exclude-from /tmp/exclude.txt $backupdir
done