Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I am using tcsh shell and have #!/bin/tcsh as my 1st line of shell script. When I directly type on the unix shell the following line works succesfully. setenv ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2

    Setting environment variables in tcsh

    Hi,
    I am using tcsh shell and have
    #!/bin/tcsh
    as my 1st line of shell script.

    When I directly type on the unix shell the following line works succesfully.
    setenv MODEL_ROOT=/proj/aps/storm/logic/gold/CURRENT/

    I know it because when I use echo, it reflects the change.

    However, when I try to automate it in a script, the following line always fails.
    less ./wrapper_runtest.o* | grep SCRIPTS_RUN_PATH | grep /$ | awk '{print "setenv "$1" "$2}' | tcsh

    I am reading a file called wrapper_runtest.o* and $2 has the desired path in the file. $1 and $2 are parsing as expected but the assignment using setenv fails.

    I tried several different things but the environment variable just wont get set to the desired path.
    Can you please help me?

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Environment variables are context-sensitive. Ie, when you set them in a shell or any other program, they go away when the shell/program exits. To propagate the variables set in a shell script, you need to source it either with the "source" or "." commands from the top-level shell. So, if your script is named "setvars.sh" and it looks like this:
    Code:
    setenv MODEL_ROOT=/proj/aps/storm/logic/gold/CURRENT/
    Then from your shell, do this:
    Code:
    source setvars.sh
    Now you will see the environment variable MODEL_ROOT in your local context because the "source" command executes in the current shell context.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2
    Thanks a lot Rubberman. That helps a lot. Never thought of it from that angle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...