2013-03-28 141 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