Operating System - Linux
1827876 Members
1605 Online
109969 Solutions
New Discussion

How to verify correct directory before running command

 
SOLVED
Go to solution
Mike Keys
Regular Advisor

How to verify correct directory before running command

I have a script that I am trying to make bulletproof. I have a very dangerous find/remove command that I execute from a particualr directory. This runs as root. If the directory that the script runs from does not exist, then the command runs in the current directory, which can cause damage.

I cd into a directory and I want to add something to the script that verifies that I am in the correct directory before running the find/remove command.

Any suggestions?
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to verify correct directory before running command

Hi Mike:

# cd /path || { echo "STOP!"; exit 1; }

...will exit your script if '/path' isn't valid.

Another safeguard in scripts is to use 'set -u' so that unset variables are treated as errors:

# cat ./thisfails
#!/usr/bin/sh
set -u
cd ${DIR}
exit 0

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: How to verify correct directory before running command

Why cd into the directory first? Why not let find do all the work for you.

Instead of this:

cd /dir
find . -type f -exec rm

Do this:

find /dir -type f -exec rm

Now if /dir does not exist find will error.

Here is an example (where the /tmp/pww1 directory does not exist):

# find /tmp/pww1 -type f -exec rm {} \+
find: cannot stat /tmp/pww1


Mike Keys
Regular Advisor

Re: How to verify correct directory before running command

James,

I entered that code and have the following for my script (I am running from the /home/ops directory):

#! /sbin/sh
cd /home/ops/mike || { echo "Can't find directory"; exit 1; }
echo "You are in the $PWD directory"

After testing to make sure that if I could cd to the directory, that the command would fail, I added the echo command to make sure that I was in the correct directory after the script ran. However, after the script ran and I issued a 'll' command I was still in the /home/ops directory. The script didn't go into the directory.

Do I need to issue another cd command if the check passes?
Mike Keys
Regular Advisor

Re: How to verify correct directory before running command

Patrick,

My original script looked like this (the directory is not important for this example):

#! /sbin/sh
cd /home/ops/mike
#find $PWD * -mtime +30 -exec rm {} \;

so, instead of using the variable $PWD, I should jus hardcode the directory in the find command?
James R. Ferguson
Acclaimed Contributor

Re: How to verify correct directory before running command

Hi Mike:

If you pass the 'cd' syntax I gave, you are where you should be. You can play with this:

#!/usr/bin/sh
set -u
DIR=/home
cd ${DIR} 2> /dev/null && echo "I am in $(pwd)" || { echo "Can't 'cd' to ${DIR}"; exit 1; }
echo "...continuing"

Regards!

...JRF...
Mike Keys
Regular Advisor

Re: How to verify correct directory before running command

O.K. I see, the cd in the script is only valid while in the script. Once the script is done, the pwd is returned to what it was originally. For example, if I started in the /home/ops directory and executed the script that runs a command in the /home/ops/mike dir, then while the script is running it should be in the /home/ops/mike dir. Once the script is done and I do a 'pwd' I would see /home/ops.
James R. Ferguson
Acclaimed Contributor

Re: How to verify correct directory before running command

Hi Mike:

> I see, the cd in the script is only valid while in the script. Once the script is done, the pwd is returned to what it was originally.

Yes, that is true. A child process (your script) cannot alter the environment of its parent (the shell). After all, children should obey their parents :-)

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to verify correct directory before running command

Hi (again) Mike:

One more thing you should understand is the difference between parentheses and curly braces in a shell script:

( list ) executes in a separate environment.

{ list ; } executes in the *same* environment.

For example, consider:

# ./notewell
#!/usr/bin/sh
set -u
DIR1=/home
DIR2=/var/tmp
{ cd ${DIR1} && ls -ld . || exit 1; }
echo "I am now in $PWD"
( cd ${DIR2} && ls -ld . || exit 2 )
echo "I am STILL in $PWD"

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to verify correct directory before running command

>Mike: the cd in the script is only valid while in the script. Once the script is done, the pwd is returned to what it was originally.

Right. If you want a script fragment to actually cd, you need to source it:
. check_directory
Rasheed Tamton
Honored Contributor

Re: How to verify correct directory before running command

>so, instead of using the variable $PWD, I should jus hardcode the directory in the find command?

Yes. It is better - if you have few dirs in the script. In that case, you can use as Patrick said.


find $PWD -type f -mtime +30 -exec ls {} \+

find /home/mike -type f -mtime +30 -exec ls {} \+

replace ls with rm -i while you test the script.

rgds.