腳本正則表達式
『壹』 sh腳本里怎樣用正則表達式
sh腳本里可以調用sed,awk,grep,perl之類的命令來使用正則表達式。
如果是bash 3.0腳本,支持進程內的正則表達式,使用下面的語法:
[[ string =~ regex ]]
if[[abcfoobarbletch=~'foo(bar)bl(.*)']]
then
echoTheregexmatches!
echo$BASH_REMATCH--outputs:foobarbletch
echo${BASH_REMATCH[1]}--outputs:bar
echo${BASH_REMATCH[2]}--outputs:etch
fi
使用這個語法的性能要比生成一個新的進程來運行grep命令優越,因為正則表達式匹配在bash進程內完成。如果正則表達式或者字元串包括空格或者shell 關鍵字,(諸如'*'或者'?'),就需要用引號包裹。
『貳』 如何在Jenkins Ant腳本中使用正則表達式
引用自(圖文並茂):
http://jingyan..com/article/380abd0a6c6e7d1d90192cb4.html
具體步驟:
下載並安裝一個Jenkins伺服器,它是開源的,可以從如下地址了解並下載安裝:
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins
步驟閱讀
2
進入Jenkins伺服器磁碟系統,新建一個文件夾,並在裡面建立兩個文件:
1. CmdOutput.txt 文件,存放測試數據的一個文件;
2. BuildTest.xml 文件,用來執行構建的ant腳本文件。
步驟閱讀
3
打開 CmdOutput.txt 文件,輸入類似如下的測試數據:
RunTest output: Begin to start TestExecute/TestComplete Instance on remote computer ...*
TestExecute.TestExecuteApplication.10
Begin to open TestExecute/TestComplete project suite on remote computer ...*
2
後面的Ant腳本將會先載入讀取這個文件的內容,然後把最後一行的返回代碼「2」提取出來.
步驟閱讀
4
打開 BuildTest.xml 文件,輸入如下的Ant腳本。裡面將會使用propertyregex來提取返回代碼,其中property用來指定提取到的值存放在什麼屬性裡面,input用來指定匹配來源,regexp用來指定正則表達式,select用來指定選擇哪個匹配子項,casesensitive指定是否區分大小寫。:
<project name="hello" basedir="." default="Test" xmlns:props="antlib:org.apache.ant.props">
<property environment="JenkinsEnv"/>
<!--
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
<typedef uri="antlib:org.apache.ant.props" resource="org/apache/ant/props/antlib.xml" classpath="${JenkinsEnv.ANT_HOME}\\ant-props-1.0Alpha.jar" />
<propertyhelper>
<props:nested/>
</propertyhelper>
<!-- *********************************************** -->
<target name="GetReturnCode">
<loadfile property="CmdOutput" srcFile="CmdOutput.txt"/>
<echo message="CmdOutput: ${CmdOutput}" />
<propertyregex property="ResultCode"
input="${CmdOutput}"
regexp="^([\s\S]*)(\r?\n)+(\d{1,})(\r?\n)*"
select="\3"
casesensitive="false" />
<echo message="ResultCode: ${ResultCode}" />
</target>
</project>
步驟閱讀
5
在Jenkins伺服器上建立一個TEST的Job,然後點擊配置按鈕:
步驟閱讀
6
在構建的標簽處,添加一個Invoke Ant,然後分別輸入要構建的Ant腳本文件,和要跑的任務Target:
步驟閱讀
7
點擊保存按鈕保存上一步做的配置,然後點擊Build Now按鈕開始構建Job:
步驟閱讀
8
構建完畢之後,把滑鼠放到構建記錄上面,點擊下拉箭頭,彈出菜單中選擇「Console Output」查看控制台輸出:
步驟閱讀
9
結果頁面中,前面 CmdOutput.txt 文件中的返回代碼「2」被提取並顯示出來了:
步驟閱讀
END
『叄』 腳本正則表達式
-v host=$(hostname) 是變數設置,將shell中的變數值【$(hostname)】 賦值給 awk中的變數【host】
後面單引號中的 / / 之間的內容【/^$/ 、/Linux/、/pgpgin/】都是正則表達式
>後面的是路徑和文件【輸出位置】
『肆』 如何在Jenkins Ant腳本中使用正則表達式
方法/步驟
1
下載並安裝一個Jenkins伺服器,它是開源的,可以從如下地址了解並下載安裝(如圖):
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins
2
進入Jenkins伺服器磁碟系統,新建一個文件夾,並在裡面建立兩個文件(如圖):
1. CmdOutput.txt 文件,存放測試數據的一個文件;
2. BuildTest.xml 文件,用來執行構建的ant腳本文件。
3
打開 CmdOutput.txt 文件,輸入類似如下的測試數據(如圖):
RunTest output: Begin to start TestExecute/TestComplete Instance on remote computer ...*
TestExecute.TestExecuteApplication.10
Begin to open TestExecute/TestComplete project suite on remote computer ...*
2
後面的Ant腳本將會先載入讀取這個文件的內容,然後把最後一行的返回代碼「2」提取出來.
4
打開 BuildTest.xml 文件,輸入如下的Ant腳本(如圖)。裡面將會使用propertyregex來提取返回代碼,其中property用來指定提取到的值存放在什麼屬性裡面,input用來指定匹配來源,regexp用來指定正則表達式,select用來指定選擇哪個匹配子項,casesensitive指定是否區分大小寫。:
<project name="hello" basedir="." default="Test" xmlns:props="antlib:org.apache.ant.props">
<property environment="JenkinsEnv"/>
<!--
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
<typedef uri="antlib:org.apache.ant.props"
resource="org/apache/ant/props/antlib.xml"
classpath="${JenkinsEnv.ANT_HOME}\\ant-props-1.0Alpha.jar" />
<propertyhelper>
<props:nested/>
</propertyhelper>
<!-- *********************************************** -->
<target name="GetReturnCode">
<loadfile property="CmdOutput" srcFile="CmdOutput.txt"/>
<echo message="CmdOutput: ${CmdOutput}" />
<propertyregex property="ResultCode"
input="${CmdOutput}"
regexp="^([\s\S]*)(\r?\n)+(\d{1,})(\r?\n)*"
select="\3"
casesensitive="false" />
<echo message="ResultCode: ${ResultCode}" />
</target>
</project>
5
在Jenkins伺服器上建立一個TEST的Job,然後點擊配置按鈕(如圖):
6
在構建的標簽處,添加一個Invoke Ant,然後分別輸入要構建的Ant腳本文件,和要跑的任務Target(如圖):
7
點擊保存按鈕保存上一步做的配置,然後點擊Build Now按鈕開始構建Job(如圖):
8
構建完畢之後,把滑鼠放到構建記錄上面,點擊下拉箭頭,彈出菜單中選擇「Console Output」查看控制台輸出(如圖):
9
結果頁面中,前面 CmdOutput.txt 文件中的返回代碼「2」被提取並顯示出來了(如圖):
http://jingyan..com/article/380abd0a6c6e7d1d90192cb4.html
『伍』 正則表達式和shell腳本的關系
簡而言之:
shell腳本中往往涉及大量字元串處理工作,而正則表達式就是為了幫助分析字元串格式是否匹配你的要求;正則替換可以實現替換字元串中符合正則要求的部分,使用起來非常靈活。
可以說,掌握了正則表達式,你的shell腳本水平就上了一個台階。
『陸』 shell腳本和正則表達式有什麼不同
不一樣的東東。shell腳本是為shell編寫的自動執行命令的集合,是一種編程語言。而正則表達式則是用於文本搜索匹配的一種非常強大的字元串查找工具。兩者不是一樣的,shell腳本中可以使用正則表達式,Javascript腳本也可以使用正則表達式,Php,asp,C,C++和C#,Java等編程語言也都可以使用正則表達式。
『柒』 如何在Jenkins Ant腳本中使用正則表達式
方法/步驟
1
下載並安裝一個Jenkins伺服器,它是開源的,可以從如下地址了解並下載安裝。
2
進入Jenkins伺服器磁碟系統,新建一個文件夾,並在裡面建立兩個文件
1) CmdOutput.txt 文件,存放測試數據的一個文件;
2) BuildTest.xml 文件,用來執行構建的ant腳本文件。
3
打開 CmdOutput.txt 文件,輸入類似如下的測試數據
RunTest output: Begin to start TestExecute/TestComplete Instance on remote computer ...*
TestExecute.TestExecuteApplication.10
Begin to open TestExecute/TestComplete project suite on remote computer ...*
2
後面的Ant腳本將會先載入讀取這個文件的內容,然後把最後一行的返回代碼「2」提取出來.
4
打開 BuildTest.xml 文件,輸入如下的Ant腳本。裡面將會使用propertyregex來提取返回代碼,其中property用來指定提取到的值存放在什麼屬性裡面,input用來指定匹配來源,regexp用來指定正則表達式,select用來指定選擇哪個匹配子項,casesensitive指定是否區分大小寫。:
<project name="hello" basedir="." default="Test" xmlns:props="antlib:org.apache.ant.props">
<property environment="JenkinsEnv"/>
<!--
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
<typedef uri="antlib:org.apache.ant.props"
resource="org/apache/ant/props/antlib.xml"
classpath="${JenkinsEnv.ANT_HOME}\\ant-props-1.0Alpha.jar" />
<propertyhelper>
<props:nested/>
</propertyhelper>
<!-- *********************************************** -->
<target name="GetReturnCode">
<loadfile property="CmdOutput" srcFile="CmdOutput.txt"/>
<echo message="CmdOutput: ${CmdOutput}" />
<propertyregex property="ResultCode"
input="${CmdOutput}"
regexp="^([\s\S]*)(\r?\n)+(\d{1,})(\r?\n)*"
select="\3"
casesensitive="false" />
<echo message="ResultCode: ${ResultCode}" />
</target>
</project>
5
在Jenkins伺服器上建立一個TEST的Job,然後點擊配置按鈕:
6
在構建的標簽處,添加一個Invoke Ant,然後分別輸入要構建的Ant腳本文件,和要跑的任務Target:
7
點擊保存按鈕保存上一步做的配置,然後點擊Build Now按鈕開始構建Job:
8
構建完畢之後,把滑鼠放到構建記錄上面,點擊下拉箭頭,彈出菜單中選擇「Console Output」查看控制台輸出:
9
結果頁面中,前面 CmdOutput.txt 文件中的返回代碼「2」被提取並顯示出來了。
『捌』 如何在Jenkins Ant腳本中使用正則表達式
引用自(圖文並茂):
http://jingyan..com/article/380abd0a6c6e7d1d90192cb4.html
具體步驟:
下載並安裝一個Jenkins伺服器,它是開源的,可以從如下地址了解並下載安裝:
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins
進入Jenkins伺服器磁碟系統,新建一個文件夾,並在裡面建立兩個文件:
1. CmdOutput.txt 文件,存放測試數據的一個文件;
2. BuildTest.xml 文件,用來執行構建的ant腳本文件。
打開 CmdOutput.txt 文件,輸入類似如下的測試數據:
RunTest output: Begin to start TestExecute/TestComplete Instance on remote computer ...*
TestExecute.TestExecuteApplication.10
Begin to open TestExecute/TestComplete project suite on remote computer ...*
2
後面的Ant腳本將會先載入讀取這個文件的內容,然後把最後一行的返回代碼「2」提取出來.
打開 BuildTest.xml 文件,輸入如下的Ant腳本。裡面將會使用propertyregex來提取返回代碼,其中property用來指定提取到的值存放在什麼屬性裡面,input用來指定匹配來源,regexp用來指定正則表達式,select用來指定選擇哪個匹配子項,casesensitive指定是否區分大小寫。:
<project name="hello" basedir="." default="Test" xmlns:props="antlib:org.apache.ant.props">
<property environment="JenkinsEnv"/>
<!--
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${JenkinsEnv.ANT_HOME}\\ant-contrib.jar"/>
</classpath>
</taskdef>
<typedef uri="antlib:org.apache.ant.props" resource="org/apache/ant/props/antlib.xml" classpath="${JenkinsEnv.ANT_HOME}\\ant-props-1.0Alpha.jar" />
<propertyhelper>
<props:nested/>
</propertyhelper>
<!-- *********************************************** -->
<target name="GetReturnCode">
<loadfile property="CmdOutput" srcFile="CmdOutput.txt"/>
<echo message="CmdOutput: ${CmdOutput}" />
<propertyregex property="ResultCode"
input="${CmdOutput}"
regexp="^([\s\S]*)(\r?\n)+(\d{1,})(\r?\n)*"
select="\3"
casesensitive="false" />
<echo message="ResultCode: ${ResultCode}" />
</target>
</project>
在Jenkins伺服器上建立一個TEST的Job,然後點擊配置按鈕:
在構建的標簽處,添加一個Invoke Ant,然後分別輸入要構建的Ant腳本文件,和要跑的任務Target:
點擊保存按鈕保存上一步做的配置,然後點擊Build Now按鈕開始構建Job:
構建完畢之後,把滑鼠放到構建記錄上面,點擊下拉箭頭,彈出菜單中選擇「Console Output」查看控制台輸出:
結果頁面中,前面 CmdOutput.txt 文件中的返回代碼「2」被提取並顯示出來了
----------------------------------------
為什麼會有這么多人問這個問題呢?