2010-03-25 24 views
2

我有一個目錄,並在其中有幾個文件。我試圖解密這些文件並將它們移動到另一個目錄。我無法弄清楚如何設置輸出文件名並將其移動。Shell腳本解密和移動文件從一個目錄到另一個目錄

因此,目錄結構如下所示:

/Applications/MAMP/bin/decryptandmove.sh 
/Applications/MAMP/bin/passtext.txt 
/Applications/MAMP/bin/encrypted/test1.txt.pgp 
/Applications/MAMP/bin/encrypted/test2.txt.pgp 
/Applications/MAMP/htdocs/www/decrypted/ 

對於所有在加密的目錄中的文件,我試圖對其進行解密,然後將它們移動到目錄WWW /解密/。我不知道加密目錄中的文件名會提前(此腳本最終將通過cron作業運行),所以我只想輸出具有相同文件名的解密文件,但沒有pgp。因此,結果將是:

/Applications/MAMP/bin/decryptandmove.sh 
/Applications/MAMP/bin/passtext.txt 
/Applications/MAMP/bin/encrypted/ 
/Applications/MAMP/htdocs/decrypted/test1.txt.pgp 
/Applications/MAMP/htdocs/decrypted/test2.txt.pgp 

所以,這是我迄今爲止所寫的,它不起作用。 FILE和FILENAME都是錯誤的。我甚至沒有接觸到移動部分。

pass_phrase=`cat passtext.txt|awk '{print $1}'` 

for FILE in '/Applications/MAMP/bin/encrypted/'; 
do 
    FILENAME=$(basename $FILE .pgp) 
    gpg --passphrase $pass_phrase --output $FILENAME --decrypt $FILE 
done 

回答

1
#!/bin/bash 
p=$(<passtext.txt) 
set -- $p 
pass_phrase=$1 
destination="/Applications/MAMP/htdocs/www/decrypted/" 
cd /Applications/MAMP/bin/encrypted 
for FILE in *.pgp; 
do 
    FILENAME=${FILE%.pgp} 
    gpg --passphrase "$pass_phrase" --output "$destination/$FILENAME" --decrypt "$FILE" 
done 
+0

我一直在嘗試使用這個,但我不斷收到: gpg:/ Applications/MAMP/bin/encrypted /:讀取錯誤:是目錄 gpg:decrypt_message失敗:eof – KittyYoung 2010-03-25 02:51:49

+0

哦,等等...你改變它:)讓我再試一次... – KittyYoung 2010-03-25 02:52:30

+0

它是完美的!非常感謝你。我完全不瞭解大部分的語法,但我一定會試着弄清楚它的含義。 – KittyYoung 2010-03-25 03:03:46

0

這應該修復FILENAME和FILE問題,儘管只有當您處於「解密」目錄中時它纔會起作用。如果要解決這個問題,你必須正確的目錄添加到文件名可變:)

pass_phrase=`cat passtext.txt|awk '{print $1}'` 

for FILE in /Applications/MAMP/bin/encrypted/*; do 
    FILENAME=$(basename $FILE .pgp) 
    gpg --passphrase $pass_phrase --output $FILENAME --decrypt $FILE 
done 
0

我喜歡做cd第一:

cd /Applications/MAMP/bin/encrypted/ 

然後

for FILE in $(ls); do ... 

for FILE in `ls`; do ... 

我個人比較喜歡:

ls | while read FILE; do ... 

那麼也許

cd - 

重要:如果使用ls方法,確保您的文件名不包含空格。另請參閱ghostdog74評論中的鏈接(謝謝,BTW) - 這個頁面通常非常有用。

+0

沒用使用ls' – ghostdog74 2010-03-25 01:18:08

+0

@ ghostdog74的':也許你是對的,但使用LS有一定的優勢:1)你不必使用基本名稱擺脫不必要的路徑。 2)您可以使用ls的所有選項,甚至在使用這些值之前通過其他工具管理它。 – 2010-03-25 01:54:24

+1

在for循環中使用'ls'完全沒有優勢。如果文件名具有空格,則會分叉另一個進程並容易出錯。至於2),你沒有使用'ls -l',所以沒有選項可以談論 – ghostdog74 2010-03-25 02:07:02

相關問題