解析 .env 文件的 Bash 命令包含等号 (=) 值

发布于 2025-01-20 03:09:19 字数 890 浏览 5 评论 0原文

我有一个 .env 文件,

# Some variables
USERNAME=user1
PASSWORD=

# Other variables
URL=https://example.com
TOKEN=muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==

目的

我的目的是创建一行 bash 命令来打印 中的每个非空变量。 格式,应

  1. 为每个变量打印一行,名称和名称之间带有 [space] 价值;
  2. 忽略评论;
  3. 忽略没有值的变量;

预期输出

预期输出是,

USERNAME user1
URL https://example.com
TOKEN muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==

当前解决方案

我当前的解决方案如下,

grep -v '^#' .env | grep -v '^[[:space:]]*$' | grep -v '=$' | sed 's/=/ /' | while read -r line; do echo $line; done

实际输出

USERNAME user1
URL https://example.com

它只打印出前两行,最后一行的问题是由 TOKEN 中的等号(=)引起的价值。

需要帮助

任何人都可以帮助我纠正命令吗?如果有更简单的方法来实现目标,也欢迎。

I have a .env file,

# Some variables
USERNAME=user1
PASSWORD=

# Other variables
URL=https://example.com
TOKEN=muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==

Purpose

My purpose is to create one line bash command to print each non-empty variables in <name> <value> format, which shall

  1. Print one line for each variable with [space] between name and
    value;
  2. Ignore comments;
  3. Ignore variables with no value;

Expected output

The expected output is,

USERNAME user1
URL https://example.com
TOKEN muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==

Current Solution

My current solution is as following,

grep -v '^#' .env | grep -v '^[[:space:]]*

Actual output

USERNAME user1
URL https://example.com

It only prints out the first two lines, the issue with last line is caused by the equal (=) signs in TOKEN value.

Help needed

Anyone can help me to rectify the command? also welcome if there is easier way to achieve the goal.

| grep -v '=

Actual output


It only prints out the first two lines, the issue with last line is caused by the equal (=) signs in TOKEN value.

Help needed

Anyone can help me to rectify the command? also welcome if there is easier way to achieve the goal.

| sed 's/=/ /' | while read -r line; do echo $line; done

Actual output

It only prints out the first two lines, the issue with last line is caused by the equal (=) signs in TOKEN value.

Help needed

Anyone can help me to rectify the command? also welcome if there is easier way to achieve the goal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不奢求什么 2025-01-27 03:09:19

Using sed

$ sed '/=\</!d;s/=/ /' input_file
USERNAME user1
URL https://example.com
TOKEN muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==

Delete lines that do not match a valid variable ie have no inpurt after =, this will also match blank lines and for your example data, lines with comments as there is线上没有有效的变量。最后,替换一个空间中=平等字符的第一次出现。

Using sed

$ sed '/=\</!d;s/=/ /' input_file
USERNAME user1
URL https://example.com
TOKEN muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==

Delete lines that do not match a valid variable i.e have no inpurt after =, this will also match blank lines and for your example data, lines with comments as there is no valid variable on the line. Finally, replace the first occurance of the = equal character for a space.

无力看清 2025-01-27 03:09:19

处理任何任何环境变量文件的唯一方法是实际解释它。下面描述了各种打印变量列表的方法。

各种“环境文件”通常设计为POSIX Shell运行以评估所有变量。这使得在分配到变量时可以调用外部命令,执行计算等。

这样做仅是您相信输入文件。使用以下脚本解析文件可以执行任何命令。

您可以源自文件(indentin命令):

. .env

在摘要之后,可以评估变量。如果您知道要打印的所有变量,请使用此方法(对于vars 用户名password

. .env

for var_name in USERNAME PASSWORD
do
    printf "%s %s\n" "$var_name" "${!var_name}"
done

如果要打印明确指定的所有变量,请使用此方法。 :

. .env

grep '^\w+=' | sed 's/=//' | while read var_name
do
    printf "%s %s\n" "$var_name" "${!var_name}"
done

但是该解决方案也不是完美的,因为.env文件可以使用不同于var_name = value不同的构造。分配VAR。使用set命令打印所有变量。

这将打印出绝对所有变量。 (实际上,环境文件应从系统环境VAR继承其变量,因此其中很多。)

. .env
printenv | sed 's/=/ /'

sed用于摆脱平等符号。

提防一些危险的变量值。这些变量甚至包含newline字符,背景和其他控制字符。


还有许多不同的实用程序来以使其将它们加载回外壳的方式打印变量。例如:

声明-P类型-P - 打印出声明命令要声明所有当前设置的 shell 变量

导出-p - 相同,但对于 emoverive 变量(这可能是您想要的东西

The only way to process any environment variable file is to actually interpret it. Various methods for printing out the variable lists are described below.

Various “environment files” are usually designed to be run by POSIX shell to evaluate all the variables. That makes it possible to call external commands, perform calculations etc. when assigning to the variables.

Do this only is you trust you input file. Any command can be executed by parsing the file using the script below.

You can source the file (builtin command .):

. .env

After you source it, you can evaluate the variables. If you know what all variables you want to print, use this (for vars USERNAME and PASSWORD)

. .env

for var_name in USERNAME PASSWORD
do
    printf "%s %s\n" "$var_name" "${!var_name}"
done

If you want to print all the variables that are explicitly specified, use this approach:

. .env

grep '^\w+=' | sed 's/=//' | while read var_name
do
    printf "%s %s\n" "$var_name" "${!var_name}"
done

But this solution is also not perfect, because the .env file can use constructions different from var_name=value to assign the vars. Use the set command to print all variables.

This prints out absolutely all variables. (In reality, the environment file should inherit its variables from the system environment vars, so there is a lot of them.)

. .env
printenv | sed 's/=/ /'

The sed is used to get rid of the equals signs.

Beware of some dangerous variable values. The variables can contain even newline character, backspaces and other control characters.


There is also a lot of various utilities for printing the variable in a way that allow loading them back to the shell. For example:

declare -p or typeset -p — prints out declare commands to declare all the currently set shell variables

export -p — the same, but for environment variables (that is probably that thing you want)

set — prints out all shell variables

被翻牌 2025-01-27 03:09:19

如果文件仅包含注释和简单的 key=value 行,字符串中没有 # 字符,则可以使用此管道:

sed 's/#.*//' | fgrep = | sed 's/=/ /'

或者如果您不想输出空变量,请使用这:

sed 's/#.*//' | grep =. | sed 's/=/ /'

If the file contains only comments and simple key=value lines without # chars in strings, you can use this pipeline:

sed 's/#.*//' | fgrep = | sed 's/=/ /'

Or if you do not want to output empty variables, use this:

sed 's/#.*//' | grep =. | sed 's/=/ /'
一场信仰旅途 2025-01-27 03:09:19

我会做这样的事情

bash -c 'source <(cat .env | grep USERNAME); echo $USERNAME'

为什么?

  • 通过 source 重用解析逻辑的熟悉命令很
  • 容易理解,
  • 而不是实现一个,

为什么不呢?

  • 键重复
    • 尽管用函数包装可以减轻这种情况
  • 您可能更喜欢使用 sed 或其他解析方法的
  • 情况,但出于某种原因,您可能不想生成新的 shell bash -c

灵感来自 https://unix.stackexchange.com/a/176874

I would do something like this

bash -c 'source <(cat .env | grep USERNAME); echo $USERNAME'

why?

  • short-ish
  • easy to understand with familiar commands
  • reusing parsing logic via source instead of implementing one

why not?

  • key is repeating
    • although wrapping with a function would mitigate that
  • maybe you prefer using sed or other parsing methods
  • maybe you don't want to spawn a new shell bash -c for some reason

inspired by https://unix.stackexchange.com/a/176874

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文