使用bash从文本文件中提取变量的值 [英] extracting a variable's value from text file using bash

查看:388
本文介绍了使用bash从文本文件中提取变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Linux和bash. 我有一个简单的文本文件,如下所示:

I am using Linux and bash. I have a simple text file like below:

VAR1=100
VAR2=5
VAR3=0
VAR4=99

我想通过bash提取VAR2的值,即5.

I want to extract by means of bash the value of VAR2, that is 5.

我该怎么办?

推荐答案

最简单的方法可能是使用source或只是.来读取和执行文件.这将适用于您的示例,因为变量值中没有空格.否则,您需要使用grep + cut或awk,如其他答案所述.

The simplest way might be to use source or simply . to read and execute the file. This would work with your example, because there are no spaces in the variable values. Otherwise you need to use grep + cut or awk, as stated in other answers.

. /path/to/your/file
echo $VAR2

如dawg所述,这将使脚本中的其他变量也可用,并且可能会覆盖现有变量.

[edit] As stated by dawg, this would make the other variables available in your script too, and possibly overwrite existing variables.

这篇关于使用bash从文本文件中提取变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆