设置环境变量的Shell脚本

时间:2022-06-01 12:41:12

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

#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