Operating System - HP-UX
1820635 Members
1825 Online
109626 Solutions
New Discussion юеВ

Setting variables in a sh (Posix) shell by calling a csh script ???

 
SOLVED
Go to solution
Jaris Detroye
Frequent Advisor

Setting variables in a sh (Posix) shell by calling a csh script ???

I am writing a script in sh (Posix) which needs environment variables which are setup in a csh script. (Calling a csh users .login file)

Can this be done? Everytime I call the csh scripts upon exiting the variables are lost to the calling script. I have tried declaring the .login script to use /bin/csh and calling it with a leading '.' from my sh script, but this leads Posix to attempt to translate the .login script which it can not since it is written in csh.

Can any one assist?
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: Setting variables in a sh (Posix) shell by calling a csh script ???

This violates one of the fundamental tenets of UNIX; a child process can inherit the enviroment of the parent the the child can never directly alter the parent's env.

You must do something like have the child write to a file and have the parent read the file and set the variables. If the other script were POSIX shell then you could use the "." command to include the other script. It actually becomes a part of the foreground process.

e.g.

echo "Stuff"
. ./myenv.sh
echo "More Stuff"

In this case, any variables set in myenv.sh would be in effect because this did not spawn a child process.
If it ain't broke, I can fix that.
Shannon Petry
Honored Contributor

Re: Setting variables in a sh (Posix) shell by calling a csh script ???

Not by any means that I can think of off the top of my head.

WHY?

Cshell will read POSIX variable, and translate them to a C-Shell format. POSIX does not take external variables into consideration when it launches.

POSIX shell will read the .profile and system profile, but could care less what another shell has set before it launches. There is another issue, which is more obvious.. The variables are not set nor maintained the same (hence why C-SHell converts them).

You need to do 1 of 3 things..

Convert your script to cshell, use an external parser (I.E. Perl) to convert them, or convert your variables to posix shell.

Regards,
Shannon
Microsoft. When do you want a virus today?
Tom Danzig
Honored Contributor
Solution

Re: Setting variables in a sh (Posix) shell by calling a csh script ???

You could put some code in your sh script to transcribe the .login contents. Something like:

cat .login | awk '/^set/' | grep -v path | while read a b
do
export $b
done

I grep'ed out the path vairable as this one won't work due to syntax. If you don't need the path variable, this should work.

Jordan Bean
Honored Contributor

Re: Setting variables in a sh (Posix) shell by calling a csh script ???

You could also have the posix shell exec the csh to source and exec the script again. Try this:

#!/usr/bin/sh -x
if [ -z "${DO_CSH_SOURCE}" ]
then
export DO_CSH_SOURCE=1
exec csh -xc "source ~/.login && exec $0"
fi
# continue normally

Remember to use setenv in .login if you want the variables available to POSIX. It's kinda dirty, but it gets the job done.

Shannon Petry
Honored Contributor

Re: Setting variables in a sh (Posix) shell by calling a csh script ???

The problem I see with the examples given is that they can not handle real scripting.

Sorry, but I dont see
set HOST=`/bin/hostname`

as a script. I do see

set USERNAME=`whoami`
set OS=`uname -s`
if ( -f "/tmp/$USERNAME.lock" ) then
set SESSION="CURRENT"
else
set SESSION="FREE"
setenv PATH ( /apps/$OS/bin $path )
set LOCKFILE="/tmp/$USERNAME.lock"
touch $LOCKFILE
run_my_app
rm $LOCKFILE
endif

as a script.

So what happens if you cat a file with this content and try to just grep/awk out the set strings, and parse into POSIX?

It assigns a bunch of crap, so is broken. The dependancy on the "if" statement negates what you gave as an answer to the problems.

Now, the next step in your logic would be to convert my c-shell "if" statements to Posix. Guess what? Just like in POSIX, you can write "if" statements in multiple ways. So there are always going to be failures in this method as well.


So the answers given will work in the simple case of not a script, but setting simple variables. Add any form of intelligence to the script and it's not possible without alot of work.

Depending on how smart your .login, .cshrc, or other c-shell script file is, dictates how difficult this task really is.

Regards,
Shannon
Microsoft. When do you want a virus today?