2009-09-02 70 views
2

需要一些幫助,這是我的shell腳本技能是略小於L337 :(bash腳本存檔文件,然後將複製新的

我需要給gzip幾個文件,然後從另一頂複製較新的位置我需要能夠從其他腳本調用此腳本以下列方式

exec script.sh $oldfile $newfile 

任何人都可以點我在正確的方向

編輯:?要添加更多的細節:

該腳本將用於上傳到一個文件夾中的一些文件,每月更新,舊文檔需要歸檔到一個壓縮文件和新的文件,這些文件可以有不同的名稱,複製舊的頂部。該腳本需要通過另一個腳本的文檔大小來調用文檔。此腳本的基本流程應該是 -

  1. 腳本文件應該創建一個新的gzip 存檔使用特定名稱(從腳本前綴常數和當前月份和年份如prefix.september.2009創建.tar.gz)僅當它不存在 時,否則添加到現有的。
  2. 舊文件複製到歸檔。
  3. 取代舊文件與新的。

由於提前,
理查德

編輯:加在存檔文件名

+0

好了,但因爲要存儲多個文件作爲一個壓縮文件,你現在還需要指定存檔文件名作爲額外的參數,因爲它可以不再從舊文件中確定(您可以將舊文件添加到任何現有的存檔中,或者您可能想要創建一個新文件)。你能解決你想如何在你的工作流程中工作嗎? – 2009-09-02 15:59:30

+0

問題已更新:文件名應由腳本中的前綴常量以及當前的月份和年份創建,例如fileprefix.month.year.tar.gz – Frozenskys 2009-09-02 16:13:04

+0

我知道我可以使用類似於PREFIX =「fileprefix」; FILENAME = $(日期+「$ PREFIX。%B.%Y.gz」)的格式來格式化文件名,但怎麼做我檢查它是否存在,如果它添加更多文件? – Frozenskys 2009-09-02 16:39:46

回答

2

下面是基於您澄清的修改後的腳本。我用tar檔案,與gzip壓縮,在多個文件存儲在一個單一的存檔(不能存儲單獨使用gzip多個文件)。此代碼只是表面測試 - 它可能有一個或兩個錯誤,你應該添加更多的代碼等檢查命令的成功,如果你在憤怒中使用它。但它應該讓你在那裏的大部分。

#!/bin/bash 

oldfile=$1 
newfile=$2 

month=`date +%B` 
year=`date +%Y` 

prefix="frozenskys" 

archivefile=$prefix.$month.$year.tar 

# Check for existence of a compressed archive matching the naming convention 
if [ -e $archivefile.gz ] 
then 
    echo "Archive file $archivefile already exists..." 
    echo "Adding file '$oldfile' to existing tar archive..." 

    # Uncompress the archive, because you can't add a file to a 
    # compressed archive 
    gunzip $archivefile.gz 

    # Add the file to the archive 
    tar --append --file=$archivefile $oldfile 

    # Recompress the archive 
    gzip $archivefile 

# No existing archive - create a new one and add the file 
else 
    echo "Creating new archive file '$archivefile'..." 
    tar --create --file=$archivefile $oldfile 
    gzip $archivefile 
fi 

# Update the files outside the archive 
mv $newfile $oldfile 

保存爲script.sh,然後使其可執行:

chmod +x script.sh 

然後像這樣運行:

./script.sh oldfile newfile 

像``frozenskys.September.2009.tar.gz , will be created, and newfile will replace oldfile . You can call this script with exec`來自另一個腳本,如果你想。只要把這行寫在你的第二個腳本中:

exec ./script.sh $1 $2 
+0

這差不多:) - 但我需要能夠繼續添加更多的文件到檔案。我已經更新了這個問題,以便更好地解釋我想要做的事情的流程。 – Frozenskys 2009-09-02 15:46:52

+0

我已更新問題以添加更多詳細信息。 – Frozenskys 2009-09-02 16:41:04

+0

你是我的英雄,這很完美! – Frozenskys 2009-09-02 21:49:10

2

一個好的REFFERENCE任何的bash腳本是Advanced Bash-Scripting Guide模式的細節。

本指南介紹了每一件事情的bash腳本。

的基本方法我會採取的是:

Move the files you want to zip to a directory your create. 
    (commands mv and mkdir) 

zip the directory. (command gzip, I assume) 

Copy the new files to the desired location (command cp) 

以我的經驗的bash腳本主要是瞭解如何使用這些命令很好,如果你可以在命令行中運行它,你可以在你的腳本運行。

可能有用的另一個命令是

pwd - this returns the current directory 
+1

並且不要忘記錯誤條件,cp,mv,gzip等都可能失敗,你編寫的任何腳本應該儘可能健壯(好習慣),在試圖操作它們之前檢查$ oldfile和$ newfile是否存在。你寫的命令成功了,Perl有一個很好的方法來做到這一點:「some_command ||死了「some_command失敗」,但我們也可以在bash中做到這一點。 – Tim 2009-09-02 15:54:33

+0

好點 - 但我需要弄清楚如何讓腳本工作,然後才能添加錯誤處理:) – Frozenskys 2009-09-02 16:15:04

0

爲什麼你不使用版本控制?這很容易;只是檢查出來,並壓縮。

(道歉,如果這不是一個選項)

+0

好主意,這就是我用於我的代碼更新 - 使用優秀的beanstalkapp服務,但這不是這些文檔的選項。 – Frozenskys 2009-09-02 16:44:36

相關問題