rm: cannot remove '–rf': No such file or d

2019-09-16 04:57发布

问题:

I have a Bash script that automates creating some SVN folders. In the course of doing so, it creates a temporary directory. When I try to delete that temp directory with the rm -rf command, I get the following error...

rm: cannot remove '–rf': No such file or directory

It seems to think that "-rf" is a file name. The command works fine on the command line.

Here is my script...

#!/bin/bash

if [ $# -lt 1 ]; then
  echo "Usage: $0 reponame1 reponame2 ..."

else
  for var in "$@"
  do
      REPONAME=$var

      mkdir -p ~/temp-$REPONAME/branches
      mkdir ~/temp-$REPONAME/tags
      mkdir ~/temp-$REPONAME/trunk

      svnadmin create $REPONAME
      svn import ~/temp-$REPONAME svn+ssh://username@192.168.123.234/home/username/svnrepos/$REPONAME -m "Initial structure"

      rm –rf ~/temp-$REPONAME/
  done
fi

And here is the output

$ ./mkrepo.sh mysvnrepo
username@192.168.123.234's password:
username@192.168.123.234's password:
Adding         /home/username/temp-mysvnrepo/branches
Adding         /home/username/temp-mysvnrepo/tags
Adding         /home/username/temp-mysvnrepo/trunk
Committing transaction...
Committed revision 1.
rm: cannot remove '–rf': No such file or directory
rm: cannot remove '/home/username/temp-mysvnrepo/': Is a directory

回答1:

You managed to type a unicode "EN DASH"(U+2013) which is not recognised by rm as a normal hyphen "-"(U+002D) so rm thinks it is the beginning of a file name and not of your parameters. They do look alike, but they are not the same for a program. To fix it, just erase it and type it again making sure you take the normal hyphen/minus key.



回答2:

The '-' in your script in rm –rf is not the one it expects. The correct one is rm -rf.

I hope you can spot the difference.

rm –rf rm -rf



标签: bash rm