本文介绍了根据文件中的键值对设置环境变量,其中某些值为引用的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了以下bash脚本,以便从名为 params.env 的特定文件导出值:

I have made the following bash script in order to export values from a specific file named params.env:

#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done

SOURCE=$(dirname ${SOURCE})

export $(cat "${SOURCE}/../params.env" | xargs)

params.env 具有以下值:

Param1=param1
Param2="Space separated value"

但是它成功导出Param1,但是它无法导出Param2。

But it successfully exports Param1 but it fails to export Param2.

你有什么想法吗?

推荐答案

首先 - 如果您信任您的文件以您当前用户的权限安全地评估为shell脚本,则可能很简单:

First -- if you trust your file to be safely evaluated as a shell script with your current user's privileges, this could be as simple as:

set -a                           # automatically export all shell variables
source "${SOURCE}/../params.env" # evaluate file as a shell script
set +a                           # turn off automatic export






否则,是适当的。

这篇关于根据文件中的键值对设置环境变量,其中某些值为引用的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:26