Linux的拉链和排除通过的bash / shell脚本目录 [英] linux zip and exclude dir via bash/shell script

查看:161
本文介绍了Linux的拉链和排除通过的bash / shell脚本目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个bash / shell脚本,拉上一个特定的文件夹,并忽略某些子显示目录的文件夹中。

I am trying to write a bash/shell script to zip up a specific folder and ignore certain sub-dirs in that folder.

这是我想要的zip文件夹 sync_test5

This is the folder I am trying to zip "sync_test5":

我的bash脚本生成忽略列表(基于 ),并调用这样的zip功能:

My bash script generates an ignore list (based on) and calls the zip function like this:

#!/bin/bash

SYNC_WEB_ROOT_BASE_DIR="/home/www-data/public_html"
SYNC_WEB_ROOT_BACKUP_DIR="sync_test5"
SYNC_WEB_ROOT_IGNORE_DIR="dir_to_ignore dir2_to_ignore"

ignorelist=""
if [ "$SYNC_WEB_ROOT_IGNORE_DIR" != "" ];
then
    for ignoredir in $SYNC_WEB_ROOT_IGNORE_DIR
    do
        ignorelist="$ignorelist $SYNC_WEB_ROOT_BACKUP_DIR/$ignoredir/**\*"
    done
fi

FILE="$SYNC_BACKUP_DIR/$DATETIMENOW.website.zip"
cd $SYNC_WEB_ROOT_BASE_DIR;
zip -r $FILE $SYNC_WEB_ROOT_BACKUP_DIR -x $ignorelist >/dev/null
echo "Done"

现在这个脚本运行没有错误,但它是的忽略/排除我指定显示目录。

Now this script runs without error, however it is not ignoring/excluding the dirs I've specified.

所以,我有shell脚本输出它试图运行的命令,这是:

So, I had the shell script output the command it tried to run, which was:

zip -r 12-08-2014_072810.website.zip sync_test5 -x  sync_test5/dir_to_ignore/**\* sync_test5/dir2_to_ignore/**\*

现在如果我直接在腻子运行上面的命令就是这样,它的工作原理:

Now If I run the above command directly in putty like this, it works:

那么,为什么没有我的shell脚本排除按预期工作?正被执行的命令是相同的(在外壳和油灰直接地)。

So, why doesn't my shell script exclude working as intended? the command that is being executed is identical (in shell and putty directly).

推荐答案

由于在一个变量反斜杠quotings分词后,不进行评估。

Because backslash quotings in a variable after word splitting are not evaluated.

如果您有 A ='123 \\ 4'回声$ A 会给

123\4

但如果你直接做这样回声123 \\ 4 ,你会得到

1234

显然你传递的变量和没有变量的参数是不同的。

Clearly the arguments you pass with the variable and without the variables are different.

您可能只是为了与反斜杠引用你的观点:

You probably just meant to not quote your argument with backslash:

ignorelist="$ignorelist $SYNC_WEB_ROOT_BACKUP_DIR/$ignoredir/***"

顺便说一下,有什么实际的作品是不作价glob模式:

Btw, what actual works is a non-evaluated glob pattern:

zip -r 12-08-2014_072810.website.zip sync_test5 -x 'sync_test5/dir_to_ignore/***' 'sync_test5/dir2_to_ignore/***'

您可以验证这一点。

echo zip -r 12-08-2014_072810.website.zip sync_test5 -x  sync_test5/dir_to_ignore/**\* sync_test5/dir2_to_ignore/**\*

这是我的建议:

#!/bin/bash

SYNC_WEB_ROOT_BASE_DIR="/home/www-data/public_html"
SYNC_WEB_ROOT_BACKUP_DIR="sync_test5"
SYNC_WEB_ROOT_IGNORE_DIR=("dir_to_ignore" "dir2_to_ignore")

IGNORE_LIST=()
if [[ -n $SYNC_WEB_ROOT_IGNORE_DIR ]]; then
    for IGNORE_DIR in "${SYNC_WEB_ROOT_IGNORE_DIR[@]}"; do
        IGNORE_LIST+=("$SYNC_WEB_ROOT_BACKUP_DIR/$IGNORE_DIR/***")  ## "$SYNC_WEB_ROOT_BACKUP_DIR/$IGNORE_DIR/*"  perhaps is enough?
    done
fi

FILE="$SYNC_BACKUP_DIR/$DATETIMENOW.website.zip"  ## Where is $SYNC_BACKUP_DIR set?
cd "$SYNC_WEB_ROOT_BASE_DIR";
zip -r "$FILE" "$SYNC_WEB_ROOT_BACKUP_DIR" -x "${IGNORE_LIST[@]}" >/dev/null
echo "Done"

这篇关于Linux的拉链和排除通过的bash / shell脚本目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆