1

这是一个非常简短的问题。$example但是在 bash 文件中将变量作为 tar 的参数放置是否存在语法错误?

我将文件写为

//only portion that really matters
#!/bin/bash
...
tar -cvpzf $filename $backup_source

//here's the actual code
#!/bin/bash
backup_source="~/momobobo"
backup_dest="~/momobobo_backup/"
dater=`date '+%m-%d-%Y-%H-%M-%S'`
filename="$backup_dest$dater.tgz"
echo “Backing Up your Linux System”
tar -cvpzf $filename $backup_source
echo tar -cvpzf $filename $backup_source
echo “Backup finished”

//and heres the error
“Backing Up your Linux System”
tar: ~/momobobo: Cannot stat: No such file or directory
tar (child): ~/momobobo_backup/07-02-2013-18-34-12.tgz: Cannot open: No such file or directory
tar   (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar -cvpzf ~/momobobo_backup/07-02-2013-18-34-12.tgz ~/momobobo

注意“echo tar ...”。当我复制并粘贴输出并在终端中运行它时,tar文件没有问题。我目前正在运行 Xubuntu,并且已经进行了更新。

4

1 回答 1

6

~不会用双引号扩展到您的主目录。

只需删除双引号:

backup_source=~/momobobo
backup_dest=~/momobobo_backup/

如果您有想要引用的内容,您可以使用~/"momobobo"

于 2013-07-03T00:28:13.893 回答