Reputation: 767
I have a shell (ksh
) script. I want to determine whether a certain directory is present in /tmp
, and if it is present then I have to delete it. My script is:
test
#!/usr/bin/ksh
# what should I write here?
if [[ -f /tmp/dir.lock ]]; then
echo "Removing Lock"
rm -rf /tmp/dir.lock
fi
How can I proceed? I'm not getting the wanted result: the directory is not removed when I execute the script and I'm not getting Removing Lock
output on my screen.
I checked manually and the lock file is present in the location.
The lock file is created with set MUTEX_LOCK "/tmp/dir.lock"
by a TCL program.
Upvotes: 6
Views: 37434
Reputation: 17054
For directory check, you should use -d
:
if [[ -d /tmp/dir.lock ]]; then
echo "Removing Lock"
rm -rf /tmp/dir.lock
fi
Upvotes: 4
Reputation: 72756
In addition to -f
versus -d
note that [[ ]]
is not POSIX, while [ ]
is. Any string or path you use more than once should be in a variable to avoid typing errors, especially when you use rm -rf
which deletes with extreme prejudice. The most portable solution would be
DIR=/tmp/dir.lock
if [ -d "$DIR" ]; then
printf '%s\n' "Removing Lock ($DIR)"
rm -rf "$DIR"
fi
Upvotes: 22