?
Solved

Detecting dir change in script

Posted on 2003-02-25
7
Medium Priority
?
164 Views
Last Modified: 2010-04-21
How would I verify if I had a sucessful dir change in a script? For example if in a script I have:

cd $newDir

or cd docs

how would I verify that the cd happened?????/

0
Comment
Question by:894359
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:bira
ID: 8021083
hi
   Use pwd in your script to show current directory, as below:

    newDir=/tmp
    cd $newDir
    pwd


0
 
LVL 6

Expert Comment

by:bira
ID: 8021097
OOOPPPsss

   You should also call the script using a dot before the script name:

     . yourscript
0
 

Author Comment

by:894359
ID: 8021160
Not following you! I thoght about saving path before change and then comparing!

currPathname=`pwd`
    cd $branch
     
     if [ `pwd` != "$currPathname$branch" ] ; then
      echo "Could not cd to the correct branch directory: $branch" >&2
      exit 2
    fi
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:bira
ID: 8021401
So what about this  ?


   newDir=/tmp
   cd $newDir
   if [ $? != 0 ] ; then
       echo "cd failed"
     exit
   fi
      echo "cd OK" to `pwd`

 *** dont forget to place the dot when firing the script
     in order to use the parent shell:

             . yourscript
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8021532
Short way is to do

cd /some/dir || exit $?

0
 

Author Comment

by:894359
ID: 8022553
Hi Tintin will this work ?

Short way is to do

cd /some/dir || exit 2 $? echo "something here!"

Also what does the $? do?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 400 total points
ID: 8022623

The $? variable contains the return code of the last command.

If the cd fails for whatever reason, the return code will be > 0.

If you want to do more than just an exit with the status, then use something like bira's solution (although, it should really use -ne instead of != for the comparision)

If you have lots of similar checks you can do something like:

#!/bin/sh

Error
{
  echo "$*" >&2
  exit 1
}

cd /some/dir || Error "cd failed"
[ -f /some/file ] || Error "/some/file does not exist"
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question