我正在使用shell腳本讀取一個包含某些文件路徑的屬性文件。現在取決於這個文件路徑,我想創建zip文件的名稱。事情是這樣的......我的屬性文件內容::如何使用shell腳本在unix中創建所需的文件名
path=tmp/inputs/logs/abc
path=tmp/backup/inte/xyz
destpath=abc/xyz
現在我能夠創建文件名爲abc.zip和xyz.zip爲:
paths=`grep path myfile.property |cut -d= -f2`
d_path=`grep destpath myfile.property |cut -d= -f2`
filename=$d_path/$(basename $paths).zip
其中創建abc.zip和xyz.zip。但是我想通過獲取路徑的最後三個參數來創建名稱。事情是這樣的......
- 爲
abc.zip
應該inputs_logs_abc.zip
和 - 爲
xyz.zip
應該backup_inte_xyz.zip
編輯
Paths=`grep path myfile.txt |cut -d= -f2`
d_Path=`grep destpath myfile.txt |cut -d= -f2`
for s_Path in $Paths
do
prefix=${Paths%%/*/*/*}
without_prefix=${Paths##${prefix}/}
slashes_to_underscores=${without_prefix//\//_}
zipFile=$d_Path/${slashes_to_underscores}.zip
find $s_Path -type f -name "*.log" | xargs zip -mT $zipFile [email protected]
done
以上就是我code.By使用這個我不能達到我的目標。 有人可以幫助我嗎?
你正在編寫哪個shell?這些片段中的'destpath'和'd_path'的相關性是什麼? – Johnsyweb 2010-10-05 17:13:52
我正在使用bash.destpath是屬性文件中存在的關鍵字,據此我讀取此文件並將此路徑存儲在d_path變量中。 – MAS1 2010-10-05 17:52:46
然後我的答案應該可以解決你的問題。你可以在提示符下試一試,並在你去看它是如何工作的時候回顯變量。 – Johnsyweb 2010-10-05 18:36:11