shell脚本常用命令
生活随笔
收集整理的這篇文章主要介紹了
shell脚本常用命令
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、單引號與雙引號
單引號中完全是保持字符串的原型輸出,而雙引號進行了命令替換。
2、> 和 >>
'>'? 為創建: echo “hello shell”? > out.txt
'>>' 為追加:echo “hello shell”? >> out.txt
3、對于=的問題,如果兩邊沒有空格,表示賦值命令,if [ $var=1 ]表示$var變量賦值成功而不是判斷;
如果=兩邊都有空格表示判斷,if [ $var = 1 ]表示$var變量是否等于1;
# 將a.txt中內容插入b.txt中‘匹配的內容’之后 row=$(grep -n '匹配的內容' b.txt | awk -F ':' '{print $1}') # row=$(expr $row + 1) sed -i "${row} r a.txt" b.txt# 重新導入依賴包,mvn install -Dmaven.test.skip # 執行sleep,是否需要jar包是否存在? # 執行mvn validate# sh echo.sh換成mvn validate,獲取輸出結果 warnContext=`sh echo.sh | grep '[WARNING]'` echo "$warnContext"判斷zk是否啟動
for host in $hosts do#result_array[$i]=`echo "have_started"`zkp_result_array[$i]=`ssh $host "/bin/bash /opt/java_shell/judgezkp_shell"`#echo ${result_array[$i]}let i++ done flag=1 for((j=0;j<3;j++)); doif [ ${zkp_result_array[$j]} != $started ];thenflag=0breakfi donejudgezkp_shell----判斷zk的腳本 #!/bin/bash param=`/opt/jdk1.8.0_192/bin/jps | grep QuorumPeerMain`;if [ -n "$param" ]; then echo "have_started";else echo "not_start";fidenpendencyCheck.sh
#! /bin/bashbasePath=`pwd` # 依賴沖突文件夾 depFolder=denpendencyCheck # 依賴沖突匯總文件 collectCSV=collectCSV.csvfunction check(){echo "[DICTIONARY]在 `pwd` 目錄發現pom文件"if !(flag=$(cat 'pom.xml' | grep 'maven-enforcer-plugin'));then# 將enforcer-template.txt中內容插入pom.xml中‘<plugins>’之后row=$(grep -n '<plugins>' pom.xml | awk -F ':' '{print $1}' | head -n 1)# row=$(expr $row + 1)sed -i "${row} r $basePath\enforcer-template.txt" pom.xmlfi{# mvn validate,獲取輸出結果mvn validate>warnContext.txtstartRow='maven-enforcer-plugin'endRow='DependencyConvergence'#截取文件中需要的部分(sed -n "/$startRow/,/$endRow/p" warnContext.txt | grep -Ev "($startRow|$endRow)") >>$1"/"$2".csv"(sed -n "/convergence/p" warnContext.txt)>temp.txtcat temp.txt | while read linedotemp1=${line%path*} #從右向左截取第一個 src 后的字符串temp2=${temp1#*for} #從左向右截取第一個 / 后的字符串temp3=${temp2%:*}echo "$2,$temp3" >>$1"/"$collectCSVdone} && {rm -rf warnContext.txtrm -rf temp.txt} }# 遞歸遍歷項目根目錄下文件夾 function dfsDir(){if [ -d $1 ];thenfor file in `ls $1`docd $fileif [ -e "pom.xml" ]thencheck $2 $3ficd ..donefi }if [ ! -d "$depFolder" ] thenmkdir "$depFolder" fi#遍歷所有倉庫 for file in `ls $basePath` doif [[ -d $file ]]&&[[ $file != $depFolder ]];thenecho "[PROJECT]正在執行依賴檢查 $file..."touch $basePath"/"$depFolder"/"$file".csv"# 進入項目目錄cd $fileprojPath=`pwd`#如果項目根目錄包含'pom.xml'if [ -e 'pom.xml' ];thencheck $basePath"/"$depFolder $fileelsedfsDir $projPath $basePath"/"$depFolder $filefi# 退出到上一層cd ..fi donemaven-enforcer-plugin的
配置,即enforcer-template.txt
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>3.0.0-M3</version><executions><execution><id>enforce</id><goals><goal>display-info</goal><goal>enforce</goal></goals><configuration><rules><!-- rule 0--><requireJavaVersion><message>You are running an older version of Java. This application requires at least JDK ${java.version}</message><version>1.8.0</version></requireJavaVersion><!-- rule 1--><DependencyConvergence/><!-- rule 2--><bannedDependencies><excludes><exclude>org.apache.maven</exclude><exclude>org.apache.maven:badArtifact</exclude><exclude>*:badArtifact</exclude></excludes><includes><!--only 1.0 of badArtifact is allowed--><include>org.apache.maven:badArtifact:1.0</include></includes></bannedDependencies></rules><fail>false</fail></configuration></execution></executions></plugin>dependencyCheck_v6.sh,分析所有代碼倉庫的maven依賴
#! /bin/bash basePath=`pwd` resultFolder=$basePath'/'resultFolder startRow='have been resolved' endRow='[INFO] BUILD' exportTime=`date +%y%m%d%H%M%S`function outputDenpendecy(){{temp=`mvn dependency:resolve`mvn dependency:resolve -Dverbose | (sed -n "/$startRow/,/$endRow/p" | grep -Ev "($startRow|$endRow)") >resolve.txtif [[ ! -s 'resolve.txt' ]] || [[ `grep -c '\[ERROR\]' resolve.txt` -ne '0' ]]thenecho "【ERROR】--- $1 執行pom.xml文件出錯"echo $1','執行pom.xml文件出錯 >>$2'/'fail.$exportTime.csvreturnficat resolve.txt | while read linedoif [[ $line =~ ':jar:' ]];then#從右向左截取第一個 : 后的字符串temp1=${line%:*} #從左向右截取第一個 ] 后的字符串temp2=${temp1#*]} version=${temp2##*:}groupId=${temp2%%:*}temp3=${temp2#*:}artifactId=${temp3%%:*}echo $1','$groupId','$artifactId','$version >>$2'/'collect.$exportTime.csvfidone} && {rm -rf resolve.txt} } # 遍歷存在pom.xml的最淺層目錄的所有pom文件 function shallowDir(){pomList=(`find $1'/'$2 -name 'pom.xml'`)pathList=()count=0for pom in ${pomList[@]}; dopathList[$count]=`dirname $pom`count=$(expr $count + 1)doneif [ $count -eq 0 ]; thenecho "【WARNING】---$2 沒有pom.xml文件"echo $2','沒有pom.xml文件 >>$3'/'fail.$exportTime.csvreturnfifor curPath in ${pathList[@]}dodelCount=0for otherPath in ${pathList[@]}doif [[ $otherPath != $curPath ]] && [[ $otherPath =~ ^$curPath ]]; then unset pathList[$delCount]pathList=(${pathList[*]})elsedelCount=$(expr $delCount + 1)fidonedonefor path in ${pathList[@]};docd $pathoutputDenpendecy $2 $3done }if [ ! -d "$resultFolder" ] thenmkdir "$resultFolder" fi touch $resultFolder'/'collect.$exportTime.csv touch $resultFolder'/'fail.$exportTime.csvfor file in `ls $basePath` doif [[ -d $file ]]&&[[ $file != 'resultFolder' ]];thenecho "【RUNNING】---正在執行統計依賴 $file..."# 進入項目目錄cd $fileif [ -e 'pom.xml' ];thenoutputDenpendecy $file $resultFolderelseshallowDir $basePath $file $resultFolderfi# 返回到代碼匯總目錄,不能使用cd ..cd $basePathfi done?
errorCheck.sh?處理代碼倉庫master分支依賴分析出錯
#! /bin/bashbasePath=`pwd` resultFolder=$basePath'/'resultFolder exportTime=`date +%y%m%d%H%M%S` cat errorList.txt | while read line dotemp1=`echo $line | tr -d '\\r'`if [ ! -d $temp1 ]thencontinueficd $temp1branchList=(`git branch --format='%(refname:short)'`)for branch in ${branchList[*]}; dotemp=`git checkout $branch`mvn dependency:list >errorCheck.txtif [[ `grep -c '\[ERROR\]' errorCheck.txt` -ne '0' ]]thenecho $temp1','$branch',ERROR'>>$resultFolder'/'errorCheck.$exportTime.csvelseecho $temp1','$branch',SUCCESS'>>$resultFolder'/'errorCheck.$exportTime.csvfirm -rf errorCheck.txtdonecd .. done?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的shell脚本常用命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kafka记录及面试题
- 下一篇: 清华新生C++作业难上热搜!大厂猎头表示