38

我正在编写一个 shell 脚本,以读取具有 key=value 对的文件并将这些变量设置为环境变量。但我有一个疑问,如果我source file.txt将在该文件中定义的变量设置为环境变量,或者我应该逐行读取文件并使用导出命令设置它?

在这种情况下,源命令与导出不同吗?

4

2 回答 2

36

当您source创建文件时,将设置分配,但除非allexport设置了选项,否则不会导出变量。如果要导出所有变量,则使用文件allexportsource文件比读取文件和export显式使用要简单得多。换句话说,你应该这样做:

set -a
. file.txt

(我更喜欢.它,因为它比 更便携source,但source在 中工作得很好bash。)

请注意,导出变量不会使其成为环境变量。它只是使它成为任何子shell中的环境变量。

于 2013-03-18T11:13:58.113 回答
21

source( .) vs export(最后还有一些文件锁 [ flock] 的东西):

简而言之:

  1. source some_script.sh, 或 POSIX 兼容的等价物 ,. some_script.sh从其他脚本中引入变量,而
  2. export my_var="something" 将变量推送到从当前脚本/进程调用/启动的其他脚本/进程。

在 Linux shell 脚本中使用source some_script.shor. some_script.sh有点像import some_module在 Python、#include <some_header_file.h>C 或 C++ 中使用。它从源代码中引入变量。

使用export some_var="something"有点像在本地设置该变量,因此它可用于当前脚本或进程的其余部分,然后还将其传递给您从此时起可能调用的任何和所有子脚本或进程。

更多细节:

所以这:

# export `some_var` so that it is set and available in the current script/process,
# as well as in all sub-scripts or processes which are called from the
# current script/process
export some_var="something"
# call other scripts/processes, passing in `some_var` to them automatically
# since it was just exported above! 
script1.sh  # this script now gets direct access to `some_var`
script2.sh  # as does this one
script3.sh  # and this one

好像你已经这样做了:

# set this variable for the current script/process only
some_var="something" 
# call other scripts/processes, passing in `some_var` to them **manually**
# so they can use it too 
some_var="something" script1.sh  # manually pass in `some_var` to this script
some_var="something" script2.sh  # manually pass in `some_var` to this script
some_var="something" script3.sh  # manually pass in `some_var` to this script

除了上面的第一个版本,我们调用的地方export some_var="something"实际上是递归地将变量传递或导出到子进程,所以如果我们script1.sh从当前脚本/进程内部调用,那么script1.sh将从当前脚本中获取导出的变量,如果script1.sh调用script5.sh, 和script5.sh调用script10.sh, 那么这两个脚本也将自动获取导出的变量。这与上面的手动情况相反,只有那些在调用脚本时使用手动设置的变量显式调用的脚本才能获取它们,因此子脚本不会自动从它们的调用脚本中获取任何变量!

如何“取消导出”变量:

请注意,一旦您导出了一个变量,调用unset它将“取消导出它”,如下所示:

# set and export `some_var` so that sub-processes will receive it
export some_var="something"
script1.sh  # this script automatically receives `some_var`

# unset and un-export `some_var` so that sub-processes will no longer receive it
unset some_var
script1.sh  # this script does NOT automatically receive `some_var`

总之:

  1. source. 进口
  2. export 出口
  3. unset 不出口

例子:

创建此脚本:

source_and_export.sh

#!/bin/bash

echo "var1 = $var1"
var2="world"

然后将其标记为可执行:

chmod +x source_and_export.sh

现在,我在终端上运行一些命令来测试source( .) 和export使用此脚本的命令。在以开头的行之后输入您看到的命令$(不包括注释)。其他行是输出。按顺序运行命令,一次一个命令:

$ echo "$var1"              # var1 contains nothing locally

$ var1="hello"              # set var1 to something in the current process only
$ ./source_and_export.sh    # call a sub-process
var1 =                      # the sub-process can't see what I just set var1 to
$ export var1               # **export** var1 so sub-processes will receive it
$ ./source_and_export.sh    # call a sub-process
var1 = hello                # now the sub-process sees what I previously set var1 to
$ echo "$var1 $var2"        # but I can't see var2 from the subprocess/subscript
hello 
$ . ./source_and_export.sh  # **source** the sub-script to _import_ its var2 into the current process
var1 = hello
$ echo "$var1 $var2"        # now I CAN see what the subprocess set var2 to because I **sourced it!**
hello world                 # BOTH var1 from the current process and var2 from the sub-process print in the current process!
$ unset var1                # unexport (`unset`) var1
$ echo "$var1"              # var1 is now NOT set in the current process
$ ./source_and_export.sh    # and the sub-process doesn't receive it either
var1 = 
$ var1="hey"                # set var1 again in the current process
$ . ./source_and_export.sh  # if I **source** the script, it runs in the current process, so it CAN see var1 from the current process!
var1 = hey                  # notice it prints
$ ./source_and_export.sh    # but if I run the script as a sub-process, it can NOT see var1 now because it was `unset` (unexported)
var1 =                      #   above and has NOT been `export`ed again since then!
$

使用文件作为进程之间的全局变量

有时,特别是在编写脚本来启动程序和事物时,我遇到了一些export似乎无法正常工作的情况。在这些情况下,有时必须求助于使用文件本身作为全局变量来将信息从一个程序传递到另一个程序。这是如何做到的。在此示例中,文件“~/temp/.do_something”的存在用作进程间布尔变量:

# In program A, if the file "~/temp/.do_something" does NOT exist, 
# then create it
mkdir -p ~/temp
if [ ! -f ~/temp/.do_something ]; then
    touch ~/temp/.do_something  # create the file
fi


# In program B, check to see if the file exists, and act accordingly

mkdir -p ~/temp
DO_SOMETHING="false"
if [ -f ~/temp/.do_something ]; then
    DO_SOMETHING="true"
fi

if [ "$DO_SOMETHING" == "true" ] && [ "$SOME_OTHER_VAR" == "whatever" ]; then 
    # remove this global file "variable" so we don't act on it again
    # until "program A" is called again and re-creates the file
    rm ~/temp/.do_something 
    do_something
else
    do_something_else
fi

如上所示,简单地检查文件是否存在对于在程序和进程之间全局传递布尔条件非常有用。但是,如果您需要传递更复杂的变量,例如字符串或数字,您可能需要通过将这些值写入文件来执行此操作。在这种情况下,您应该使用文件锁定功能flock,以正确确保进程间同步。它是一种进程安全(即:“进程间”)互斥体原语。你可以在这里读到它:

  1. shell 脚本flock命令:https ://man7.org/linux/man-pages/man1/flock.1.html 。另见man flockman 1 flock
  2. Linux 库 C 命令:https ://man7.org/linux/man-pages/man2/flock.2.html 。另请参阅man 2 flock。你必须#include <sys/file.h>在你的 C 文件中使用这个函数。

参考:

  1. https://askubuntu.com/questions/862236/source-vs-export-vs-export-ld-library-path/862256#862256
  2. 我自己的实验和测试
  3. 我将把上面的例子添加到我在 GitHub 上的项目中,在bash文件夹下:https ://github.com/ElectricRCAAircraftGuy/eRCaGuy_hello_world
于 2020-06-28T18:17:31.533 回答