[英]How to write a bash shell script which takes one argument (directory name)
[英]How to write a script for bash to archive a directory and keep its name?
對於我的 uni,我需要編寫一個簡單的腳本來備份幾個目錄並將它們移動到另一個目錄。 我對 Linux 很陌生,所以我有點迷路了。 在此之前,他們展示了如何使用 tar 歸檔並使用 gzip 壓縮,所以我假設這就是我需要進行這些備份的方式。 它也是初學者級別,所以腳本應該盡可能簡單。 這是我的腳本:
#!/bin/bash
echo
directories="work/ extract/ test/"
for directory in $directories
do
tar --create --gzip --file= ~/backups "$directory".tgz $directory
done
這就是結果:
pi@raspberry:~ $ ./my_backup.sh
tar: Removing leading `/' from member names
tar (child): : Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: : Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar: Removing leading `/' from member names
tar (child): : Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: : Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar: Removing leading `/' from member names
tar (child): : Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: : Cannot write: Broken pipe
tar: Child returned status 2
tar: Error is not recoverable: exiting now
我只能猜測這與名稱有關,因為我認為名稱中不允許使用“/”,但我不知道如何刪除它。 我在我大學的論壇上問過這個問題,但沒有人回答。 非常感謝你的幫助。
我認為您可以將目錄放入列表中,並通過 for 循環遍歷它們。 所以而不是
directories="work/ extract/ test/"
寫
directories=("work" "extract" "test")
如果您在與需要備份的目錄相同的目錄中使用腳本。
我還不能測試它,但我認為它應該以這種方式工作。
#!/bin/bash
set -e
directories=(work extract test)
for directory in ${directories[@]}; do
tar -czf "${HOME}/backups/${directory}.tgz" "${directory}"
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.