0

我通过虚拟机使用 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 时,`' 后缺少目标文件操作数

4

2 回答 2

2

这是清理后的语法:

#!/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" $location: 在这里使用之前都没有locationlocation1没有定义。

于 2012-11-28T21:59:15.327 回答
1

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

于 2012-11-28T22:03:49.657 回答