当前位置:网站首页>Shell 脚本的替换功能实现
Shell 脚本的替换功能实现
2022-07-07 04:31:00 【Wu_Candy】
Shell 简介
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。
Shell 既是一种命令语言,又是一种程序设计语言。
Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
Shell 脚本运行
Shell 脚本(shell script),是一种为 Shell 编写的脚本程序。
运行 Shell 脚本有两种方法:
1、作为可执行程序
Step1: 将代码保存为 test.sh 文件
Step2: 并 cd 到test.sh 文件所在目录
Step3: chmod +x ./test.sh #使脚本具有执行权限
Step4: ./test.sh #执行脚本
2、作为解释器参数
这种运行方式是,直接运行解释器,其参数就是 shell 脚本的文件名,执行命令如下:
/bin/bash test.sh
Shell 脚本替换功能实现
目标:将jvm启动时的设置为2048M的内存分配调整为1024M
具体实现脚本代码如下:
#!/bin/bash #表示指定了shell脚本解释器的路径
baseDir="/home/q/www" #定义了一个目录变量
function replace() { #定义了一个方法,在下面的for 循环中调用
filename=$1 #接收第一个参数
from=$2 #接收第二个参数
to=$3 #接收第三个参数
echo 'Processing '$filename #输出正在处理的文件名
sudo sed -i "s/$from/$to/g" $filename #真正替换的命令sed
c=`grep "$from" $filename | wc -l` #统计替换后的次数
if [[ $c != 0 ]]; then #如果统计后的次数不为0就表示替换失败
echo "replace $filename failed!" #输出替换失败的提示语
exit 1 #退出
fi #if判断的结束
echo "Replace $filename success!" #输出替换成功的提示语
}
for this in `find $baseDir -type f -name startenv.sh`; #找到指定目录下的文件名为startenv.sh 的文件,并执行for 循环
do #开始执行
# 替换jdk配置
echo $this #输出找到的文件名及全路径
replace $this "2048" "1024" #调用上面定义的方法去执行替换操作
done #结束执行
以上 Shell 脚本中每行都有清晰的注释说明,建议仔细阅读。
欢迎关注【无量测试之道】公众号,回复【领取资源】
Python+Unittest框架API自动化、
Python+Unittest框架API自动化、
Python+Pytest框架API自动化、
Python+Pandas+Pyecharts大数据分析、
Python+Selenium框架Web的UI自动化、
Python+Appium框架APP的UI自动化、
Python编程学习资源干货、
Vue前端组件化框架开发、
资源和代码 免费送啦~
文章下方有公众号二维码,可直接微信扫一扫关注即可。
备注:我的个人公众号已正式开通,致力于IT互联网技术的分享。
包含:数据分析、大数据、机器学习、测试开发、API接口自动化、测试运维、UI自动化、性能测试、代码检测、编程技术等。
微信搜索公众号:“无量测试之道”,或扫描下方二维码:

添加关注,让我们一起共同成长!
边栏推荐
- 科技云报道:从Robot到Cobot,人机共融正在开创一个时代
- [UTCTF2020]file header
- 今日现货白银操作建议
- 1140_ SiCp learning notes_ Use Newton's method to solve the square root
- Determining the full type of a variable
- pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
- 微信小程序中使用wx.showToast()进行界面交互
- [Stanford Jiwang cs144 project] lab4: tcpconnection
- Why should we understand the trend of spot gold?
- 【VHDL 并行语句执行】
猜你喜欢
随机推荐
Most elements
测试周期被压缩?教你9个方法去应对
IO stream file
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
LeetCode 90:子集 II
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
C语言通信行程卡后台系统
UWB learning 1
gslx680触摸屏驱动源码码分析(gslX680.c)
[UVM foundation] what is transaction
[webrtc] m98 Screen and Window Collection
在线直播系统源码,使用ValueAnimator实现view放大缩小动画效果
KBU1510-ASEMI电源专用15A整流桥KBU1510
《动手学深度学习》(四) -- 卷积神经网络 CNN
nacos
知识点滴 - 关于苹果认证MFI
You Li takes you to talk about C language 6 (common keywords)
Redis technology leak detection and filling (II) - expired deletion strategy
【p2p】本地抓包
1141_ SiCp learning notes_ Functions abstracted as black boxes







![[UTCTF2020]file header](/img/e3/818e2d531a06ab90de189055f634ad.png)

