当前位置:网站首页>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自动化、性能测试、代码检测、编程技术等。
微信搜索公众号:“无量测试之道”,或扫描下方二维码:
添加关注,让我们一起共同成长!
边栏推荐
- Why is the row of SQL_ The ranking returned by number is 1
- 直播平台源码,可折叠式菜单栏
- misc ez_ usb
- 3D reconstruction - stereo correction
- 通信设备商,到底有哪些岗位?
- Tencent's one-day life
- Solve could not find or load the QT platform plugin "xcb" in "
- Live broadcast platform source code, foldable menu bar
- A bit of knowledge - about Apple Certified MFI
- 开源生态|打造活力开源社区,共建开源新生态!
猜你喜欢
Jenkins远程构建项目超时的问题
Use and analysis of dot function in numpy
为什么要了解现货黄金走势?
[SUCTF 2019]Game
Common method signatures and meanings of Iterable, collection and list
php导出百万数据
面试结束后,被面试官在朋友圈吐槽了......
2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
自定义类加载器加载网络Class
【Unity】物体做圆周运动的几个思路
随机推荐
Leetcode-206. Reverse Linked List
JS get all date or time stamps between two time stamps
Tencent's one-day life
Ansible
开源生态|打造活力开源社区,共建开源新生态!
@component(““)
面试结束后,被面试官在朋友圈吐槽了......
What are the positions of communication equipment manufacturers?
2022焊工(初级)判断题及在线模拟考试
Pytest+allure+jenkins environment -- completion of pit filling
KBU1510-ASEMI电源专用15A整流桥KBU1510
Hands on deep learning (IV) -- convolutional neural network CNN
Wx is used in wechat applet Showtoast() for interface interaction
php导出百万数据
Operation suggestions for today's spot Silver
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Idea add class annotation template and method template
2022年茶艺师(中级)考试试题及模拟考试
buuctf misc USB
LeetCode 90:子集 II