当前位置:网站首页>实际工作中用到的shell命令 - sed
实际工作中用到的shell命令 - sed
2022-06-27 18:41:00 【我是胖虎啊】
背景
目前跑的接口自动化是通过Testng + Jenkins来做的, 但是项目可能跑在不同的平台上, 不同的平台有不同的ip.
目前是使用了Jenkins的参数化构建功能, 每次跑的时候,手动输入一个ip,然后对该ip的服务进行自动化测试.
Testng框架的配置文件是application.properties, 我们将服务的ip记录在这个文件中.
需求
根据从Jenkins传入的参数, 动态修改application.properties中的ip变量,然后进行后续测试工作.
application.properties 示例
server_ip=http://192.168.100.100
port=9000
path=/home/data
需求分析
目前想到的方法有2个:
- 用python脚本读取文件,正则替换制定文本,生成个新文件
- 用shell脚本来操作
从最简化考虑,优先考虑shell的方法.
Linux有3剑客: grep ,sed, awk.其中grep主要做过滤, sed主要做文本的相关处理(如修改替换等),awk主要做数据处理,报告输出等。所以应该考虑使用sed命令来解决此问题.
实际操作
- 从网上搜了下相关的sed用法, 参考链接: https://blog.csdn.net/ganfanren00001/article/details/122765854
本次用到的sed命令截图
- 举一反三, 我是这样使用的
sed "/server_ip=/c server_ip=http://192.168.200.200" application.properties
- 但是这样有个问题,这样修改的只是暂时的,并没实际影响到真实的文件.其实sed有个-i参数,加上这个参数才会对真实的文件生效, 所以优化下就是
sed -i "/server_ip=/c server_ip=http://192.168.200.200" application.properties
- 这个命令在本地跑通了,接下来就是集成到Jenkins中使用这个命令
- 首先在Jenkins中,选择参数化构建,我定义的参数名是server_ip
- 在shell脚本中使用这个变量即可
注: Jenkins使用传入变量的格式为:${变量名}
#!/bin/bash
echo "传入的ip是:${server_ip}"
# 根据传入参数修改项目的ip
cd /home/data/jenkins/workspace/maven_testng/maven_testng/src/main/resources
sed -i "/server_ip=/c server_ip=http://${server_ip}" application.properties
echo "项目已更改为传入的ip: ${server_ip}"
踩坑点: sed 命令后面要使用双引号, 不能使用单引号, 否则会将变量识别成普通字符串!
边栏推荐
- UOS prompts for password to unlock your login key ring solution
- pfSense Plus22.01中文定制版发布
- ABAP-CL_ OBJECT_ Collection tool class
- SQL audit platform permission module introduction and account creation tutorial
- On the drawing skills of my writing career
- Linux system plays Oracle database multi table query connection query with a smile
- [STL programming] [common competition] [Part 1]
- 优维HyperInsight:掘金164.94亿美元可观测市场的“金锄头”?
- 原创翻译 | 机器学习模型服务工具对比:KServe,Seldon Core和BentoML
- 动物养殖生产虚拟仿真教学系统|华锐互动
猜你喜欢
I haven't thought about the source for some time. After upgrading to the latest version 24, the data encryption problem is repeatedly displayed
![[STL programming] [common competition] [Part 3]](/img/15/0c397d74128268e17897615ad1c84e.png)
[STL programming] [common competition] [Part 3]

抗洪救灾,共克时艰,城联优品驰援英德捐赠爱心物资

Postman 汉化教程(Postman中文版)

智联招聘的基于 Nebula Graph 的推荐实践分享

What is a low code development platform? Why is it so hot now?

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献

微信iOS版8.0.24更新发布 缓存细分清理上线

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

Mongodb introduction and typical application scenarios
随机推荐
基于微信小程序的高校毕业论文管理系统#毕业设计
一场分销裂变活动,不止是发发朋友圈这么简单
Recommended practice sharing of Zhilian recruitment based on Nebula graph
308. 2D area and retrieval - variable segment tree / hash
什么是堆栈?
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
No wonder people chose apifox instead of postman
Redis persistence
二叉树相关问题2
基于微信小程序的高校党员之家服务管理系统系统小程序#毕业设计,党员,积极分子,学习,打卡,论坛
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
Massive data attended the Lanzhou opengauss meetup (ECOLOGICAL NATIONAL trip) activity, enabling users to upgrade their applications with enterprise level databases
Summary of redis big key problem handling
爱数课实验 | 第六期-金融反欺诈案例研究
开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS
Database lock problem
After kotlin wechat payment callback, the interface is stuck and uipagefragmentactivity windowleft is thrown
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
qt中文乱码
SQL reported an unusual error, which confused the new interns