Dinesh
Dinesh

Reputation: 16428

Deleting a directory contents using shell scripts

I am a newbie to Shell scripting. I want to delete all the contents of a directory which is in HOME directory of the user and deleting some files which are matching with my conditions. After googled for some time, i have created the following script.

#!/bin/bash
#!/sbin/fuser


PATH="$HOME/di"

echo "$PATH";

if [ -d $PATH ]
then
 rm -r $PATH/*
 fuser -kavf $PATH/.n*
 rm -rf $PATH/.store
 echo 'File deleted successfully :)'
fi

If I run the script, i am getting error as follows,

/users/dinesh/di
dinesh: line 11: rm: command not found
dinesh: line 12: fuser: command not found
dinesh: line 13: rm: command not found
File deleted successfully :)

Can anybody help me with this?

Thanks in advance.

Upvotes: 1

Views: 7671

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753705

This line:

PATH="$HOME/di"

removes all the standard directories from your PATH (so commands such as rm that are normally found in /bin or /usr/bin are 'missing'). You should write:

PATH="$HOME/di:$PATH"

This keeps what was already in $PATH, but puts $HOME/di ahead of that. It means that if you have a custom command in that directory, it will be invoked instead of the standard one in /usr/bin or wherever.

If your intention is to remove the directory $HOME/di, then you should not be using $PATH as your variable. You could use $path; variable names are case sensitive. Or you could use $dir or any of a myriad other names. You do need to be aware of the key environment variables and avoid clobbering or misusing them. Of the key environment variables, $PATH is one of the most key ($HOME is another; actually, after those two, most of the rest are relatively less important). Conventionally, upper case names are reserved for environment variables; use lower case names for local variables in a script.

Upvotes: 2

Gordon Davisson
Gordon Davisson

Reputation: 125788

PATH is a special variable that controls where the system looks for command executables (like rm, fuser, etc). When you set it to /users/dinesh/di, it then looks there for all subsequent commands, and (of course) can't find them. Solution: use a different variable name. Actually, I'd recommend using lowercase variables in shell scripts -- there are a number of uppercase reserved variable names, and if you try to use any of them you're going to have trouble. Sticking to lowercase is an easy way to avoid this.

BTW, in general it's best to enclose variables in double-quotes whenever you use them, to avoid trouble with some parsing the shell does after replacing them. For example, use [ -d "$path" ] instead of [ -d $path ]. $path/* is a bit more complicated, since the * won't work inside quotes. Solution: rm -r "$path"/*.

Random other notes: the #!/sbin/fuser line isn't doing anything. Only the first line of the script can act as a shebang. Also, don't bother putting ; at the end of lines in shell scripts.

#!/bin/bash

path="$HOME/di"

echo "$path"

if [ -d "$path" ]
then
 rm -r "$path"/*
 fuser -kavf "$path"/.n*
 rm -rf "$path/.store"
 echo 'File deleted successfully :)'
fi

Upvotes: 2

nhahtdh
nhahtdh

Reputation: 56809

You are modifying PATH variable, which is used by the OS defines the path to find the utilities (so that you can invoke it without having to type the full path to the binary). The system cannot find rm and fuser in the folders currently specified by PATH (since you overwritten it with the directory to be deleted), so it prints the error.

tl;dr DO NOT use PATH as your own variable name.

Upvotes: 3

Related Questions