Operating System - HP-UX
1829921 Members
2322 Online
109993 Solutions
New Discussion

Loading a new environment from a file

 
SOLVED
Go to solution
Manuales
Super Advisor

Loading a new environment from a file

Hi ...
the doubt in question is:

I hava a user own csh
I need work with ksh
I know that i have change kind of shell in /usr/passwd file in kind of shell field, but i do not have password root and we do not know it ..
for above told, i'm using /usr/bin/ksh in the beginning of the scripts
i'm testing follows:
i created a file which contanins a file which contantains only variables set.
but, when i run script variables are not set and if i execute the line into script over prompt can works without problems, i mean, variables are set ...
why?

Script which has variables set:
named: environment
contains:
a="/home/usera"
b="/home/userb"
c="/home/userc"

other script which run above file:
named: test
contains follows:
echo "step1"
. /home/environment
echo "step1"

well ..
i logon ...
i run ksh to enter ksh environment
i run step1
appears:
step1
step2

if i test:
echo $a
do not shown something !!!!

why!!!
if i run online:
. /home/environment
and then verify environment as follows:
echo $a
appears:
/home/usera

why!!!
doubt here is , if i run environment file from a file, why do not work????

help please ..
thanks, Manuales ..
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Loading a new environment from a file

If I can follow all of this then:

you need to add this at the end of your environment script:

export a b c

You also need to make absolutely certain that you do this:
. /home/environment
rather than
./home/environment

In the first case, the "." sources the file and it becomes part of the current process; in the second case, the file is run as a child process and a child can never alter the environment of a parent. A sourced file can also not contain a return or exit statement because the effect is to exit the foreground process.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Loading a new environment from a file

Make sure you are sourcing the environment before executing any commands in the same shell as the script i.e. your shell should contain the line below

. /home/environment

besides are you testing at the command prompt after the subshell has finished executing your script?
Manuales
Super Advisor

Re: Loading a new environment from a file

o.k......
Doing other test:

i'm login with csh user

i have a file named environment
which contains:
set a=/home/usera

if in command line i run:
set a=/home/usera
and if i run:
echo $a
appears: /home/usera

but ...
if i save this command line in a file named environment_into_file as follows:
cat enviroment_into_file
. /home/environment

and if i run environment_into_file
do not run!!! if i run echo $a after run environment_into_file do not work because do not show valu variable "a" !!!
why !!!

SOS PLEASE !!

A. Clay Stephenson
Acclaimed Contributor

Re: Loading a new environment from a file

Because csh syntax is very different from Korn/POSIX syntax.

For example

export XX=0 in POSIX
would be
setenv XX 0 in csh

The use of csh is generally discouraged these days.
If it ain't broke, I can fix that.
Manuales
Super Advisor

Re: Loading a new environment from a file

o.k.
i have updated "environment" file follows:
setenv a /home/usera

file test contents:
. /home/environment

if i run appears follows:
test[2]: setenv: not found.

why !!!
Manuales
Super Advisor

Re: Loading a new environment from a file

sorry .. ksh is the same than posix ?
Sandman!
Honored Contributor

Re: Loading a new environment from a file

Manuales...you need to run the environment_into_file in the current shell too, otherwise the environment file contained will set variables in the same shell as the env_into_file, or...

. environment_into_file

then do an echo $a

cheers!
Manuales
Super Advisor

Re: Loading a new environment from a file

It is very stranger .. do not work ...
Bill Hassell
Honored Contributor

Re: Loading a new environment from a file

Perhaps everything will be fixed if you simply change the user's shell. Just use the chsh command. It does not need root privileges:

chsh user_name /usr/bin/ksh

To answer your question, is the POSIX shell the same as ksh? The answer is yes other than some obscure features. The POSIX shell is /usr/bin/sh.

If you have scripts the start with:

#!/usr/bin/ksh

these scripts can be run from the csh shell. That first line tells the current shell what program to use to interpret this shell script. Note that whenever you run a script normally, everything takes place in a sub=program. Whatever happens in this sub-program has no effect on the current shell. That's why you must use the . followed by a space to have the shell interpret the script without a subprogram.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: Loading a new environment from a file

Hi Manuales:

A Posix shell conforms to to the specifications of the POSIX.2 Shell and Utility standards.

Posix shells build on the Korn shell originally developed at AT&T by David Korn. Sprinkled in the Posix shell are some of the features of the C-shell ('csh') as originally written by Bill Joy. Together, this ancestry evolves into a formal standard that any Posix-complaint shell must meet.

On HP-UX, the standard POSIX shell resides in '/sbin/sh' (as statically linked code) and in '/usr/bin/sh' (using dynamically linked libraries).

The 'ksh' shell on HP-UX found in '/usr/bin/ksh' is a Korn-88 version. It is not Posix compliant. A more modern, Korn-93 version with more Posix compliance lives in '/usr/dt/bin/dtksh'.

If you examine the end of the 'sh-poxix' man pages you can begin to get a sense of the standards that are established to describe conformance. If you really want to know more, begin here:

http://www.opengroup.org/onlinepubs/009695399/toc.htm

Regards!

...JRF...