qfwfq
June 20, 2007, 10:26am
1
Ok, pretty hard to explain but I'll do my best.
Let's say MYVAR content is /home/me then
MYVAR=/home/me
I want to cd to that dir using the MYVAR variable.
Can't do cd $MYVAR even if I echo MYVAR and gives me /home/me.
MYVAR=/home/me
echo $MYVAR
cd $MYVAR
Does not change dir to that dir.
If you running your script as:
your_script.sh
Changing a directory in a script, it is only valid for the shell script context.
If you want the script to affect the directory of the parent:
. your_script.sh
qfwfq
June 20, 2007, 10:40am
3
That's correct... but I do that cd because I need to start an application from that dir and then exit the script. So that is the only reason why I need to cd to that dir. The cd does not work... in fact it does not, it does cd to $MYVAR instead of cd /home/me.
After you issue the "cd", try the following:
pwd
echo $PATH
qfwfq
June 20, 2007, 10:54am
5
Ok, I think I am not clear...sorry about that. Rewind...
Into /etc/profile file there are some variables:
USERHOME=/test
ADMINHOME=/test2
Then if the user me has USERHOME string into it's .profile file, then MYVAR becomes /test
MYVAR=$(sed -n '/HOME}/s/.*{\(.*HOME\)}.*/\1/p' /home/me/.profile)
echo $MYVAR
Then the result of the echo command will be /test
I need to cd to that /test using the variable :
MYVAR=$(sed -n '/HOME}/s/.*{\(.*HOME\)}.*/\1/p' /home/me/.profile)
echo $MYVAR
cd $MYVAR
So cd to /test
Still:
Inside of the shell script, after you issue the "cd":
pwd
echo $PATH
qfwfq
June 20, 2007, 11:10am
7
I get :
USERHOME
./test[4]: USERHOME: not found
/usr/local/utl/scripts
/usr/sbin:/usr/bin:/usr/ccs/bin:/usr/contrib/bin:/usr/contrib/Q4/bin:/opt/ipf/bin:/opt/hparray/bin:/opt/nettladm/bin:/opt/fcms/bin:/opt/sec_mgmt/b
astille/bin:/opt/resmon/bin:/opt/gnome/bin:/usr/bin/X11:/opt/ignite/bin:/usr/con
trib/kwdb/bin:/opt/mozilla:/opt/wbem/bin:/opt/wbem/sbin:/opt/graphics/common/bin
:/opt/mx/bin:/opt/sec_mgmt/spc/bin:/opt/upgrade/bin:/opt/hpnpl//bin:/usr/local/b
in:/opt/sanmgr/commandview/client/sbin:/opt/sanmgr/cssi/Licensing/sbin:/usr/loca
l/bin:/uv/bin:/opt/cfg2html:/usr/contrib/bin/X11:/sbin:/usr/local/utl:/usr/local
/bin
qfwfq,
As you can see, you are getting the value "USERHOME" in the "$MYVAR",
not "/test".