- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- How to verify correct directory before running com...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:13 AM
04-30-2008 06:13 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:20 AM
04-30-2008 06:20 AM
Solution# 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...
- Tags:
- CD
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:22 AM
04-30-2008 06:22 AM
Re: How to verify correct directory before running command
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
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:30 AM
04-30-2008 06:30 AM
Re: How to verify correct directory before running command
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:32 AM
04-30-2008 06:32 AM
Re: How to verify correct directory before running command
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:36 AM
04-30-2008 06:36 AM
Re: How to verify correct directory before running command
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:46 AM
04-30-2008 06:46 AM
Re: How to verify correct directory before running command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 06:51 AM
04-30-2008 06:51 AM
Re: How to verify correct directory before running command
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 07:18 AM
04-30-2008 07:18 AM
Re: How to verify correct directory before running command
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 09:02 PM
04-30-2008 09:02 PM
Re: How to verify correct directory before running command
Right. If you want a script fragment to actually cd, you need to source it:
. check_directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2008 10:31 PM
05-05-2008 10:31 PM
Re: How to verify correct directory before running 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.