Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with if statement 1

Status
Not open for further replies.

dsawyer

IS-IT--Management
Jan 15, 2003
14
0
0
US
I cant seem to get the "cd" command to execute within an if statement. This is what I have:
#!/bin/sh
PRJ=$1
if [ $PRJ = wm ]; then
cd /projects/wm/work/$USER
else
echo "project directory not found"
fi

If i replace the "cd" line with an echo command the script works:
#!/bin/sh
PRJ=$1
if [ $PRJ = wm ]; then
echo "this works"
else
echo "project directory not found"
fi


 
I suspect the problem is with $USER. Did you declare the value of USER earlier in the script? USER isn't automatically set on some versions of Unix.
 
Are you testing it with a $USER that does not have a directory there?

take out the $USER and see if it goes to /projects/wm/work/

Blue
 
Im using solaris and $USER is already defined, I did try it again without the $USER variable and it still didnt work. When I run the script the shell takes it without any errors however the "cd" command doesnt take.
 
i rewrote the statement using csh and i get the same exact results.
 
Dude --

I've tried to recreate your problem, but to no avail.
My script works.

Does the following work for you using a known directory
and adding the last directory as an argument??


#!/bin/sh
VAR=$1
if [1 -eq 1 ]
then
echo "works"
echo `pwd`
cd /some/dir/$VAR
echo `pwd`
else
echo "blah blah"
fi

I can't figure out why it doesn't work for you. Could one of the set options be set that's causing this?
 
what do you get if you add the line, echo "$USER"
 
Do an ls -ld on /project/wm/work and on /project/wm/work/$USER. Does the user who is running the script have permissions to that directory?
 
Along the lines of my last posting, change /project/wm/work/$USER to /tmp and see if the cd works.
 
What do you mean "the cd command doesn't take"? Are you trying to get the script to change your working directory?

If you are trying to get this script to change the current working directory of your interactive session, you need to "source" the script. That is, type...
Code:
   .  scriptname
...to run it (that's "dot, space, scriptname"). This will execute the commands within your current process.

You need to keep in mind that a shell script by default runs in a subprocess separate from the calling process. The cd IS changing the working directory of the subprocess it's running in. But, when the script ends, control returns to your interactive session which is still at the directory you were at when you ran the shell script.

Or, has the variable USER not been exported? You might try the following...
Code:
   #!/bin/sh
   PRJ=$1
   DIR=/projects/wm/work/$USER

   if [ $PRJ =  wm ]; then
      if [ ! -d ${DIR} ]; then
         echo "ERROR: Directory ${DIR} doesn't exist!"
      else
         cd ${DIR}
         pwd
      fi
   else
      echo 'project not "wm"'
   fi
This adds a test for the directory existing before doing the "cd".

Hope this helps.

 
You got it. What I needed to do was source the script.
csh%source <scriptname>

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top