当前位置:网站首页>实际工作中用到的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 命令后面要使用双引号, 不能使用单引号, 否则会将变量识别成普通字符串!
边栏推荐
- It took me 6 months to complete the excellent graduation project of undergraduate course. What have I done?
- 【精品必读】Linux系统Oracle数据库趣解子查询
- Safety is the last word, Volvo xc40 recharge
- Database lock problem
- 开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS
- Cocoscreator plays audio and synchronizes progress
- Binary tree related problems 2
- Select auto increment or sequence for primary key selection?
- Is it safe to open an account and buy stocks on the Internet? New to stocks, no guidance
- CSDN 技能樹使用體驗與產品分析(1)
猜你喜欢

最佳实践:优化Postgres查询性能(下)

优维HyperInsight:掘金164.94亿美元可观测市场的“金锄头”?

Grasp the detailed procedure of function call stack from instruction reading

On the drawing skills of my writing career

Runmaide medical opened the offering: without the participation of cornerstone investors, the amount of loss doubled

BTC和ETH重新夺回失地!引领市场复苏?加密将步入“冰河时代”!

Summary of redis big key problem handling

redis数据结构

Oracle architecture summary

Openharmony hisysevent dotting and calling practice of # Summer Challenge (L2)
随机推荐
数仓的字符截取三胞胎:substrb、substr、substring
UE4: explanation of build configuration and config
Best practice: optimizing Postgres query performance (Part 2)
元宇宙虚拟数字人离我们更近了|华锐互动
Yyds dry goods counting SQL sub query
基于微信小程序的高校毕业论文管理系统#毕业设计
When developing digital collections, how should cultural and Museum institutions grasp the scale of public welfare and Commerce? How to ensure the security of cultural relics data?
低代码开发平台是什么?为什么现在那么火?
SQL reported an unusual error, which confused the new interns
花了6个月时间完成本科优秀毕业设计,我做了什么?
原创翻译 | 机器学习模型服务工具对比:KServe,Seldon Core和BentoML
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Pfsense plus22.01 Chinese customized version release
Implementation string mystring
开启生态新姿势 | 使用 WrodPress 远程附件存储到 COS
[数组]BM99 顺时针旋转矩阵-简单
如何降低用户关注的非必要页面的权重传递?
Oracle architecture summary
Necessary software tools in embedded software development
数据库事务