【问题标题】:restore script in LinuxLinux中的恢复脚本
【发布时间】: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 子句中使用它时,它只是一个空字符串。

标签: linux bash shell


【解决方案1】:

这是清理后的语法:

#!/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

但不幸的是,我无法推断您在else 子句中的目标是什么;例如:mv -i $filename "$location1" $locationlocationlocation1 在此处使用之前均未定义。

【讨论】:

  • 如果我恢复 -n 文件名,它应该会询问我想用 -n 恢复文件的位置,我应该将文件恢复到原始位置
【解决方案2】:

grep 需要两个参数,一个模式和一个文件。你已经给了它三个,一个模式,命令cd,和一个文件。 cd 这里不需要。

【讨论】:

    猜你喜欢
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多