0

My project environment has both the csh and bash scripts

There is a csh script (say c.csh) sourced from other csh and bash scripts and c.csh currently has just few setenvs and I got the setenv working from both csh/bash scripts using the solution from the following post.

How to write a setevn/export script that will work in both csh and bash?

c.csh

test "$?BASH_VERSION" = "0" || eval 'setenv() { export "$1=$2"; }' 

    setenv MY_PROJ_BASE /home/test1
    ....
    ...
    setenv MY_ENV_FILE /test2/log

MY_ENV_FILE has the environment variables and now I need to unset each variable similar to following.

c.csh

setenv MY_PROJ_BASE /home/test1
..........
........
setenv MY_ENV_FILE /test2/log
.........
........
foreach ev ("`cat $(MY_PROJ_BASE)/$(MY_ENV_FILE)`")
   unsetenv $ev
end

The above code would throw error when sourced from bash scripts. The forloop code is only required when sourced from csh scripts and not needed (DON'T CARE) when sourced from bash scripts.

I tried to use eval similar to setenv but couldn't get it working.

 test "$?BASH_VERSION" = "0" || eval 'foreach() do_something_to_work'
2

1 Answer 1

0

I got the solution, awk command does the trick

test "$?BASH_VERSION" = "0" && eval `awk 'BEGIN { str="unsetenv "} {print str$1}' $MY_ENV_FILE

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .