linux source和export有什么区别?

w8f9ii69  于 2023-06-21  发布在  Linux
关注(0)|答案(2)|浏览(152)

我写了一个shell脚本,读取一个文件,其中有键=值对,并设置这些变量作为环境变量。但我有一个疑问,如果我做source file.txt将设置该文件中定义的变量作为环境变量,或者我应该读取文件逐行,并设置它使用导出命令?
在这种情况下,源命令与导出命令是否不同?

oogrdqng

oogrdqng1#

当您source文件时,将设置赋值,但不会导出变量,除非已设置allexport选项。如果您希望导出所有变量,那么使用allexportsource文件要比读取文件并显式使用export简单得多。换句话说,你应该做:

set -a
. file.txt

(我更喜欢.,因为它比source更便携,但sourcebash中工作得很好。
请注意,导出变量并不会使其成为环境变量。它只是使其成为任何子shell中的环境变量。

x33g5p2x

x33g5p2x2#

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

简而言之:

  1. source some_script.sh或与POSIX兼容的等价物. some_script.sh * 从其他脚本 * 中引入变量,而
  2. export my_var="something" * 将变量推送到从当前脚本/进程调用/启动的其他脚本/进程 *。
    在Linux shell脚本中使用source some_script.sh. some_script.sh有点像在Python中使用import some_module,或者在C或C++中使用#include <some_header_file.h>它从源脚本中引入变量。
    使用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. unsetunexports

示例:

创建此脚本:

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

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

  1. shell脚本flock命令:https://man7.org/linux/man-pages/man1/flock.1.html。参见man flockman 1 flock
  2. Linux library C命令:https://man7.org/linux/man-pages/man2/flock.2.html。参见man 2 flock。必须在C文件中使用#include <sys/file.h>才能使用此函数。

参考文献:

  1. https://askubuntu.com/questions/862236/source-vs-export-vs-export-ld-library-path/862256#862256
    1.我自己的实验和测试
    1.我将把上面的例子添加到我在GitHub上的项目中,在bash文件夹下:https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world

相关问题