【问题标题】:export variables from files within folder从文件夹中的文件导出变量
【发布时间】:2023-02-10 22:19:00
【问题描述】:

我正在尝试读取 config_files 文件夹中所有以 .env 结尾的文件,然后运行 ​​export 命令以将它们作为环境变量提供。

到目前为止,我已经尝试过:

#! /bin/bash
for file in "$(find ~/config_files -maxdepth 3 -name '*.env')"; do export $(grep -v '^#' $file | xargs); done

#! /bin/bash
for file in  "$(find ~/config_files -regex '.*/.*\.\(env\)$')"; do export $(xargs < $file); done

最终总是会出现 declare -x 问题,例如:

声明-x COLORTERM="真彩色"

我还尝试将 -print 添加到 bash 文件中,例如:

for file in "$(find ~/.ros/PS_AD/config_files -maxdepth 3 -name '*.env' -print)"; do export $(grep -v '^#' $file | xargs); done

但后来我得到了:

./script: 第 3 行:导出:`/home/imr/config_files/docker-image/docker_specs.env:random=1': 不是有效标识符

*.env 文件如下所示:

random=1

我错过了什么?

【问题讨论】:

    标签: linux bash


    【解决方案1】:

    你应该使用 while IFS= 'loop' over the lines,然后使用 export $(xargs &lt; path) to export those 到 shell:

    $ cat foo/a.env
    foo=bar
    $
    $ find /tmp/foo -type f -print0 | 
        while IFS= read -r -d ''  p; do export $(xargs < $p); done
    $
    $ echo $foo
    bar
    $
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2021-08-18
      • 2022-11-23
      • 2019-09-09
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      相关资源
      最近更新 更多