shell script if嵌套for循环脚本

通过一个脚本介绍shell script编写的三个注意事项

文章目录

  • 脚本内容
  • 脚本关键点讲解

脚本内容

输出设备告警并将其清除

bash-4.3$ cat clear_alarm.sh
#!/bin/bash
j=`/opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management active-notifications"|grep "remoteNFinstance=fake_smf_nfid"`
i=`/opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management active-notifications"|grep "remoteNFinstance=ismf-notify"`
if [[ $j ]];then
        for k in `/opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management active-notifications"| grep -B5 "remoteNFinstance=fake_smf_nfid" | grep fault-id | awk -F " " '{print $2}'`:
        do
                /opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management active-notifications" >>alarm.log
                /opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management clear-notification fault-id $k" >>alarm.log
        done
elif [[ $i ]];then
        for g in `/opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management active-notifications"| grep -B5 "remoteNFinstance=ismf-notify" | grep fault-id | awk -F " " '{print $2}'`:
        do
                /opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management active-notifications" >>alarm.log
                /opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management clear-notification fault-id $g" >>alarm.log
        done
else
        echo `date`" no connetion alarm" >>alarm.txt
fi

脚本关键点讲解

  • if判断
j=`/opt/services/epg/bin/ttx/bin/epg_pdc_run_command "epg node fault-management active-notifications"|grep "remoteNFinstance=fake_smf_nfid"`  //命令输出处理后负值给变量
if [[ $j ]]  //if判断上面命令是否有匹配结果,如果有匹配结果为ture,否则为false
  • if循环嵌套语法
if [command];then
   for:
      do
      "action command"
      done
elif
   for:
      do
      "action command"      
      done
else
      "action command"
fi
   
  • Linux命令和字符传混合输出
echo `date`" no connetion alarm"    //命令要放到``里

你可能感兴趣的:(linux,linux,shell,脚本,循环嵌套)