删除目录下的除今天外的所有文件夹 目录下的文件夹规律【年月日时分秒】
1 2 3 4 5 6 7 8 9 10 $ ll total 0 drwxr-xr-x 2 taoyi staff 68B 11 3 18:29 20171027121219 drwxr-xr-x 3 taoyi staff 102B 11 3 18:30 20171101124273 drwxr-xr-x 3 taoyi staff 102B 11 3 18:29 20171102124212 drwxr-xr-x 2 taoyi staff 68B 11 3 18:29 20171103121216 drwxr-xr-x 3 taoyi staff 102B 11 3 18:29 20171103124211 drwxr-xr-x 2 taoyi staff 68B 11 3 18:29 20171103124216 drwxr-xr-x 2 taoyi staff 68B 11 3 18:29 20171104124212
shell脚本
1 2 3 4 5 6 7 8 9 10 11 12 ls -l /Users/taoyi/Desktop/test0001/ | awk '/^d/ {print $NF}' > /Users/taoyi/Desktop/test0001/dir for i in $(grep -v `date +%Y%m%d` /Users/taoyi/Desktop/test0001/dir)do rm -rf /Users/taoyi/Desktop/test0001/$i done rm -rf /Users/taoyi/Desktop/test0001/dir
kill指定name的pid 1 2 3 4 5 6 7 8 pid_name=$1 ps -ef | grep -v grep | grep $pid_name | while read username pid other do kill -9 $pid done
adb截图导出并展示 1 2 3 4 5 6 adb shell /system/bin/screencap -p /sdcard/screenshot.jpg adb pull /sdcard/screenshot.jpg ~/shell-tools/ScreenShots/ open ~/shell-tools/ScreenShots/screenshot.jpg
android打包并安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 cd /Users/taoyi/git_projects/Gitlab/androidclientnative/git checkout develop git pull rm -rf /Users/taoyi/git_projects/Gitlab/androidclientnative/app/build/ rm -rf /Users/taoyi/shell-tools/APK/*.apk gradle clean assembleFengcheBeta cp -rf /Users/taoyi/git_projects/Gitlab/androidclientnative/app/build/outputs/apk/beta/*.apk /Users/taoyi/shell-tools/APK/ adb uninstall com.shadow.fengche adb install /Users/taoyi/shell-tools/APK/*.apk
Shell脚本解析xml文件字段 示例文件内容build.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <parameters > <hudson.model.StringParameterValue > <name > SCHEME</name > <description > scheme configuration of this project StoreCI</description > <value > Coding_iOS</value > </hudson.model.StringParameterValue > <hudson.model.StringParameterValue > <name > CONFIGURATION</name > <description > configuration of packing, Release/Debug</description > <value > Release</value > </hudson.model.StringParameterValue > <hudson.model.StringParameterValue > <name > OUTPUT_FOLDER</name > <description > output folder for build artifacts, it is located in workspace/project root dir.</description > <value > build_outputs</value > </hudson.model.StringParameterValue > <hudson.model.StringParameterValue > <name > BRANCH</name > <description > git repository branch</description > <value > master</value > </hudson.model.StringParameterValue > </parameters > <causeBag class ="linked-hash-map" > <entry > <hudson.model.Cause_-UserIdCause > <userId > shadow</userId > </hudson.model.Cause_-UserIdCause > <int > 1</int > </entry > </causeBag >
1 2 3 4 5 sed -n 's/.*>\(.*\)<\/userId>/\1/p' $JENKINS_HOME /jobs /$JOB_NAME /builds/$BUILD_NUMBER /build.xml userId=(`sed -n 's/.*>\(.*\)<\/userId>/\1/p' $JENKINS_HOME /jobs /$JOB_NAME /builds/$BUILD_NUMBER /build.xml`)
Shell脚本获取文本特定字段 1 2 head -1 $JENKINS_HOME /jobs /$JOB_NAME /builds/$BUILD_NUMBER /log | awk -F '\\[0m' '{print $NF}'
Jenkins获取构建人,并赋值到变量并使用 取值shell
1 2 3 head -1 $JENKINS_HOME /jobs /$JOB_NAME /builds/$BUILD_NUMBER /log | awk -F '\\[0m' '{print $NF}' > userId read userId < userIdecho "userId=${userId} " > userId.txt
set Build Name
中加入构建人
1 ${PROPFILE,file="userId.txt",property="userId"}
Jenkins获取安卓APP版本号并赋值给变量并使用 取值shell
1 2 versionName=`cat app/gradle.properties | grep 'VERSION_NAME' | cut -d '=' -f 2 ` echo "versionName=${versionName} " > versionName.txt
set Build Name
中加入安卓APP版本号
1 ${PROPFILE,file="versionName.txt",property="versionName"}
获取目录的所有csv文件并合并为一个csv文件 1 2 3 4 5 6 7 8 9 10 11 12 interface=$1 cd /Users/taoyi/git_projects/Gitlab/RF_InterfaceTest/Library/处理接口文档mv -f ./*/*/*/*/*.csv ./CSV/ cat ./CSV/*.csv > ./$interface .csv rm -rf ./output rm -rf ./CSV/*.csv
Json
中的字典转化成Robot-Framework
的参数格式1 2 3 4 5 6 7 8 file_name=$1 sed -ig 's/":/=/g' $file_name sed -ig 's/"//g' $file_name sed -ig 's/,/ /g' $file_name
简单的服务器部署脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #!/bin/bash set -o errexitset -o xtracedir_project_home="/home/shadow/projects/topgear" dir_tomcat_home="/home/shadow/tomcats/12001_topgear-test" file_war="*.war" file_catalina_out=${dir_tomcat_home} "/logs/catalina.out" echo "----update code from git begin" cd ${dir_project_home} git reset --hard git pull branch=$1 if [ ! -n "$branch " ];then git checkout master else git checkout $branch fi echo "----update code from git end" echo "----build project begin" cd ${dir_project_home} mvn clean install -DskipTests=true echo "----build project end" echo "----shutdown tomcat" ps auxwww | grep java | grep ${dir_tomcat_home} | awk '{print $2}' | xargs kill -9 2>/dev/null; sleep 1s echo "----reset war file" rm -rf ${dir_tomcat_home} /webapps/ROOT; rm -rf ${dir_tomcat_home} /webapps/ROOT.war; cp -r ${dir_project_home} /topgear-web/target/$file_war ${dir_tomcat_home} /webapps/ROOT.war echo "----start tomcat" sh ${dir_tomcat_home} /bin/startup.sh; cd ${dir_tomcat_home} /logstail -f catalina.out
Jenkins上部署Tomcat的通用脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!/bin/bash set -o errexitset -o xtraceexport BUILD_ID=pleaseDontKillMeexport JAVA_HOME=/opt/shadow/javadir_tomcat_home="/home/shadow/tomcats/12001_topgear-test" file_war="*.war" file_catalina_out=${dir_tomcat_home} "/logs/catalina.out" echo "----build project begin" mvn config:load -Denv=DEV-STABLE -Dtoken=O6eq7WSKlC mvn clean install -DskipTests=true echo "----build project end" echo "----shutdown tomcat" ps auxwww | grep java | grep ${dir_tomcat_home} | awk '{print $2}' | xargs kill -9 2>/dev/null; sleep 1s echo "----reset war file" rm -rf ${dir_tomcat_home} /webapps/ROOT; rm -rf ${dir_tomcat_home} /webapps/ROOT.war; cp -r ${WORKSPACE} /topgear-web/target/$file_war ${dir_tomcat_home} /webapps/ROOT.war echo "----start tomcat" sh ${dir_tomcat_home} /bin/startup.sh;
自动部署服务器应用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 server_tomcat=$1 git_repo=$2 nu=$3 tar -xvf apache-tomcat-9.0.4.tar.gz mv apache-tomcat-9.0.4 ${server_tomcat} cp 12001_topgear-test/deploy.sh ${server_tomcat} cp 12001_topgear-test/conf/server.xml ${server_tomcat} /conf/server.xml sed -ig "s/11001/110${nu} /g" ./${server_tomcat} /conf/server.xml sed -ig "s/12001/120${nu} /g" ./${server_tomcat} /conf/server.xml sed -ig "s/13001/130${nu} /g" ./${server_tomcat} /conf/server.xml sed -ig "s/14001/140${nu} /g" ./${server_tomcat} /conf/server.xml sed -ig "s/15001/150${nu} /g" ./${server_tomcat} /conf/server.xml sed -ig "s/topgear\"/${git_repo} \"/g" ./${server_tomcat} /deploy.sh sed -ig "s/12001_topgear-test/${server_tomcat} /g" ./${server_tomcat} /deploy.sh sed -ig "s/topgear-web/${git_repo} -web/g" ./${server_tomcat} /deploy.sh sed -ig 's/mvn config:load/# mvn config:load/g' ./${server_tomcat} /deploy.sh
Sed后面的表达式一般用单引号引起来'
,当需要使用变量 时就换用双引号"
。
指定次数执行pybot
脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 tcName=$1 directPath=$2 total=$3 count=1 while [ "$count " -le "$total " ]; do echo "$count " pybot -d results --test "$tcName " "$directPath " count=$((count + 1)) done echo "Finished."
顺序列出当前服务器上运营的 tomcat 应用 1 2 ps -ef | grep tomcat | awk -F "/" '{print $10 | sort -n}'
指定文件中的换行符替换为空格 使用正则1 cat listTomcat | sed ':label;N;s/\n/ /;b label'
使用 shell1 cat listTomcat | tr "\n" " "
根据 tag 和 env 执行用例 1 2 3 4 5 6 7 8 9 10 11 12 13 #!/usr/bin/env bash env=$1 tag=$2 ~/.pyenv/shims/pybot \ -d results \ -V ./envVars.py:$env \ --listener ./RobotListener.py \ --include=$tag \ .
把文件A的内容插入到文件B中的指定行后 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #!/usr/bin/env bash tomcatDir=$1 echo "hello, begin..." echo "" echo "dealing ${tomcatDir} " src_file="/home/shadow/scripts/resource" dest_file="/home/shadow/jenkins/Home/jobs/" ${tomcatDir} "/config.xml" function addLines (){ :<<BLOCK sed -i "2i\\insert line" file 该sed命令使用的是-i参数指定i\选项,在第2行后插入内容 2i\\ 拆解3部分:2为行号,i\为sed行下追加命令,\为转义字符(必须转义读取变量) "" 双引号,保持引号内的字面值,可读\$转义后的变量内容,单引号不行。 echo $delimit_line | sed -i "2i\\$delimit_line " $dest_file BLOCK sed -i.backup $(($(cat ${dest_file} | wc -l)))'d' ${dest_file} cat $src_file | while read line do echo $line | sed -i $(($(cat ${dest_file} | wc -l)+1))"i\\$line " $dest_file echo $line done } addLines echo "" echo "hey, end..." exit 0
shell脚本中调用shell脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/usr/bin/env bash jobsList="/home/shadow/scripts/jobsList" function repeatAddLines (){ cat $jobsList | while read line do /home/shadow/scripts/addLines.sh ${line} done } repeatAddLines