bash for 循环排除特定子目录

bash for 循环排除特定子目录

我有一个使用口是心非执行目录备份的脚本。我们想从“for”循环中排除一些子目录。我该如何实现这一目标?

当前脚本

backup_volume() { TARGET_URL=$1
   for vol in /dir/*; do
      VOLUME=$(basename $vol)
      duplicitiy backupcommand...
   done
}

/dir/ 内有一些我们想要排除的子目录..任何提示将不胜感激!

答案1

duplicity知道排除目录

duplicity --exclude=/dir/somedir/someotherdir/dontbackup ...other options...

/dir(这将运行一次来​​备份整个)

如果您需要为 的每个子目录进行单独备份/dir,首先,请确保使用模式匹配子目录/dir/*/,然后根据您想要避免的文件名测试目录的文件名:

for vol in /dir/*/; do
    case $vol in
        */somedirectory/)    continue ;;
        */someotherdirtory/) continue ;;
    esac

    volume=$( basename "$vol" )

    duplicity ...
done

或者,

for vol in /dir/*/; do
    volume=$( basename "$vol" )

    case $volume in
        somedirectory)    continue ;;
        someotherdirtory) continue ;;
    esac

    duplicity ...
done

或者,

for vol in /dir/*/; do
    volume=$( basename "$vol" )

    case $volume in
        somedirectory|someotherdirectory|more here)    continue ;;
    esac

    duplicity ...
done

或者,使用扩展的通配模式bash

shopt -s extglob

for vol in /dir/!(somedirectory|someotherdirectory|...)/
    volume=$( basename "$vol" )
    duplicity ...
done

相关内容