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!

script does not work 3

Status
Not open for further replies.

daveleo

Programmer
Mar 2, 2002
54
0
0
US
apologies for this simple question....i have several books here that say this should work, and it doesnt....

i have HP-UX11.0 and my default shell is /sbin/sh

i tried to write a long script and run it and it didnt work, so i backed up and started with this simple script and it doesnt work either.

my test script, named [test], is

#!/sbin/sh
goto="cd /home/dleo/9hair"
export goto
echo $goto
echo "youre done"
# end of script

i set chmod for [test] to execute permission and ran it by typing ./test and it appeared to run correctly seen by the echoes to the screen

but when i type in $goto after the $ prompt and then check my pwd, it is not /home/dleo/9hair ....i am still in the directory i was in.

however.... when i type in goto="cd /home/dleo/9hair" at the command line and then type in $goto ....that correctly takes me to /home/dleo/9hair.

this is exactly what i experienced with the longer script file....it seemed to run but didnt really.

my books say this should work.

any help would be appreciated .... i have noone who knows unix.

daveleo
 
It looks like the problem is your script creates a new shell process which probably does do the cd OK, but then returns control back to the original shell, in the place where you kicked it off from. Essentially these are two distinct 'sessions'. If you want to do this, why not set an alias for goto rather than a script?
 
test and goto are reserved words, change them
why do you use /sbin/sh ?
to corectly export, you need to source the file
. ./myprog
 
When you run your script - it runs as a child process of the shell you started from. If you set and export a variable as you have done, it will be 'valid' for that shell (process). However, when the child process exits (ie. your script ends), the variable is not passed back to the parent process.

It does however work the other way, try.
export VAR1=hello
echo $VAR1

ksh (start a CHILD process - a new shell)
echo $VAR1 (should display hello)

So, environment variables are passed to a child process, but not from a child to parent.

If you ran your script using :

. ./test

it should work as the extra . tells the script to run, but in the current shell.

Hope this helps,

Martin
 
he he .. in the time it took me to type my answer, loads of replies appeared.

A good point was made, test and goto are reserved commands :

Good practice is to name a shell script :

somename.sh, eg. test.sh

also, for variables, use uppercase if you are unsure, this minimises the chances of clashes.

martin
 
AFAIK, test isn't a reserved word in the sense that it will cause problems if used from the command line, only if it is used incorrectly within a script? Agree with the general thrust, however!
 
thanks enormously.
your comments sent me reading up on various ways of executing scripts, what subshells mean, shell variables, etc
i made (almost) all the changes suggested then typed in
. ./davestest.sh (the space between dots is important!!)

and it ran fine and worked as planned.

i am especially glad that all your responses did not make me feel like an idiot.

daveleo
 
Glad to be of help -

unix has many commands, it's never ending learning and you can't know everything, so don't worry about asking questions.

Unless you've done it/used a command etc... you can't really be expected to know it - I'm sure all of us here know things that others don't.

Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top