2013-03-28 144 views
0

因此,我需要在bash中写一个脚本来备份目录的文件。目录(备份)文件的Bash脚本

脚本获取文件(备份文件列表)作为参数,最后一个参数必须是目标文件夹(目录)。如果目标文件夹不存在,则必须通过脚本创建。

我打算使用循环来移动参数列表(文件),但我不知道如何使用最后一个参数,并检查它是否存在。

脚本召唤:

./myScript.sh file1 file2 file3... fileN target_folder 

感谢。 :)

我开始:

#!/bin/bash 

#doing backup of files passed as list of arguments. 

if [ "$#" lt "2" ] 
then 
    echo usage: "./myScript.sh <list of arguments -files for backup.>" 
    exit 
fi 
for arg in "$#" 
do 
    if #last argument exist as folder in directory, just copy all files in 
      else #make targer folder and copy all files in 

done 
+0

它听起来像你改写'cp'? – Kent

+0

是的...任务很简单。我需要使用cp来做到这一点.. – gyrfalcon

回答

0

使用getopts并通过目录作为命名参数:

./myScript.sh -d target_folder file1 file2 file3... fileN 
+0

我需要使用最后一个参数(目标文件夹)的eval .. -.-' – gyrfalcon

+0

我没有看到什么阻止你在命名参数上使用'eval'。 –

+0

我解决了它..对不起,仍在学习。 :) – gyrfalcon