0

Although I was able to figure out my first script and get it to run, I am having issues with my second script. Basically I need to retrieve my deleted files and restore them in my created home directory. I was able to get some of question 2, and all of 3 and 4 to work successfully. The issue I have with question 2 is if I type up the file that I want to restore, it will pull up the file and inode number which is fine. However, if I run the script without the desired file, it will show a bunch of inodes and the command "Missing Operand". The missing operand command is one that I want since I created it in order to test that the files I want to restore exist. I have attached my code below. Again, I wonder what I got wrong. I did some of the suggestions shellcheck offered but I still got the same results.

Here is my code:

#!/bin/bash

#2.
fileName=$1
inode=$(ls -i $1 | cut -d " " -f1)
echo "safe_rm_restore $1_$inode"

#3.
function movingfile(){

    if [ -e $HOME/deleted ] ;
    then
        mv $1_$inode  $HOME/deleted  $(grep $1 $HOME/.restore.info | cut -d" " -f3)
    fi
 }
 movingfile $1

#4.
function safe(){
    filename=$1

    if [ ! -e $1 ] ;
    then
        echo "safe_rm_restore :cannot restore '$1': No such file or directory"
        exit 1
    elif [ $# -eq 0 ] ;
    then
        echo "safe_rm_restore: cannot restore '$1' : Missing operand"
        exit 1
    fi
}
safe $1

##5.
function restoreFile(){
    if [ -e $(grep  $1 $HOME/.restore.info | cut -d" " -f2) ] ;
    then
        read -p "File currently exists. Do you want to overwrite y/n?: word"
        if [[ $word = "yes" || $word = "Y" || $word = "y" ]] ;
        then
            rm $(grep $1 $HOME/.restore.info | cut -d" " -f2)
            mv $HOME/deleted $(grep $1/.restore.info | cut -d" " -f2)
        fi
        if [[ $word = "n" ]] ;
        then
            rm $1
            exit 0
        else
            echo "Invalid Option"
            exit 1
        fi
    fi
}
restoreFile $1

##6.
function restored(){

    remove=$(grep -v $1 $HOME/.restore.info | tee $HOME/.restore.info)
    if [[ ! -e $HOME/deleted && -e $(grep $1 $HOME/.restore.info | -d":" -f2)]];
    then
        $remove
    fi
}
restored $1

I've followed the suggestions that you all gave me (and it moved me towards the right direction) but I still had some issues getting my operations to fully work. For instance, if no file name was given, my script was supposed to say Missing Operand but instead it gives all inode numbers in my file and my script freezes for sometime. Here is my updated second bash script. The indention looks great from my UNIX system but is somehow skewed when I transfer it on here. Thank you again!

#!/bin/bash


##2.
fileName="$1"
inode="$(ls -i $1 |cut -d" " -f1)"
echo "safe_rm_restore $1_$inode"
touch "$1"










##3.
movingfile(){


        if [ -e $HOME/deleted ] ;
        then
        touch $1_$inode
                mv $1_$inode  $HOME/deleted  $(grep $1 $HOME/.restore.info | cut -d" "  -f4)

 else
 exit 1
 fi
        }
        movingfile $1


##4.
safe(){
filename=$1

        if [ ! -e $1 ] ;
        then
                echo "safe_rm_restore :cannot restore '$1': No such file or directory"
                exit 1
        elif [ $# -eq 0 ] ;
        then
                echo "safe_rm_restore: cannot restore '$1' : Missing operand"
                exit 1
        fi
        }
safe $1


##5.
restoreFile(){
        if [ -e $(grep  $1 $HOME/.restore.info | cut -d" " -f4) ] ;
                then
read -p "File currently exists. Do you want to overwrite y/n?:" word
        if [[ $word = "yes" || $word = "Y" || $word = "y" ]] ;
        then

                mv $1_$inode  $HOME/project $(grep $1/.restore.info | cut -d" " -f4)
fi
        if [[ $word = "n" ]] ;
        then
        rm -r $1_$inode
  exit 0
else
        echo "Invalid Option"
        exit 1
fi
fi
}
restoreFile $1

##6.
restored(){

remove=$(grep -v $1 $HOME/.restore.info | tee $HOME/.restore.info)
if [[ ! -e $HOME/deleted && -e $(grep $1 $HOME/.restore.info |cut -d":" -f4) ]];
then
        $remove
fi
}
restored $1
OceanOO
  • 1
  • 4
  • Almost definitely a quoting issue - make sure you wrap your variables in double quotes. Get your script checked at [shellcheck.net](http://shellcheck.net). And check this post [When to wrap quotes around a shell variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable). – codeforester Aug 11 '17 at 03:42
  • 1
    Also, run your script with `bash -x script.sh` and find out which line is failing. – codeforester Aug 11 '17 at 03:46
  • I've edited your code to make it more readable. Try to use consistent indentation, it will pay off in the long term and make your code much easier to debug. Beginners often don't see the need, but if you look at any experienced programmer's code you will see it is formatted well. – cdarke Aug 11 '17 at 06:53
  • The `if` statement in `restored` should have a space before the `]]`. The syntax for a function is *either* `function name {` OR `name() {` (preferred). To use both is just making more work. – cdarke Aug 11 '17 at 06:56

0 Answers0