首页 > 系统相关 >shell读取配置文件中的参数

shell读取配置文件中的参数

时间:2022-12-03 22:55:58浏览次数:37  
标签:shell 读取 配置文件 ip age echo user config properties

shell读取配置文件中的参数 配置文件

配置文件config.properties如下:

user=bk
age=25
ip=192.168.37.1

 一、使用cat+awk语句过滤并赋值变量

user=`cat config.properties | awk -F "=" 'NR==1{print $2}'  `
age=`cat config.properties | awk -F "=" 'NR==2{print $2}'  `
ip=`cat config.properties | awk -F "=" 'NR==3{print $2}'  `
echo $user
echo $age
echo $ip

查看运行结果:

 

二、用sed 流处理器,取得=号后面的部分,并赋给变量

user=`sed '/^user=/!d;s/.*=//' config.properties`
age=`sed '/^age=/!d;s/.*=//' config.properties`
ip=`sed '/^ip=/!d;s/.*=//' config.properties`
echo $user
echo $age
echo $ip

查看运行结果:

 

三.使用eval方法解析配置

while read line;do
    eval "$line"
done < config.properties
echo $user
echo $age
echo $ip

查看运行结果:

 

四.使用load将配置加载入环境变量

source /root/test20221203/config.properties
echo $user
echo $age
echo $ip

查看运行结果:

 

标签:shell,读取,配置文件,ip,age,echo,user,config,properties
From: https://www.cnblogs.com/mrwhite2020/p/16793525.html

相关文章

  • Shell脚本(一)
    Shell:命令行解释器,接收应用程序/用户命令,然后调用操作系统内核。1.入门脚本以#!/bin/bash开头1xqchang@sam-System-Product-Name:~/linux/datas$touchhelloworld.s......
  • jenkins集成shell与gitlab
    jenkins集成shell    故障:                     故障:  开发提交了代码,然后自动发布测试环境持续集成持续交......
  • 16.读取TXT中内容放到列表中
    #操作思路:#1.打开指定的TXT#2.读取TXT中的内容#3.处理数据#4.将内容存放在数组中#5.打印数组中的数据defprint_hi(name):print(f'Hi,{name}')wit......
  • python | 一个简单的icmp shell实现(不完善)
    python|一个简单的icmpshell实现(不完善)一个是server端,一个是cilent端,其实问题还是很多的,尤其是在真实网络中的时候,这个shell只适用于直连的情况,并且data不能太大(因为ic......
  • pip 生成requirement配置文件,快速安装配置环境
    pip生成requirement配置文件,快速安装配置环境在当前目录下生成requirements.txt文件pipfreeze>./requirements.txt按照requirement配置文件,快速配置环境pipinst......
  • Python笔记-从配置读取参数
    实用的脚步通常需要一些动态参数,如果参数太多,从命令行传递就太麻烦了。从配置文件读取,是比较实用的方法。以下示例为从test.cfg中读取参数,配置文件为json格式。配置文件......
  • shell实现的进度条
    ​​具体脚本如下progressbar.sh​​:#TheMITLicense(MIT)#Copyright(c)2014HaiKieu#Permissionisherebygranted,freeofcharge,toanypersonobtainingacop......
  • wexpect - 一个可以自动化交互式shell输入的库
    linux下有个pexpect的lib,可以实现一些交互式输入,pexpect网上的教程很多,这里就不多说了;但这个库不能用于windows,如果要实现windows下shell自动化交互式操作的话,可以参考p......
  • typesafe读取配置文件
    typesafe可以读取properties文件、.config文件1.pom<dependency><groupId>com.typesafe</groupId><artifactId>config</artifact......
  • scipy.io.wavfile.read, soundfile.read, librosa.load三种读取音频文件的方式的区
    scipy.io.wavfile.read,soundfile.read,librosa.load三种读取音频文件的方式的区别importscipy.io.wavfileaswavfileimportsoundfileassfimportlibrosaimpo......