<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to verify correct directory before running command in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189598#M91345</link>
    <description>James,&lt;BR /&gt;&lt;BR /&gt;I entered that code and have the following for my script (I am running from the /home/ops directory):&lt;BR /&gt;&lt;BR /&gt;#! /sbin/sh&lt;BR /&gt;cd /home/ops/mike || { echo "Can't find directory"; exit 1; }&lt;BR /&gt;echo "You are in the $PWD directory"&lt;BR /&gt;&lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;Do I need to issue another cd command if the check passes?</description>
    <pubDate>Wed, 30 Apr 2008 13:30:25 GMT</pubDate>
    <dc:creator>Mike Keys</dc:creator>
    <dc:date>2008-04-30T13:30:25Z</dc:date>
    <item>
      <title>How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189595#M91342</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;Any suggestions?</description>
      <pubDate>Wed, 30 Apr 2008 13:13:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189595#M91342</guid>
      <dc:creator>Mike Keys</dc:creator>
      <dc:date>2008-04-30T13:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189596#M91343</link>
      <description>Hi Mike:&lt;BR /&gt;&lt;BR /&gt;# cd /path || { echo "STOP!"; exit 1; }&lt;BR /&gt;&lt;BR /&gt;...will exit your script if '/path' isn't valid.&lt;BR /&gt;&lt;BR /&gt;Another safeguard in scripts is to use 'set -u' so that unset variables are treated as errors:&lt;BR /&gt;&lt;BR /&gt;# cat ./thisfails&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;set -u&lt;BR /&gt;cd ${DIR}&lt;BR /&gt;exit 0&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 30 Apr 2008 13:20:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189596#M91343</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-30T13:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189597#M91344</link>
      <description>Why cd into the directory first?  Why not let find do all the work for you.&lt;BR /&gt;&lt;BR /&gt;Instead of this:&lt;BR /&gt;&lt;BR /&gt;cd /dir&lt;BR /&gt;find . -type f -exec rm&lt;BR /&gt;&lt;BR /&gt;Do this:&lt;BR /&gt;&lt;BR /&gt;find /dir -type f -exec rm&lt;BR /&gt;&lt;BR /&gt;Now if /dir does not exist find will error.&lt;BR /&gt;&lt;BR /&gt;Here is an example (where the /tmp/pww1 directory does not exist):&lt;BR /&gt;&lt;BR /&gt; # find /tmp/pww1 -type f -exec rm {} \+&lt;BR /&gt;find: cannot stat /tmp/pww1&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 30 Apr 2008 13:22:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189597#M91344</guid>
      <dc:creator>Patrick Wallek</dc:creator>
      <dc:date>2008-04-30T13:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189598#M91345</link>
      <description>James,&lt;BR /&gt;&lt;BR /&gt;I entered that code and have the following for my script (I am running from the /home/ops directory):&lt;BR /&gt;&lt;BR /&gt;#! /sbin/sh&lt;BR /&gt;cd /home/ops/mike || { echo "Can't find directory"; exit 1; }&lt;BR /&gt;echo "You are in the $PWD directory"&lt;BR /&gt;&lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;Do I need to issue another cd command if the check passes?</description>
      <pubDate>Wed, 30 Apr 2008 13:30:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189598#M91345</guid>
      <dc:creator>Mike Keys</dc:creator>
      <dc:date>2008-04-30T13:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189599#M91346</link>
      <description>Patrick,&lt;BR /&gt;&lt;BR /&gt;My original script looked like this (the directory is not important for this example):&lt;BR /&gt;&lt;BR /&gt;#! /sbin/sh&lt;BR /&gt;cd /home/ops/mike&lt;BR /&gt;#find $PWD * -mtime +30 -exec rm {} \;&lt;BR /&gt;&lt;BR /&gt;so, instead of using the variable $PWD, I should jus hardcode the directory in the find command?</description>
      <pubDate>Wed, 30 Apr 2008 13:32:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189599#M91346</guid>
      <dc:creator>Mike Keys</dc:creator>
      <dc:date>2008-04-30T13:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189600#M91347</link>
      <description>Hi Mike:&lt;BR /&gt;&lt;BR /&gt;If you pass the 'cd' syntax I gave, you are where you should be.  You can play with this:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;set -u&lt;BR /&gt;DIR=/home&lt;BR /&gt;cd ${DIR} 2&amp;gt; /dev/null &amp;amp;&amp;amp; echo "I am in $(pwd)" || { echo "Can't 'cd' to ${DIR}"; exit 1; }&lt;BR /&gt;echo "...continuing"&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 30 Apr 2008 13:36:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189600#M91347</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-30T13:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189601#M91348</link>
      <description>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.</description>
      <pubDate>Wed, 30 Apr 2008 13:46:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189601#M91348</guid>
      <dc:creator>Mike Keys</dc:creator>
      <dc:date>2008-04-30T13:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189602#M91349</link>
      <description>Hi Mike:&lt;BR /&gt;&lt;BR /&gt;&amp;gt; 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.&lt;BR /&gt;&lt;BR /&gt;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 :-)&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 30 Apr 2008 13:51:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189602#M91349</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-30T13:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189603#M91350</link>
      <description>Hi (again) Mike:&lt;BR /&gt;&lt;BR /&gt;One more thing you should understand is the difference between parentheses and curly braces in a shell script:&lt;BR /&gt;&lt;BR /&gt;( list ) executes in a separate environment.&lt;BR /&gt;&lt;BR /&gt;{ list ; } executes in the *same* environment.&lt;BR /&gt;&lt;BR /&gt;For example, consider:&lt;BR /&gt;&lt;BR /&gt;# ./notewell&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;set -u&lt;BR /&gt;DIR1=/home&lt;BR /&gt;DIR2=/var/tmp&lt;BR /&gt;{ cd ${DIR1} &amp;amp;&amp;amp; ls -ld . || exit 1; }&lt;BR /&gt;echo "I am now in $PWD"&lt;BR /&gt;( cd ${DIR2} &amp;amp;&amp;amp; ls -ld . || exit 2  )&lt;BR /&gt;echo "I am STILL in $PWD"&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 30 Apr 2008 14:18:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189603#M91350</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-04-30T14:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189604#M91351</link>
      <description>&amp;gt;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.&lt;BR /&gt;&lt;BR /&gt;Right.  If you want a script fragment to actually cd, you need to source it:&lt;BR /&gt;. check_directory</description>
      <pubDate>Thu, 01 May 2008 04:02:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189604#M91351</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-05-01T04:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to verify correct directory before running command</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189605#M91352</link>
      <description>&amp;gt;so, instead of using the variable $PWD, I should jus hardcode the directory in the find command?&lt;BR /&gt;&lt;BR /&gt;Yes. It is better - if you have few dirs in the script. In that case, you can use as Patrick said.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;find $PWD -type f -mtime +30 -exec ls {} \+&lt;BR /&gt;&lt;BR /&gt;find /home/mike -type f -mtime +30 -exec ls {} \+&lt;BR /&gt;&lt;BR /&gt;replace ls with rm -i while you test the script.&lt;BR /&gt;&lt;BR /&gt;rgds.</description>
      <pubDate>Tue, 06 May 2008 05:31:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-verify-correct-directory-before-running-command/m-p/4189605#M91352</guid>
      <dc:creator>Rasheed Tamton</dc:creator>
      <dc:date>2008-05-06T05:31:10Z</dc:date>
    </item>
  </channel>
</rss>

