需要 bash shell 脚本来从文件中读取名称值对 [英] Need bash shell script for reading name value pairs from a file

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

问题描述

我有一个类似的文件

name1=value1
name2=value2

我需要使用 shell 脚本读取这个文件并设置变量

I need to read this file using shell script and set variables

$name1=value1
$name2=value2

请提供可以执行此操作的脚本.

Please provide a script that can do this.

我尝试了下面的第一个答案,即获取属性文件,但如果值包含空格,我会遇到问题.它被解释为空格后的新命令.我怎样才能让它在有空格的情况下工作?

I tried the first answer below, i.e. sourcing the properties file but I'm getting a problem if the value contains spaces. It gets interpreted as a new command after the space. How can I get it to work in the presence of spaces?

推荐答案

使用:

while read -r line; do declare  "$line"; done <file

这篇关于需要 bash shell 脚本来从文件中读取名称值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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