shell代码中各种判断方式

AI 摘要

Shell脚本:自动重启FFmpeg进程并备份配置文件 | 快速解决FFmpeg崩溃问题,点击了解详细代码和实施步骤!

`if [ -n "$line" ]; then //判断字符串是否为空,一定要有双引号` `if [ "$PIDS" != "" ]; then//判断字符串是否为空,一定要有双引号` `if [ -f $file ]; then // 判断是否是目录` `newfile="${file//查询的字符串/替换的字符串}” //字符串替换` `if [[ -f $sourceFile ]] && [[ ${sourceFile##*.} == "mp4" ]] ; then //判断扩展名`

#判断是否需要重启ffmpeg进程
path=/home/media/fileloop/restart.txt
backup=/home/media/fileloop/p_txt_backup

for line in $(cat $path | sed -e 's/\r//g')
do
        if [ -n "$line" ]; then
                echo $(date -d "today" +"%Y%m%d_%H%M")_$line
                echo "ps axu  | grep ffmpeg | grep \"/data/media/fileloop/$line/p.txt\" | grep -v grep | awk '{print \$2}'"
                pid=`ps axu  | grep ffmpeg | grep "/data/media/fileloop/$line/p.txt" | grep -v grep | awk '{print $2}'`
                if [ -n "$pid" ]; then
                        echo "kill -9 $pid"
                        kill -9 $pid
                        cp /home/media/fileloop/$line/p.txt  $backup/${line}_$(date -d "today" +"%Y%m%d_%H%M%S").txt
                else
                        echo "can not grep  \"/data/media/fileloop/$line/p.txt\" in processlist"
                fi
        fi
done

#cp $path /root/osstools/restart_backup/
cp /dev/null $path