iT邦幫忙

0

sun unix foreach迴圈問題

  • 分享至 

  • xImage

請問一下 ,如果我想使用2個foreach迴圈程式如下 ,不過執行後沒有秀出我要的結果 ,可以請教高手我的程式是哪裡有問題嗎?
P.S這是公司老板的需求 ,所以有些東西無法上傳 ,所以有不清楚的地方請多原諒!!

if ( `wc -l /tmp/fatal_error_record | nawk '{print $1}'` == 0 ) then
        exit 0
else
        grep "ERROR:" /tmp/fatal_error_record | grep -v "linked to" | nawk '{print $2}'> /tmp/error_code <---這個檔案有成功裡面有內容
        grep "WARNING:" /tmp/fatal_error_record | grep -v "linked to" | nawk '{print $3}'> /tmp/warning_code <---這個檔案沒有成功
        foreach ERROR_CODE ( `cat /tmp/error_code` )
        foreach WARNING_CODE ( `cat /tmp/warning_code` )
                if ( `echo $ERROR_CODE | cut -c1-2` == "IL" ) then
                        /usr/local/bin/say -b -s "Alarm\! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Inform PE HOLD ALL PROD First and RWK+before one" &
                else if ( `echo $ERROR_CODE | cut -c1-2` == "RB" ) then
                        /usr/local/bin/say -b -s "Alarm\! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Rework Last One Batch" &
                else if ( `echo $ERROR_CODE | cut -c1-2` == "IP" ) then
                        /usr/local/bin/say -b -s "Alarm\! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Rework Last One pcs wafer" &
                else if ( `echo $ERROR_CODE | cut -c1-2` == "RT" ) then
                        /usr/local/bin/say -b -s "Alarm\! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Rework Last One pcs wafer" &
                else if ( `echo $WARNING_CODE | cut -c1-2` == "TS" ) then
                        /usr/local/bin/say -b -s "Alarm\! Call PE 8238: REMA SHIFT $WARNING_CODE InterLock OPEN, please Inform 8238 Check" &
                endif
        end
        set mn=`grep $ID /usr/asm/data.0000/mc_tool/Tool_ID_EQ1_EQ3 | awk -F" " '{print $3}'`
                set tgroup=`grep $ID /usr/asm/data.0000/mc_tool/Tool_ID_EQ1_EQ3 | awk -F" " '{print $6}'`
                if ( $tgroup == "EQ1" ) then
                        foreach EE (`cat /usr/asm/data.0000/mc_tool/NameList | awk -F" " '{print $1}'`)
                        mailx -s "$ID Fatal Error Occured, please Carefully Attention" $EE@vis.com.tw < /tmp/fatal_error_record
                        end
                endif
endif
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2016-12-17 19:37:40

沒事
我只是把題目弄得好看一點給高手看

if ( wc -l /tmp/fatal_error_record | nawk '{print $1}' == 0 ) then
   exit 0
else
   grep "ERROR:" /tmp/fatal_error_record | grep -v "linked to" | nawk '{print $2}'> /tmp/error_code <---這個檔案有成功裡面有內容
   grep "WARNING:" /tmp/fatal_error_record | grep -v "linked to" | nawk '{print $3}'> /tmp/warning_code <---這個檔案沒有成功
   foreach ERROR_CODE ( cat /tmp/error_code )
      foreach WARNING_CODE ( cat /tmp/warning_code )
         if ( echo $ERROR_CODE | cut -c1-2 == "IL" ) then
            /usr/local/bin/say -b -s "Alarm! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Inform PE HOLD ALL PROD First and RWK+before one" &
         else if ( echo $ERROR_CODE | cut -c1-2 == "RB" ) then
            /usr/local/bin/say -b -s "Alarm! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Rework Last One Batch" &
         else if ( echo $ERROR_CODE | cut -c1-2 == "IP" ) then
            /usr/local/bin/say -b -s "Alarm! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Rework Last One pcs wafer" &
         else if ( echo $ERROR_CODE | cut -c1-2 == "RT" ) then
            /usr/local/bin/say -b -s "Alarm! Call On Duty PE 8238: FATAL ERROR $ERROR_CODE Occured, please Rework Last One pcs wafer" &
         else if ( echo $WARNING_CODE | cut -c1-2 == "TS" ) then
            /usr/local/bin/say -b -s "Alarm! Call PE 8238: REMA SHIFT $WARNING_CODE InterLock OPEN, please Inform 8238 Check" &
         endif
      end
      set mn=grep $ID /usr/asm/data.0000/mc_tool/Tool_ID_EQ1_EQ3 | awk -F" " '{print $3}'
      set tgroup=grep $ID /usr/asm/data.0000/mc_tool/Tool_ID_EQ1_EQ3 | awk -F" " '{print $6}'
      if ( $tgroup == "EQ1" ) then
         foreach EE (cat /usr/asm/data.0000/mc_tool/NameList | awk -F" " '{print $1}')
            mailx -s "$ID Fatal Error Occured, please Carefully Attention" $EE@vis.com.tw < /tmp/fatal_error_record
         end
      endif
endif

我沒有 Sun Unix 可測試
不過光把 source code 排隊排好
好像 外面那層的foreach 沒有對應的 end

weiclin iT邦高手 4 級 ‧ 2016-12-17 22:00:21 檢舉

有三個 foreach, 可是只有兩個 end /images/emoticon/emoticon06.gif

另外
我試了一下 csh 的 foreach
(如果你是用 csh 的話)
原本的寫法

foreach ERROR_CODE ( cat /tmp/error_code )
   foreach WARNING_CODE ( cat /tmp/warning_code )

應該改成

foreach ERROR_CODE ( `cat /tmp/error_code` )
   foreach WARNING_CODE ( `cat /tmp/warning_code` )

才比較符合你想達成的效果

csh foreach 參考資料

我要發表回答

立即登入回答