shell习题171110-取消后缀

新朋友请点上方shell习题171110-取消后缀
 蓝字“阿铭linux”关注

今日shell习题:

至少用两种方法,批量把当前目录下面所有文件名后缀为.bak的后缀去掉,比如1.txt.bak去掉后为1.txt

答案明日公布。

习题171109-重启tomcat==参考答案==

说明:以下脚本为猿课同学实际线上跑的shell脚本,考虑的方面比较多,大家可以学一学他的思路

#!/bin/bash

###功能: 重启 tomcat 进程

###要求:对于tomcat中的某些应用,使用shutdown.sh是无法完全停掉所有服务的 实际操作中都需要kill掉tomcat再重启

##

### root can not run this script.

##

if [ $USER = root ]

then

        echo "root cann"t run this script!please run with other user!"

        exit 1

fi

##

### check the Parameter

##

if [[ $# -ne 1 ]]

then

        echo "Usage:$0 tomcatname"

        exit 1

fi

##

### only one process can run one time

##

TMP_FILE_U=/tmp/.tmp.ps.keyword.$USER.956327.txt

#echo $TMP_FILE_U

KEYWORD1="$0"

KEYWORD2="$1"

# 使用赋值会多fork出一个进程,所以要先重定向到一个文本,再统计.

ps ux |grep "$KEYWORD1"|grep "\<$KEYWORD2\>"|grep -v "grep" > $TMP_FILE_U

Pro_count=`cat $TMP_FILE_U |wc -l`

if [ $Pro_count -gt 1 ]

then

        echo "An other process already running ,exit now!"

        exit 1

fi

###################################################

#                                                 #

#               begin of the script               #

#                                                 #

###################################################

##

### set the Parameter

##

TOM=`echo $1|sed "s#/##g"`

TOMCAT_DIRECTORY=~/usr/local/$TOM

STARTUP_SCRIPT=$TOMCAT_DIRECTORY/bin/startup.sh

TOMCAT_LOG=$TOMCAT_DIRECTORY/logs/catalina.out

CONF_FILE=$TOMCAT_DIRECTORY/conf/server.xml

TEMPFILE=/tmp/.tmpfile.x.89342.c4r3.tmp

##

### check if the tomcat directory exist

##

if [ ! -d "$TOMCAT_DIRECTORY" ]

then

        echo "the tomcat \"$TOM\" not exist.check again!"

        exit 1

fi

##

### log roteta and delete log one week ago

##

rotate_log(){

TIME_FORMART=$(date +%Y%m%d%H%M%S)

LOG_DIR=$(dirname $TOMCAT_LOG)

mv $TOMCAT_LOG ${TOMCAT_LOG}_${TIME_FORMART}

find $LOG_DIR -type f -ctime +7 -exec rm -rf {} \;

}

##

### function start the tomcat

##

start_tomcat()

{

#echo start-tomcat-func

if [ -x  "$STARTUP_SCRIPT" ]

then

        

        rotate_log

        $STARTUP_SCRIPT

        sleep 1

        tail -f $TOMCAT_LOG

else

        if [ -e $STARTUP_SCRIPT ]

        then

                chmod +x $STARTUP_SCRIPT

#               echo "permition added!"

                if [ -x  "$STARTUP_SCRIPT" ]

                then

                        

                        rotate_log

                        $STARTUP_SCRIPT

                        sleep 1

                        tail -f $TOMCAT_LOG

                else

                        echo "The script not have excute permision,Couldn"t add permision to Script!"

                        exit 1

                fi

        else

                echo "error,the script \"startup.sh\" not exist!"

                exit 1

        fi

fi

}

##

### function stop the tomcat

##

stop_tomcat()

{

rm -rf $TEMPFILE

ps ux |grep /$TOM/ |grep -v "grep /$TOM/"|grep java > $TEMPFILE

Pro_Count=`cat $TEMPFILE|wc -l`

PIDS=`cat $TEMPFILE|awk "{print $2}"`

rm -rf $TEMPFILE

#echo $Pro_Count

if [ $Pro_Count -eq 0 ]

then

        echo "The tomcat not running now!"

else

        if [ $Pro_Count -ne 1 ]

        then

                echo "The have $Pro_Count process running,killed!"

                kill -9 `echo $PIDS`

                WC=`ps aux | grep "/$TOM/" | grep -v "grep /$TOM/" | grep java |wc -l`

                [ $WC -ne 0 ] && (echo "kill process failed!";exit 1)

        else

                echo "Process killed!"

                kill -9 `echo $PIDS`

                WC=`ps aux | grep "/$TOM/" | grep -v "grep /$TOM/" | grep java |wc -l`

                [ $WC -ne 0 ] && (echo "kill process failed!";exit 1)

        fi

fi

}

###########################

####                   ####

####  The main script  ####

####                   ####

###########################

echo -e "are you sure restart $TOM?(y or n)"

read ANS

if [ "$ANS"a != ya ]

then

   echo -e "bye! \n"

   exit 1

fi

stop_tomcat

echo "start tomcat ..."

sleep 2

start_tomcat

# end

- END -

出品 | 阿铭linux

shell习题171110-取消后缀

加私人微信:81677956  获取免费学习资料

shell习题171110-取消后缀

提升自己,才是世界上最稳健的投资

shell习题171110-取消后缀