![](/img/trans.png)
[英]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.