在Linux中恢复脚本

时间:2012-11-28 21:55:59

标签: linux bash shell

我正在通过虚拟机使用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现在有效 但是当我做resotre时它仍然无法正常工作

我的脚本更新现在看起来像:

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

之后缺少目标文件操作数

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

grep需要两个参数,一个模式和一个文件。你给它三个,一个模式,命令cd和一个文件。

}这里不需要cd