【问题标题】:What does two at (@) signs surrounding a string mean in a shell script?在 shell 脚本中,字符串周围的两个 at (@) 符号是什么意思?
【发布时间】:2021-05-01 17:56:51
【问题描述】:

例如,

# Execute the pre-hook.
export SHELL=@shell@
param1=@param1@
param2=@param2@
param3=@param3@
param4=@param4@
param5=@param5@
if test -n "@preHook@"; then
    . @preHook@
fi

对于上下文,这是from a shell script in a commit from 2004 in the Nixpkgs repo;试图查看这是否可能是一个参考功能,但字符串“shell”在整个文件中只出现一次(在区分大小写的搜索中)。

【问题讨论】:

  • 评论移至答案,感谢您的鼓励。
  • @CharlesDuffy 感谢您花时间提供这些细节!
  • 顺便说一句,还有一些其他@foo@ 示例根本与shell 无关;如果我没记错的话(可能不是,已经很多年了),这与 CVS 用于将上次更改时间戳和修订号等内容替换为需要它们的源文件的形式相同,f/e。
  • (查了一下,我错了;那是$foo$

标签: shell nixpkgs


【解决方案1】:

answer by Chris Dodd 是正确的,因为 shell 没有内在含义——因此@foo@ 通常用作标记。就你在 nixpkgs 中遇到的情况而言,它提供了一些专门用于实现这种模式的 stdenv 工具。

https://nixos.org/manual/nixpkgs/stable/#ssec-stdenv-functions 中所述,nixpkgs stdenv 提供了 shell 函数,包括 substitutesubstituteAllsubstituteInPlace 等。这会将@foo@ 值替换为相应变量的内容。


在链接提交的上下文中,可以看到在pkgs/build-wrapper/gcc-wrapper/builder.sh 中执行了该形式的替换:

    sed \
        -e "s^@gcc@^$src^g" \
        -e "s^@out@^$out^g" \
        -e "s^@bash@^$SHELL^g" \
        -e "s^@shell@^$shell^g" \
        < $gccWrapper > $dst

...正在用$out 的值替换@out@,用$SHELL 的值替换@bash@,等等。

【讨论】:

    【解决方案2】:

    @ 符号对 shell 没有任何意义——它是一个标点符号,几乎不会出现在任何实际的 shell 脚本中。

    这使它成为脚本模板中使用模式的好选择——基本思想是使用简单的搜索和替换过程(可能使用您显示的链接中的 sed 脚本)来重写模板转换为实际的 shell 脚本。模板中@name@ 形式的每个字符串都将替换为与脚本安装环境相关的其他字符串。

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 2016-04-15
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      相关资源
      最近更新 更多