shell脚本实例汇总

 

#检查程序是否安装,若未安装则进行安装,注意:交互式命令不能和其放在一起

复制代码
# 检查 expect 是否已安装
if ! command -v expect &> /dev/null; then
    echo "expect is not installed, attempting to install..."
    # 使用 yum 安装 expect
    sudo yum install expect -y
    if [ $? -eq 0 ]; then
        echo "expect installed successfully."
    else
        echo "Failed to install expect. Please check your package manager and try again."
        exit 1
    fi
else
    echo "expect is already installed."
fi
复制代码

交互式命令

#!/usr/bin/expect -f
#从远程机器拷贝到本机
set timeout
-1 spawn scp root@172.16.4.234:/opt/autoTest_Tra.tar.gz ./. expect "password:" send "passwd\r" expect eof

 

获取一段时间内cpu内存占用情况,并记录到文档中

复制代码
dir_current=$(pwd)
file_current=${dir_current}/statistics.log

if [ -f ${file_current} ];then
rm -rf statistics.log && touch statistics.log
else
touch statistics.log
fi

item_cpu () {
    cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}'|cut -f 1 -d "."`
    cpu_use=`expr 100 - $cpu_idle`
    # echo "$now cpu_use: $cpu_use" >> $file_dir
    return $cpu_use
}

read -p "please input your the number of loop you want to run : " loop_num
loop_now=1
total=0
while [ $loop_now -lt $(($loop_num+1)) ]
do 
echo $loop_now
item_cpu 
cpuUsage=$(echo $?)
total=`expr $total + $cpuUsage`
echo "$loop_now $now cpu_use: $cpuUsage" >> $file_current
sleep 5
loop_now=$(($loop_now+1))

done
echo "averageVal=$((total/loop_num))"
echo "averageVal=$((total/loop_num))" >> $file_current
复制代码

 

模糊查找文件是否存在,存在则解压

复制代码
#!/bin/bash
current_path=$(pwd)
part_of_DemuxerName="streamdemuxer"
target_Demuxer=$(find . -maxdepth 1 -name "${part_of_DemuxerName}*.tar.gz")
target_Demuxer=$current_path/$target_Demuxer
if [ -f "$target_Demuxer" ]; then
    tar -xvf $target_Demuxer
    rm -rf $target_Demuxer
else
    echo "no find $part_of_DemuxerName"
fi

part_of_ServerName="streamingserver"
target_streamingserver=$current_path/$target_streamingserver
target_streamingserver=$(find . -maxdepth 1 -name "${part_of_ServerName}*.tar.gz")
if [ -f "$target_streamingserver" ]; then
    tar -xvf $target_streamingserver
    rm -rf $target_streamingserver
else
    echo "no find $part_of_ServerName"
fi
复制代码

 

重启进程

复制代码
processname1='mpm_ep'  #第一个程序名称
PROCESS1=$(ps -ef|grep $processname1|grep -v grep|grep -v PPID|awk '{print $1}')      #过滤出进程ID
kill -9 $PROCESS1    #杀掉进程
echo "Kill the $PROCESS1 "
processname2='MediaEngineServer'
PROCESS2=$(ps -ef|grep $processname2|grep -v grep|grep -v PPID|awk '{print $1}')
kill -9 $PROCESS2
echo "Kill the $PROCESS2 "
sleep 3
/lib/modules/exec/mpm_ep &
echo 'EXE mpm_ep SUCCESS'
sleep 5
/lib/modules/exec/MediaEngineServer &
echo 'EXE MediaEngineServer SUCCESS'
复制代码

 

指定节奏ping,并记录ping结果

复制代码
#!/bin/sh
#hilinux 只能用#!/bin/sh,可能是内核不支持bash
dir_current=$(cd `dirname $0`; pwd)
file_current=${dir_current}/log.log
loop_now=1

if [ -f ${file_current} ];then
rm -rf log.log && touch log.log
else
touch log.log
fi

read -p "please input your the number of loop you want to run : " loop_num
while [ $loop_now -lt $loop_num ]
do #hilinux用不了for循环,搞不懂为啥,所以才用while循环
echo $loop_now
echo ===========$loop_now========== >>$file_current
date >> $file_current
echo "   " >>$file_current
ping -c 1 114.84.70.54 >> $file_current    #每次只发一次ping包
#ping -c 1 10.10.10.166 >> $file_current

if [ $? -eq 1 ];then
echo 'fail' >> $file_current
echo 'fail'
else
echo 'pass' >> $file_current
echo 'pass'
fi

sleep 60
loop_now=$(($loop_now+1))

done
复制代码

 

自动实现生成证书

复制代码
#!/bin/bash
dir_current=$(cd `dirname $0`; pwd)

rz

if [ -f ${dir_current}/ysm_soft.cer ];then
rm -rf ysm_soft.cer
fi

export LD_LIBRARY_PATH=.
./kdssl x509 -req -extfile ca.cnf -extensions v3_sign -in sign.req -CA ca.cer -CAkey ca.key -days 3600 -CAcreateserial -out ysm_soft.cer -sm3

if [ -f ${dir_current}/sign.req ];then
rm -rf sign.req 
fi

sz ysm_soft.cer

if [ -f ${dir_current}/ysm_soft.cer ];then
rm -rf ysm_soft.cer
fi
复制代码

 在一个文本文件中查询一个字符串

pro_port=`grep -rn "port=" ./case.conf`  #文本文件中符合的都输出,且带上行数,如,22:bport=22 28:port=8081
pro_port=${pro_port##*=} #最短取值法,等号及等号左边全去掉。
echo $pro_port

 

posted @   seven1986  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示