shell案例集合

1. 批量修改文件名

脚本:

#!/bin/bash
for f in `ls | grep love`
do
   mv $f `echo ${f%love*}.sh`
done

命令:

[root@localhost tmp]# ll
总用量 0
-rw-r--r--. 1 root root 0 1月  18 18:37 alove.sh
-rw-r--r--. 1 root root 0 1月  18 18:37 blove.sh
-rw-r--r--. 1 root root 0 1月  18 18:37 clove.sh
-rw-r--r--. 1 root root 0 1月  18 18:37 dlove.sh

结果:

[root@localhost tmp]# ll
总用量 0
-rw-r--r--. 1 root root 0 1月  18 18:37 a.sh
-rw-r--r--. 1 root root 0 1月  18 18:37 b.sh
-rw-r--r--. 1 root root 0 1月  18 18:37 c.sh
-rw-r--r--. 1 root root 0 1月  18 18:37 d.sh

2. 获取进程pid(不适用centos7)

脚本:

#!/bin/bash
echo "请输入要查询的端口号:"
read port
content=`netstat -anp | grep $port | awk '{print $7}'`
pid=${content%/java}
#if [ -z $pid ] 也可以这种方式
if ((pid == 0))
then
    echo "该进程没有启动"
else
    echo "进程pid:$pid"
fi

命令:

[root@localhost bin]# ./test.sh 
请输入要查询的端口号:
8080
进程pid:4312

3. Jenkins执行后脚本

#!/bin/bash
#define tomcat home
tomcat_home="/hfhy/tomcat/apache-tomcat-8.5.23"

#find tomcat-id and kill it;
tomcat_pid=`ps -ef | grep tomcat | grep -vE '(grep|jenkins)' | awk '{print $2}'`
if [[ $tomcat_pid != "" ]]
then
   echo "tomcat_id is:" $tomcat_pid
   kill -9 $tomcat_pid
   echo "tomcat is killed"      
else
   echo "tomcat is not started"
fi

#remove the old program
rm -rf /hfhy/tomcat/apache-tomcat-8.5.23/webapps/app
rm -rf /hfhy/tomcat/apache-tomcat-8.5.23/webapps/app.war
cp /hfhy/jenkins/home/workspace/app/gotosea-app/target/gotosea-app-0.0.1-SNAPSHOT.war /hfhy/tomcat/apache-tomcat-8.5.23/webapps/app.war

#start the tomcat
$tomcat_home/bin/startup.sh
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,499评论 9 467
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,203评论 2 33
  • 个人学习批处理的初衷来源于实际工作;在某个迭代版本有个BS(安卓手游模拟器)大需求,从而在测试过程中就重复涉及到...
    Luckykailiu阅读 4,760评论 0 11
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,935评论 6 342