Simple Unix Shell Script

Simple Unix Shell Script

Post by Arun Goe » Sat, 28 Jun 2003 07:52:21



Hi,

I am trying to execute a simple shell script to set the environment
variables.
It runs fine without any errors but i doesn't sets the environment
variables. I don't know what is wrong with the shell script.

I am using korn shell.
One more thing, is there anyway that same shell scripts can be run in
Bourne,C , Korn shell without any modification. I mean to say that we don't
have have to change the way they set different environment variables.

Any help will be greatly appreciated.

Thanks
Arun
-----------------------------------------------------
#! /bin/ksh -f
set -vx
cur_shell=`basename $SHELL`
LOCAL_DIR=/home/ag

echo "Your shell is : $cur_shell"

if [ $cur_shell = "ksh" ]
then
    #either csh or tcsh
    type=type1
    echo "$type"
fi

#Setting Env variables
if [ $type = "type1" ]
then
 LEDA=${LOCAL_DIR}
 export LEDA
fi

 
 
 

Simple Unix Shell Script

Post by Fletcher Glen » Sat, 28 Jun 2003 08:12:53


There's nothing wrong with your script, it's how you run it.
Your script gets run in a sub-shell, and when the sub-shell
exits, everything in the sub-shell's environment is gone.
You must run your script as a source document, meaning
you must execute it in the current shell.  In ksh, or sh,
execute: ".  scriptname"  (notice the leading dot).  In
csh or tcsh, execute "source scriptname".

--
Fletcher Glenn
to reply remove NOSPAM from my reply address


Quote:> Hi,

> I am trying to execute a simple shell script to set the environment
> variables.
> It runs fine without any errors but i doesn't sets the environment
> variables. I don't know what is wrong with the shell script.

> I am using korn shell.
> One more thing, is there anyway that same shell scripts can be run in
> Bourne,C , Korn shell without any modification. I mean to say that we
don't
> have have to change the way they set different environment variables.

> Any help will be greatly appreciated.

> Thanks
> Arun
> -----------------------------------------------------
> #! /bin/ksh -f
> set -vx
> cur_shell=`basename $SHELL`
> LOCAL_DIR=/home/ag

> echo "Your shell is : $cur_shell"

> if [ $cur_shell = "ksh" ]
> then
>     #either csh or tcsh
>     type=type1
>     echo "$type"
> fi

> #Setting Env variables
> if [ $type = "type1" ]
> then
>  LEDA=${LOCAL_DIR}
>  export LEDA
> fi


 
 
 

Simple Unix Shell Script

Post by Chris F.A. Johnso » Sat, 28 Jun 2003 12:21:03


[This properly belongs in comp.unix.shell; Follow-up set]


> Hi,

> I am trying to execute a simple shell script to set the environment
> variables.
> It runs fine without any errors but i doesn't sets the environment
> variables. I don't know what is wrong with the shell script.

    A script (or any other program) cannot change the environment of
    its parent process. However, there is a method, called sourcing,
    which allows a script to be run as part of the calling process.
    The syntax is: ". /path/to/script", e.g.;

. $HOME/bin/my_script

    If it's in your PATH, you can just do:

. my_script

Quote:> I am using korn shell.
> One more thing, is there anyway that same shell scripts can be run in
> Bourne,C , Korn shell without any modification. I mean to say that we don't
> have have to change the way they set different environment variables.

    There are two basic types of shell, Bourne (sh, ksh, bash, et al)
    and C (csh, tcsh); with very few exceptions, scripts written for
    one type will not run in the other. Scripting in csh is not
    recommended. See these documents for reasons:

        <http://www.grymoire.com/Unix/CshTop10.txt>
        <http://www.grymoire.com/Unix/Csh.html#uh-0>
        <http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/>

    Scripts written for a Bourne shell will (with a few gotchas) run
    in any other Bourne-type shell.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License