bru*_*ker 3 unix bash shell environment-variables
我需要做这样的事情
每小时运行此脚本并确保此变量的值现在是此脚本上次运行时设置的值。
我主要是在寻找如何设置和保留价值。我做了以下,但没有奏效。
export last_build="hello"
source .bashrc
Run Code Online (Sandbox Code Playgroud)
然后在我处理这个变量的脚本中
echo $last_build
export last_build="hello again"
echo $last_build
Run Code Online (Sandbox Code Playgroud)
当我运行这个脚本时,我得到类似的东西
hello
hello again
Run Code Online (Sandbox Code Playgroud)
当我第二次运行时,我希望输出是
hello again
hello again
Run Code Online (Sandbox Code Playgroud)
但它提供与第一次运行相同的输出。
Shell 变量是按进程存储的,因为每个 Shell 脚本都是它自己的进程,因此您不能以这种方式跨进程保存变量。但是,进程之间有多种通信方式:
就您的目的而言,文件似乎是最简单的方法。使用 bash 写入文件:
echo $VAR > /tmp/process.tmp.file.txt
Run Code Online (Sandbox Code Playgroud)
并从中阅读:
export VAR=`cat /tmp/process.tmp.file.txt`
Run Code Online (Sandbox Code Playgroud)