csh脚本作为可执行文件不设置 [英] csh script as executable does not setenv
问题描述
我无法通过可执行文件 csh / tcsh
脚本
env
变量$ b
一个 env
在中设置变量csh / tcsh
可执行脚本myscript
脚本的内容...
setenv MYVAR / abc / xyz
它不能在shell上设置并报告Undefined variable
我已将 csh / tcsh
脚本通过以下shell命令
chmod + x / home / xx / bin / myscript
也将路径更新为
set path =(/ home / xx / bin $ path)
which myscript
/ home / xx / bin / myscript
当我在命令行上运行脚本并回应env变量。
myscript
echo $ MYVAR
MYVAR未定义的变量
但是如果我以逗号来源d行
source / home / xx / bin / myscript
echo $ MYVAR
/ abc / xyz
这不是环境变量的工作原理。 >
为进程(在这种情况下为 tcsh
)设置环境变量,该进程传递给所有子进程。所以当你这样做:
$ setenv LS_COLORS = foo
$ ls
您首先为 tcsh $ c $设置
LS_COLORS
c>进程, tcsh
然后启动子进程 ls
,其中包含 tcsh
的环境(包括 LS_COLORS
),然后可以使用。
然而,重新设置环境是一个子进程,然后想要传播回父进程(不知何故)。这不可能。这与 tcsh
无关,它对系统上的任何进程都是这样。
它适用于 source
因为 source
读取一个文件,并在当前进程中逐行执行。所以它不会启动一个新的 tcsh
进程。
我会把它作为一个练习给你什么这意味着如果它可以可能:-)你真的想处理设置一些随机环境变量的不明智的shell脚本吗?而由 php
进程设置的环境变量怎么样,我们希望这些变量返回到父 httpd
进程中? : - )
您没有真正描述您要实现的目标 ,但一般来说,您想要执行以下操作:
#!/ bin / csh -f
#...做东西...
echo请将此行复制到您的环境中:
echosetenv MYVAR $ myvar
I am not able to set env
variables through an executable csh/tcsh
script
An env
variable set inside a csh/tcsh
executable script "myscript"
contents of the script ...
setenv MYVAR /abc/xyz
which is not able to set on the shell and reports "Undefined variable"
I have made the csh/tcsh
script as executable by the following shell command
chmod +x /home/xx/bin/myscript
also the path is updated to
set path = (/home/xx/bin $path)
which myscript
/home/xx/bin/myscript
When I run the script on command line and echo the env variable ..
myscript
echo $MYVAR
MYVAR "Undefined variable"
but if i source on command line
source /home/xx/bin/myscript
echo $MYVAR
/abc/xyz
This is not how environment variables work.
An environment variable is set for a process (in this case, tcsh
) which is passed on to all child processes. So when you do:
$ setenv LS_COLORS=foo
$ ls
You first set LS_COLORS
for the tcsh
process, tcsh
then starts the child process ls
which inheres tcsh
's environment (including LS_COLORS
), which it can then use.
However, what you're doing is setting the environment is a child process, and then want to propagate this back to the parent process (somehow). This is not possible. This has nothing to do with tcsh
, it works like this for any process on the system.
It works with source
because source
reads a file, and executes it line-by-line in the current process. So it doesn't start a new tcsh
process.
I will leave it as an exercise to you what the implications would mean if it would be possible :-) Do you really want to deal with unwise shell scripts that set some random environment variables? And what about environment variables set by a php
process, do we want those to go back in the parent httpd
process? :-)
You didn't really describe what goal you're trying to achieve, but in general, you want to do something like:
#!/bin/csh -f
# ... Do stuff ...
echo "Please copy this line to your environment:"
echo "setenv MYVAR $myvar"
这篇关于csh脚本作为可执行文件不设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!