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

你可能感兴趣的:(shell案例集合)