【问题标题】:bash, Good practice wanted on how to make the variable of a bash script available to another bash script?bash,关于如何使 bash 脚本的变量可用于另一个 bash 脚本的良好实践?
【发布时间】:2021-11-15 12:58:11
【问题描述】:

众所周知,通过将多个脚本使用的公共变量存储在一个公共文件中,可以使它们彼此可用。

与通过保存在 RAM 中的变量进行的交换相比,多个脚本之间通过硬盘上的文件交换变量值会导致相应硬盘上的频繁写入操作。

已知方式的软件实现:

# write a var to a file
echo "var_01=Alfa" > var_file.sh

# set a new value to a var
sed -i 's/var_01=Alfa/var_01=Berta/' var_file.sh

# delete the value of a var and the var
sed -i 's/var_01=Alfa/""/' var_file.sh

# make a var readable to a script by Include var_file.sh
source ./var_file.sh

什么是更好的做法,如何使 bash 脚本的变量可用于另一个 bash 脚本?

备注: 搜索的是:

  • 一种通过多个 bash 脚本使用 variales 的方法,无需为需要在文件中存储 var 实际值的 vars 提供帮助文件。它可以在 RAM、pe 上进行。通过非变量类型,可能是全局变量或环境变量等。

【问题讨论】:

  • 如果其他脚本是当前进程的(直接或间接)子进程,可以将变量放入环境中。
  • @user1934428 尽管使用环境变量有一些限制,例如不支持 Bash 使用的任何类型的数组或变量标志。持久保存这些和所有属性标志的唯一方法是将declare -p 的输出写入文件,然后导入文件以重新加载其声明。
  • @LéaGris :使用其他方式(例如通过文件)也仅支持文本流。如果要传递类型化的数据,则必须使用传输格式(JSON、YAML 或其他)。最后,declare -p 正在生成这样的格式,如果你 source 之后的文件意味着解析它。

标签: bash variables


【解决方案1】:

使用 bash,使用 declare 关键字可靠地(正确引用或转义特殊字符)将变量保存到文件,即使它包含空格或任何特殊字符。

最终,您可以将save_vars 函数合并到您与所有其他脚本共享的var_file.sh 中,以保存和恢复/共享变量。

var_file.sh

#!/usr/bin/env bash

# Check file is sourced from a bash script or exit with error
if [ -z "$BASH_VERSION" ] || ! (return 0 2>/dev/null); then
  printf 'This %s file is meant to be sourced by a Bash Script\n' "$0" >&2
  exit 1
fi

# Function to save itself and variable names in arguments
# into the current file
# (here it is var_file.sh but could be whatever it is named)
save_vars() {
  # save variables file
  {
    # Write self shebang and usage error handling
    cat <<'EOF'
#!/usr/bin/env bash

if [ -z "$BASH_VERSION" ] || ! (return 0 2>/dev/null); then
  printf 'This %s file is meant to be sourced by a Bash Script\n' "$0" >&2
  exit 1
fi

EOF

    # Save itself "${FUNCNAME[0]}" is the name of
    # the current function here it is save_vars
    declare -f "${FUNCNAME[0]}"

    # Save variable names passed as arguments
    declare -p "$@"

    # Into itself file (self-modify or update)
  } >"${BASH_SOURCE[0]}" 2>/dev/null
}

script1:

#!/usr/bin/env bash

# load variables or exit with failure
. var_file.sh || exit 1

printf 'Hello I am %s\n' "${BASH_SOURCE[0]}"

# Assign value even with spaces and special chars
var_01=$'Alfa Beta Gamma \317\206'
var_02='Something with
a newline'

# Invoke the save_vars function that was imported
# from var_file.sh to save var_01 and var_02
save_vars var_01 var_02

script2:

#!/usr/bin/env bash

# load variables or exit with failure
. var_file.sh || exit 1

printf 'Hello I am %s\n' "${BASH_SOURCE[0]}"
printf 'var_01 contains: %q\n' "$var_01"
printf 'var_02 contains: %q\n' "$var_02"

# set a new value to a var
var_01='Berta'

# delete var_02
unset var_02

# Invoke the save_vars function that was imported
# from var_file.sh to save var_01 only
# (var_02 does not need to be saved since it is deleted
# it will be removed/gone from the save)
save_vars var_01

script3

#!/usr/bin/env bash

# load variables or exit with failure
. var_file.sh || exit 1

printf 'Hello I am %s\n' "${BASH_SOURCE[0]}"
printf 'var_01 contains: %q\n' "$var_01"
printf 'var_02 contains: %q\n' "$var_02"

【讨论】:

  • 2 &gt; /dev/null 应该是2&gt; /dev/null
  • @Alfred.37 我已经更新了通用化并评论了更多不同的脚本。我建议您将它们全部保存并与它们一起玩,然后更改它们并看看如果您这样做或那样做会发生什么。这将有助于理解这一切是如何运作的。
  • @Léa Gris,如果我通过 Linux 编辑器删除 var_file.sh 上的空第一行,保存该字段并通​​过 Linux 编辑器重新打开文件,则不存在空的第一行。运行 scrit1 或 script2 后,var_file.sh 在#!/usr/bin/env bash 之前有一个空行。另一件事是,如果我在 var_file.sh 上删除此双重代码,则 var_file.sh 上的一部分代码通过运行 script1 和 script2 是双重和双重的。
  • @Alfred.37 然后检查cat here-document start cat &lt;&lt;'EOF' 和 here-document 正文中的 shebang 行之间没有空行:#!/usr/bin/env bash。一个 here-document 立即从 &lt;&lt;MARKER 之后的下一行开始。
猜你喜欢
  • 2018-10-09
  • 2017-01-26
  • 1970-01-01
  • 2020-10-06
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
相关资源
最近更新 更多