設置環境變量的Shell腳本

[英]Shell script for setting environment variable


I am writing a shell script to set the environment variables whose values are available in a file. Below is the shell script I wrote,

我正在編寫一個shell腳本來設置環境變量,這些變量的值在文件中是可用的。下面是我寫的shell腳本,

VARIABLE_FILE=env-var.dat
if [ -f ${VARIABLE_FILE} ] ; then
   . ${VARIABLE_FILE}
   if [ ! -z "${TEST_VAR1}" ] ; then
      export TEST_VAR1="${TEST_VAR1}"
   fi
   if [ ! -z "${TEST_VAR2}" ] ; then
      export TEST_VAR2="${TEST_VAR2}"
   fi
fi

The above code works only in bash shell, since I have used export command to set the environment variable and it fails if I used it with any other shell. Is there is any command to set the environment variable which works in any shell ?

上面的代碼只在bash shell中工作,因為我使用了export命令來設置環境變量,如果我將它與任何其他shell一起使用,它將失敗。是否有命令來設置在任何shell中工作的環境變量?

2 个解决方案

#1


3  

"Fancier" shells like bash and zsh permit you to set a variable and export it as an environment variable at the same time like so:

類似bash和zsh的“更高級”shell允許您同時設置一個變量並將其導出為環境變量:

export FOO=bar

With a standard POSIX bourne shell, the equivalent is achieved by doing it in two commands:

使用標准的POSIX bourne shell,可以通過以下兩個命令來實現:

FOO=bar
export FOO

Note that once you've exported a variable, you can reset it to a different value later in the script and it's still exported (you don't need to export it again). Also, you can export several variables at a time:

注意,一旦導出了一個變量,您可以在腳本后面將其重置為一個不同的值,並且它仍然被導出(您不需要再次導出它)。此外,您還可以一次導出多個變量:

FOO=bar
BAZ=quux
export FOO BAZ

You mentioned tcsh in your comment, but csh and derivatives are completely different from bourne-based shells (and not recommended for use!). You can rarely make a shell script compatible with both sh and csh at the same time. For csh, look into setenv

您在評論中提到了tcsh,但是csh和衍生物與基於bourne的shell完全不同(不建議使用!)很難同時讓shell腳本兼容sh和csh。對於csh,請關注setenv

#2


0  

If you really want this to happen, it can be done, but it's tricky. One way to do it is to use awk to output the correct syntax and evaluate the text coming back from awk. To share a single environment variable value file between major sh and csh flavors, the following command in a file will import a variable value file to the environment: (yes, yes, it's one huge line, due to the inflexible way that some shells treat the backticks. If you didn't mind having a .awk file too, you could use awk -f...)

如果你真的想這樣做,這是可以做到的,但這很棘手。一種方法是使用awk輸出正確的語法並評估從awk返回的文本。要在主要的sh和csh風格之間共享一個環境變量值文件,文件中的以下命令將向環境導入一個變量值文件:(是的,是的,這是一行很大的一行,因為一些shell處理回簽的方式不靈活。如果你不介意有一個。awk文件,你可以使用awk -f…)

eval `awk '{ var = $1; $1="";  val=substr($0,2); if ( ENVIRON["SHELL"] ~ /csh$/) { print "setenv", var, " \"" val "\";" } else { print var "=\"" val "\"" ; print "export", var }}' $HOME/env_value_file`

The variable value file is in this format:

可變值文件的格式如下:

FOO value for foo
BAR foo bar
BAZ $BAR plus values $FOO

Design notes for educational purposes:

教育用途設計說明:

  1. In awk, there's no easy way of accessing fields 2-NF, so if there could be spaces in our variable values we need to modify $1 to get $0 to be close to get the value we want.
  2. 在awk中,沒有簡單的方法訪問字段2-NF,所以如果變量值中有空格,我們需要修改$1以使$0接近我們想要的值。
  3. To get this to work, since a SHELL variable is always set, but not as an environment variable and not with a consistent capitalization, you have to wet a SHELL environment variable from the shell's value as below. as an environment variable before you use the script.
  4. 要使其工作,由於始終設置SHELL變量,但不作為環境變量,也不具有一致的大小寫,因此必須從SHELL的值中提取SHELL環境變量,如下所示。在使用腳本之前作為環境變量。
  5. Also, if you want the new environment values to be present after the import environment script you need to source the environment script.
  6. 此外,如果您希望在導入環境腳本之后出現新的環境值,您需要為環境腳本提供源代碼。
  7. If a shell doesn't do eval well, you'll have to tweak the script.
  8. 如果shell不能很好地完成eval,您將不得不調整腳本。

For bourne shell flavors (bash, sh, ksh, zsh):

伯恩殼口味(bash, sh, ksh, zsh):

export SHELL
. import_environment

For csh flavors: (shell variable tends to be lower case in csh shells)

對於csh口味:(在csh殼中,殼變量往往是小寫的)

setenv SHELL "$shell"
source import_environment

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/03/18/7902187ce3fe0c0c22411dce33add211.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com