【发布时间】:2012-11-29 08:37:22
【问题描述】:
我正在通过虚拟机使用 Linux,
对于我的课程,我必须创建 3 个脚本,其中一个脚本是将已删除的文件恢复到用户放置的位置或原始位置
这是到目前为止我的脚本所拥有的内容
#!/bin/sh
if [ "$1" == "-n" ]
then
cd /root/michael/trash
restore`grep "$2" cd /root/michael/store`
filename=`basename "$restore"`
echo "Where would you like to save the file?"
read location
location1=`readlink -f "$location"`
mv -i $filename "location1"/filename
else
cd /root/michael/trash
restore=`grep "$2" cd /root/michael/store`
filename=`basename "$restore"`
mv -i $filename "location1" $location
fi
但是当我尝试恢复文件时出现错误提示
grep: cd: no such file or directory
mv: cannot move `test' to `': no such file or directory
当我恢复 -n 时,sricpt 现在可以工作了 但是当我重新做时它仍然不起作用
我的脚本的更新现在看起来像:
#!/bin/sh
if [ "$1" == "-n" ]
then
cd /root/michael/trash
restore`grep "$2" /root/michael/store`
filename=`basename "$restore"`
echo "Where would you like to save the file?"
read location
location1=`readlink -f "$location"`
mv -i $filename "location1"/filename
else
cd /root/michael/trash
restore=`grep "$2" /root/michael/store`
filename=`basename "$restore"`
mv -i $filename "location1" $location
fi
现在我得到了错误: mv:当我尝试恢复 test.txt 时,在 `' 之后缺少目标文件操作数
【问题讨论】:
-
你已经发布了stackoverflow.com/q/13610821/242583 几乎完全相同的代码,事实上,你在这个中有点搞砸了。在你问另一个问题是否真的有必要之前,你应该整理并了解你在上一个问题中犯了什么错误。我建议你先阅读 shell 脚本教程。
-
其实看帖子名字的不是我发的,分明是2个人
-
道歉。我什至懒得看海报,我只是假设因为一些明显的错误。不过,您需要先阅读一些教程,如果该课程是关于 shell 脚本的,我猜是这样。这个问题与恢复脚本无关,而是如何解决你的错误。
-
当我恢复 -n 时它现在可以工作,但是当我尝试恢复 test.txt 时出现错误 mv: missing destination file operand after `test.txt'
-
变量 $location 在第一个 if 子句中设置。当此子句未运行时,变量未定义。因此,当您尝试在 else 子句中使用它时,它只是一个空字符串。