This is my remove script. Not sure why my project is unresponsive, It doesn't give an error, it just doesn't do anything when I type "sh remove filename" into the command line. I have to hit CTRL C to exit. Ignore the line numbers. I posted my code and what I have to do below:

  • Create script named remove

  • Create a recycle bin in $HOME/deleted in your script

  • For any file to be deleted, it will be a command line argument and the script should executed as follows: sh remove fileName.

  • The script must test for the same error conditions as the rm command and display the same error messages as the rm command

  • The filenames in the recycle bin should be in the following format: filename_inode

    2 sh remove "filename"
    3 function directory(){
    4 #Makes the deleted directory
    6 if [ ! -d ~/deleted ]
    7     then
    8        mkdir ~/deleted
    9 fi
    17 function movefile(){
    18 #moving files to recycle bin
    19 mv $l ~/deleted/$l
    20 echo "File moved to recycle bin "
    21 }
    24 function error_conditions(){
    25 #prints error messages
    26 if [ ! -f ~/project ]
    27     then
    28         echo "sh: remove: cannot remove '$filename': no such file or directory"
    29 elif [ ! -d ~/project ]
    31     then
    32        echo "sh remove: cannot remove '$filename': is a directory"
    33 else
    34   echo "sh remove: missing operand"
   35  fi
   37 }
   40 function delete_file(){
   41 #gets inode for filename
   42 inode=$(stat -c%i $filename)
   43 filename=$1
   44 pwd=$(readlink -e$filename)
   45 if $interactive
   46     then
            if $verbose = true ]
   47     read -p "Are you sure you want to delete $filename?" i_input
   48         if [ $i_input = "y" ] || [ $i_input = "Y" }
   49             then
   50             mv $filename ~/delete/${filename}_$inode
   51         fi
   52 fi
   53 }
   54 directory 
   55 error_conditions $*
   56 delete_file $*
   57 move_file $*
  • 2
    Is this the remove script? In that case, it is calling itself recursively on the first line (well, line 2). Remove sh remove "filename" from that line. – Kusalananda May 21 '19 at 15:50
  • This is the remove script. If I remove "sh remove filename", how would I use sh remove filename on the command to act as the same as the rm command? – jason ap May 21 '19 at 15:52
  • 2
    I don't understand. To run the script, you would make it executable and then use ./remove whateverfile. The script should not call itself recursively. If you want to have it execute whenever rm is used, you would probably wrap it in an alias or shell function that you declare in your shell's startup files. But that's a later issue, you have a number of other issues in the code that you will have to deal with first. – Kusalananda May 21 '19 at 15:53
  • 2
    No, you want a script that acts like mv. A script that acts like rm would actually remove files. You want your script to move files. In any case, the script should not need to call itself. – Kusalananda May 21 '19 at 15:58
  • 3
    Other issues: ~deleted will try to access the home directory of a user called deleted. The error_conditions does not look at $filename. $filename is not double quoted, so the script won't handle filenames with spaces, for example. You pass $* (unquoted, should be "$@") to shell functions that does not use their arguments. You have function called movefile, but call move_file. You seem to call both delete_file and move_file and they both move the file (although movefile uses the unset variable $l). There's a [ missing in a test. $interactive seems to be unset. – Kusalananda May 21 '19 at 16:02

Assuming that the code in your question is in a file called remove, line 2 is causing your infinite loop in the remove script. Basically, BASH scripts run each line as a command from top to bottom. When you try to run the remove script, it will reach line 2 (sh remove "filename") and try to run another instance of remove. That new instance tries to run a third instance of remove and so forth.

Long story short, remove or comment out line 2:

# sh remove "filename"

That said, be careful when writing a file to delete files. You don't want to make a mistake and delete something that shouldn't be deleted. I didn't go through the rest of the script. I would comment out lines that actually move files on the first few test runs at the very least.

| improve this answer | |
  • Ideally, a script is tested after each single statement added to it. That way, you know why it breaks when it breaks (it's because of the last statement you added to it). – Kusalananda May 21 '19 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.