2014-09-05 162 views
2

我想將這個Shell腳本轉換成Ruby腳本。 我已經完成了Rubular Regex,但是我不知道如何將reg ex隱含到ruby腳本中。Shell腳本>紅寶石

Shell腳本:

#!/bin/bash 
file="./NA_Hosts.txt" 
      if [ ! -e $file ];then 
          sed -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/.ipss.test.net. 60 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
          -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
          -e 's/.ipss.test.net. 60 IN A /,/g' /tmp/testing/dig_whole_ipss.txt > ./NA_Hosts.txt 
       echo ${txtgrn}Script executed successfully.${txtrst} 
       cat ./NA_Hosts.txt >> ./NA_Temp.txt 
          exit 
         else 
       read -r -p "${txtred}ATTENTION!${txtrst} Generating this file will replace the old one. Do you wish to continue? [y/N] " response 
       if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]];then 
        sed -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/.ipss.test.net. 60 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/*.ipss.test.net. 60 IN A /,/g' \ 
           -e 's/.ipss.test.net. 3600 IN A /,/g' \ 
           -e 's/.ipss.test.net. 60 IN A /,/g' /tmp/testing/dig_whole_ipss.txt > ./NA_Hosts.txt 
        changecount=`cat NA_Temp.txt NA_Hosts.txt | sort | uniq -u | wc -l` 
           echo "There have been made ${txtred}$changecount${txtrst} total changes." 
           echo ${txtgrn}NA-Hosts file has been successfully updated! ${txtrst} 
        cat NA_Hosts.txt|grep ""s-vd"" >> dslamlist.txt 
        cat NA_Hosts.txt|grep ""s-vh"" >> dslamlist.txt 
        cat ./NA_Hosts.txt > ./NA_Temp.txt 
        if [[ "$changecount">"$sendmailtreshold" ]];then 
         mail -s "Notification: NA-Host File updated!" [email protected]<<-MYTEXT 
         Node DB | NA-Host File 
         The NA-Host File has been successfully updated. 
         There has been made $changecount changes. 
         You're receiving this notification because you are a on the list of receivers. 
         *** This is an automatically generated email, please do not reply *** 
         MYTEXT 
        fi 
       else 
           echo ${txtred}Script aborted.${txtrst} 
          exit 
        fi 

shell腳本工作正常。

Rubular REG EX:

(ipc-\D{3,4}\d{3}-s-v.-\d{2})\.ipss.test.net\.\s+\d+\s+IN\s+A\s+(\d+\.\d+\.\d+\.\d+) 

的Ruby腳本的目標應該是讓這種格式:

ipc-zuc650-s-vd-04.ipss.test.net. 3600 IN A 10.150.90.110 

進入這個:

ipc-zuc650-s-vd-04,10.150.90.110 
+0

找到我讀的標題*外殼腳本優於紅寶石*,這是一個有爭議點。 .. – zetetic 2014-09-05 21:25:48

回答

1

這裏的其中一個辦法你在做什麼:

regex = /(ipc-\D{3,4}\d{3}-s-v.-\d{2})\.ipss.test.net\.\s+\d+\s+IN\s+A\s+(\d+\.\d+\.\d+\.\d+)/ 
string = "ipc-zuc650-s-vd-04.ipss.test.net. 3600 IN A 10.150.90.110" 
string.scan(regex).map{ |host,ip| "#{host},#{ip}" } 

這導致陣列如下:["ipc-zuc650-s-vd-04,10.150.90.110"]絃樂#掃描

文檔可以在http://www.ruby-doc.org/core-2.1.2/String.html#method-i-scan