linux - Bash 脚本 : if any argument is "N" then function has extra options

标签 linux bash shell

(我会重复使用它,因为它是关于同一件事的)。
我已经设法制作了一个可用的脚本,它可以执行代码块下面解释的所有内容,除了参数的顺序,即:

./test.sh B N A

将删除B.zip , 创建一个新的存档,但停在那里,A不会被处理。没关系,我可以保留N作为最后一个参数,没问题。此外,echo "no backup needed, removing"由于某种原因不起作用,但这是可选的。

我的问题是:能否以某种方式改进/更改我所做的,以便如果及时添加其他文件夹,对脚本的唯一更改是 DIRx条目?我看到的最大问题是修改 <start> 之间的 block 和 <stop> .这是脚本:


#!/bin/bash

DIR1=A
DIR2=B
DIR3=C

bak()
{
    if [[ "$*" == N ]]
    then
        if [ $1 == N ]
        then
            :
        else
            echo "no backup needed, removing"
            rm -v $1.zip
        fi
    else
        if
            [ -f $1.zip ]
        then
            /bin/mv -vi $1.zip BKP/$1.zip_`/bin/date +"%H-%M"`
        else
            echo "no "$1".zip"
        fi
    fi
}

archive()
{
    if [ $* == N ]
    then
        :
    else
        if [ $1 == C ]
        then
            7z a -mx=9 $1.zip ../path/$1 -r -x\!$1/nope
        else
            7z a -mx=9 $1.zip $1 -r -x\!$1/bogus
        fi
    fi
}

########### <start> ####################
if [ -z "$*" ] || [[ "$#" -eq 1 && "$1" == N ]]
then
    bak "$DIR1"
    bak "$DIR2"
    bak "$DIR3"
    archive "$DIR1"
    archive "$DIR2"
    archive "$DIR3"
fi
############## <stop> #####################

#if [[ "$#" -eq 1 && "$1" == N ]]
#then
#   rm -v "$DIR1".zip
#   rm -v "$DIR2".zip
#   rm -v "$DIR3".zip
#   archive "$DIR1"
#   archive "$DIR2"
#   archive "$DIR3"
#fi

if [[ "$#" -gt 1 && "$*" == *N* ]]
then
    while [ "$#" -gt 1 ]
    do
        if [ "$1" == N ]
        then
            :
        else
            rm -v "$1".zip
        fi
        archive "$1"
        shift
    done
else
    while [ "$#" -ge 1 ]
    do
        bak "$1"
        archive "$1"
        shift
    done
fi
exit 0

现在,这就是我所拥有的和想要它做的。当前目录包含脚本,test.sh , 和文件夹 AB . ls -AR产生这个:

A  B  test.sh

./A:
1.txt  2.txt  bogus

./B:
3.txt  4.txt  bogus

还有另一个文件夹,C , 在 ../path/ .同ls -AR ../path给出这个:

../path:
C

../path/C:
5.txt  6.txt  nope

../path/C/nope:
q.doc  w.rtf

我希望脚本做什么。不带参数运行时:

./test.sh

1) 检查当前目录中的现有 zip 存档
1.a) 如果它们存在,则为每个备份,并在 BKP/. 中添加额外的日期后缀
2.a) 如果没有,它会让你知道
2) 三个文件夹,A , BC已存档,文件夹 AB没有A/bogusB/bogus和文件夹 C没有../path/C/nope/*../path/C/nope/ .

如果带参数运行,这些参数可以是 A 中的任何一个, BC , 带有可选的 N .如果使用 N 运行, 只有:

./test.sh N

然后将不执行存档检查/备份,任何已存在的存档将被删除并且所有 3 个文件夹都将被存档。如果使用 A 的任意组合运行, BC ,例如:

./test.sh A C

然后只存档A.zipC.zip进行检查和备份,只有文件夹 AC已存档,A没有A/bogusC没有../path/C/nope/*../path/C/nope/ .如果使用 A 的任意组合运行, BC , 但有额外的 N ,即:

./test.sh B N C

然后不对B.zip执行检查/备份和 C.zip ,文件(如果存在)被删除,文件夹 BC已存档。

存档将(在内部)文件夹作为根目录(即打开存档,您将首先看到 ABC)并且这三个都在列表中有异常(exception)要处理的文件数:AB不需要bogus , 而 C不需要子文件夹 none以及里面的任何东西。我用 7z而不是 zip因为我可以写:

7z a x.zip ../path/./C/bla/bla

并且有C作为根目录;我无法用 zip 做到这一点(很可能我不知道怎么做,只要它有效就没关系)。

至此,检查和备份工作。归档,如果没有添加异常,我会删除 $PATH事情,工作。整个脚本没有。我会发布我到目前为止所做的每一个组合,但其中 99% 可能是不可能的,其余的很幼稚。我不在乎它的外观,只要它能完成工作即可。


非常可选:可以为“Supercalifragilistic”创建别名(或某种别名),如“SCF”吗? C文件夹有一个相当长的名字(我知道我可以做一个符号链接(symbolic link))。我对这个一无所知。

最佳答案

我成功了。这可能是非常幼稚和最糟糕的剧本,但我现在不在乎,它有效。当然,如果有人有更好的选择来分享,欢迎分享,在此之前这是整个脚本:


#!/bin/bash
# ./test.sh     = 1. searches for existing archives
#               1.a. if they exist, it backups them into BKP/.
#               1.b. if not, displays a message
#             2. archives all the directories in the array list
# ./test.sh N       = 1. deletes all the folder's archives existent and
#           specified in the array list
#             2. archives all the directories in the array list
# ./test.sh {A..F}  = 1. searches for existing archives from arguments
#               1.a. if they exist, it backups them into BKP/.
#               1.b. if not, displays a message
#             2. archives all the directories passed as arguments
# ./test.sh {A..F} N    = 1. deletes all the archives matching $argument.zip
#             2. archives all the directories passed as arguments

# The directories to be backed-up/archived, all in the current (script's) path
# except "C", on a different path
DIR=(A B C D E F)

# The back-up function, if any argument is "N", processing it is omitted
bak()
{
    if [[ "$*" == N ]]
    then
        :
    else
        if
            [ -f $1.zip ]
        then
            mv -vi $1.zip BKP/$1.zip_`date +"%H-%M"`
        else
            echo "$(tput setaf 1) no "$1".zip$(tput sgr0)"
        fi
    fi
}

# The archive function, if any argument is "N", processing it is omitted. Folder
# "C" has special treatment
archive()
{
    if [ $* == N ]
    then
        :
    else
        if [ $1 == C ]
        then
            7z a -mx=9 $1.zip ../path/$1 -r -x\!$1/nope
        else
            7z a -mx=9 $1.zip $1 -r -x\!$1/bogus
        fi
    fi
}

# case #1: no arguments
if [ -z "$*" ]
then
    for i in $(seq 0 $((${#DIR[@]}-1))) # counts from 0 to array-1
    do
        echo "$(tput setaf 2) backup$(tput sgr0)"
        bak "${DIR[i]}"
        archive "${DIR[i]}"
    done
    exit $?
fi

# case #2: one argument, "N"
if [[ "$#" -eq 1 && "$1" == N ]]
then
    for i in $(seq 0 $((${#DIR[@]}-1)))
    do
        echo "$(tput setaf 1) no backup needed, removing$(tput sgr0)"
        rm -v "${DIR[i]}".zip
        archive "${DIR[i]}"
    done
    exit $?
fi

# case #3: folders as arguments with "N"
if [[ "$#" -gt 1 && "$*" == *N* ]]
then
    while [ "$#" -gt 1 ]
    do
        if [ "$1" == N ]
        then
            :
        else
            echo "$(tput setaf 1) no backup needed, removing$(tput sgr0)"
            rm -v "$1".zip
        fi
        archive "$1"
        shift
    done
# case #4: folders as arguments without "N"
else
    while [ "$#" -ge 1 ]
    do
        echo "$(tput setaf 2) backup$(tput sgr0)"
        bak "$1"
        archive "$1"
        shift
    done
fi
exit $?

关于linux - Bash 脚本 : if any argument is "N" then function has extra options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155046/

相关文章:

php - 使用 Web 表单 (PHP) 重置 Linux 密码

linux - 通过在 bash 中附加来有条件地合并文件

c++ - 如何使用 gflags 为您自己的项目获取 bash 选项卡补全?

python - 搜索大tar.gz文件中的关键字,复制并删除

shell - awk 语法错误 : awk: line 29: syntax error at or near :

linux - 不带 shell 与内核通信

linux - 如何在 bash for 循环中使用变量

bash - 使用容器内的 shell 文件设置系统ENV

linux - 如何在 bash shell 脚本中解析文件

eclipse - Eclipse 中的配置文件编辑器