bash脚本:如果任何参数是" N"然后函数有额外的选项 [英] Bash script: if any argument is "N" then function has extra options
问题描述
(我将利用这个,因为它是同样的事情)。结果
我已成功地使一个可用的脚本,做一切code座下面发生了什么解释,只是的参数的顺序,即:
(I'll reuse this as it's about the same thing).
I have managed to make a usable script that does everything what's explained below the code block, except the order of the arguments, i.e.:
./ test.sh乙N A
将删除 B.zip
,创建一个新的存档,但停在那里, A
将不会被处理。没关系,我可以让 N
作为最后一个参数,没有问题。此外,回声没有备份需要,删除
不出于某种原因,但这是相当可选的。
will delete B.zip
, create a new archive, but stops there, A
will not be processed. It's fine, I can keep N
as the last argument, no problem. Also, the echo "no backup needed, removing"
does not work for some reason, but that's rather optional.
我的问题是:我做了什么改进/某种程度上改变,因此,如果有及时添加其他文件夹的唯一改动剧本是 DIRX
项?我看到的最大的问题是修改的&LT块;开始>
和<停止>
。下面是脚本:
My question is: can what I have done be improved/altered somehow so that if there are other folders to be added in time the only changes to the script to be the DIRx
entries? The biggest problem I see is modifying the block between <start>
and <stop>
. Here is the script:
#!/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
和文件夹 A
和 B
。 LS-AR
产生这样的:
Now, here's what I have and want it to do. The current directory holds the script, test.sh
, and the folders A
and B
. ls -AR
produces this:
A B test.sh
./A:
1.txt 2.txt bogus
./B:
3.txt 4.txt bogus
有另外一个文件夹, C
在 ../路径/
。同样 LS-AR ../路径
给出了这样的:
There is another folder, C
, in ../path/
. The same ls -AR ../path
gives this:
../path:
C
../path/C:
5.txt 6.txt nope
../path/C/nope:
q.doc w.rtf
我想剧本做什么。如果不带参数运行:
What I want the script to do. When run with no arguments:
./ test.sh
1)检查在当前目录中搜索现有的ZIP档案
1.A),如果存在的话,备份是由每个附加日期后缀为 BKP /.
结果
2.A)如果不是,它可以让你知道结果
2)三个文件夹, A
, B
和 C
存档,文件夹 A
和 B
没有 A /假
和 B /假
和文件夹 C
没有 ../路径/ C /没了/ *
和 ../路径/ C /没了/
。
1) checks for existing zip archives in the current directory
1.a) if they exist, a backup is made for each with additional date suffix into BKP/.
2.a) if not, it lets you know
2) the three folders, A
, B
and C
are archived, folders A
and B
without A/bogus
and B/bogus
and folder C
without ../path/C/nope/*
and ../path/C/nope/
.
如果带参数运行,这些都可以是任何 A
, B
或的C
,一个可选的 N
。如果与运行 N
,只:
If run with arguments, these can be any of A
, B
or C
, with an optional N
. If run with N
, only:
./ test.shñ
再没有归档检查/备份将被执行,已存在的将被删除任何档案和所有3个文件夹获取存档。如果用 A
, B的任意组合运行
或 C
例如:
then no archive check/backup will be performed, any archives already existent will be deleted and all 3 folders get archived. If run with any combination of A
, B
or C
, for example:
./ test.sh A C
那么只有档案 A.zip
和 C.zip
有检查,备份和文件夹仅 A
和 C
存档, A
没有 A /假
和 C
没有 ../路径/ C /没了/ *
和 ../路径/ C /没了/
。如果用 A
, B的任意组合运行
或 C
,但与其他 N
,即:
then only archives A.zip
and C.zip
have a check and backup and only folders A
and C
are archived, A
without A/bogus
and C
without ../path/C/nope/*
and ../path/C/nope/
. If run with any combination of A
, B
or C
, but with additional N
, i.e.:
./ test.sh乙ñç
当为 B.zip
进行校验/备份和 C.zip
,档案(如果存在的话)被删除和文件夹 B
和 C
存档。
Then no check/backup is performed for B.zip
and C.zip
, the archives (if existent) get deleted and the folders B
and C
are archived.
该档案将(内部)的文件夹的根目录下(即打开存档,您会看到 A
, B
或 C
第一)和他们三个都有例外的文件列表进行处理: A
和 B
不需要假
,而 C
不需要的子文件夹无
和任何里面。我使用 7Z
而不是拉链
,因为我可以这样写:
The archives will have (inside) the folder as the root directory (i.e. open up the archive and you'll see A
, B
or C
first) and all three of them have exceptions to the list of files to be processed: A
and B
don't need bogus
, while C
doesn't need subfolder none
and anything inside it. I use 7z
instead of zip
because I can write:
7Z一个x.zip ../路径/./ C / BLA / BLA
和具有 C
作为根目录;我不能拉链
做(最有可能的,我不知道怎么样,它不只要它的工作原理物质)。
and have C
as the root directory; I couldn't do it with zip
(most likely I don't know how to, it doesn't matter as long as it works).
到目前为止,检查和备份工作。归档,如果没有异常增加,并删除了 $ PATH
的事,工作。整个脚本没有。我会发布到目前为止,我所做的每一组合,但其中99%的人会可能已经是不可能的,其余的孩子气。我一点也不在乎它的外观,只要它的工作。
So far, the checking and the backup work. The archiving, if no exceptions are added and I remove the $PATH
thing, work. The whole script doesn't. I would have posted every combination I have done so far, but 99% of them would have probably been impossible and the rest childish. I couldn't care less how it looks as long as it does the job.
很随意:可以别名(或某种)像SCF提出,要Supercalifragilistic?在 C
文件夹中有一个相当长的名字(我可以只让一个符号,我知道)。我不知道这一个想法。
Very optional: can an alias (or some sort) like "SCF" be made to "Supercalifragilistic"? The C
folder has a rather long name (I could just make a symlink, I know). I have no idea about this one.
推荐答案
我设法做到这一点。这也可能是非常幼稚和可能出现的最坏的脚本,但我现在不关心,它的作品。当然,如果有人有一个更好的选择,分享,这是值得欢迎的,在那之前这里是整个脚本:
I managed to do it. It's probably very childish and the worst possible script but I don't care right now, it works. Of course, if somebody has a better alternative to share, it's welcome, until then here's the whole script:
#!/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 $?
这篇关于bash脚本:如果任何参数是&QUOT; N&QUOT;然后函数有额外的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!