restore script in Linux

2019-09-05 05:56发布

问题:

I'm using Linux through a virtual machine,

for my coursework i have to create 3 scripts, one of they scripts is to restore deleted files to either a location the user puts in or to the original location

here is what i have for my script so far

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

but when i try restore the file i get an error saying

grep: cd: no such file or directory
mv: cannot move `test' to `': no such file or directory

the sricpt now works when i do restore -n but it still doesnt work when i do resotre

update of my script it now looks like :

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

now I'm getting the error: mv: missing destination file operand after `' when i try to do restore test.txt

回答1:

Here's the cleaned up syntax:

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

But unfortunately I am unable to infer what your goal is in the else clause; for instance: mv -i $filename "$location1" $location : neither location or location1 is defined before use here.



回答2:

grep expects two arguments, a pattern and a file. You've given it three, a pattern, the command cd, and a file. cd is not needed here.



标签: linux bash shell