当前位置:网站首页>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自动化、性能测试、代码检测、编程技术等。
微信搜索公众号:“无量测试之道”,或扫描下方二维码:

添加关注,让我们一起共同成长!
边栏推荐
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- [guess-ctf2019] fake compressed packets
- IO流 file
- Solve could not find or load the QT platform plugin "xcb" in "
- [UTCTF2020]file header
- buuctf misc USB
- Gslx680 touch screen driver source code analysis (gslx680. C)
- Jenkins remote build project timeout problem
- 智联+影音,AITO问界M7想干翻的不止理想One
- 通信设备商,到底有哪些岗位?
猜你喜欢
随机推荐
SQL优化的魅力!从 30248s 到 0.001s
[2022 ACTF]web题目复现
微信小程序中使用wx.showToast()进行界面交互
2022年茶艺师(中级)考试试题及模拟考试
Explore Cassandra's decentralized distributed architecture
Why is the row of SQL_ The ranking returned by number is 1
探索Cassandra的去中心化分布式架构
nacos
【webrtc】m98 screen和window采集
numpy中dot函数使用与解析
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
pytest+allure+jenkins環境--填坑完畢
The metauniverse of the platofarm farm continues to expand, with Dao governance as the core
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
JS plot flot application - simple curve
Common method signatures and meanings of Iterable, collection and list
leanote私有云笔记搭建
Figure out the working principle of gpt3
解决问题:Unable to connect to Redis
[advanced digital IC Verification] command query method and common command interpretation of VCs tool








![[CV] Wu Enda machine learning course notes | Chapter 8](/img/c0/7a39355fb3a6cb506f0fbcf2a7aa24.jpg)