├── .gitignore ├── README.md ├── bin └── bashgems.sh ├── lib ├── hogwarts │ ├── adb.sh │ └── hogwarts.sh └── shellex.sh └── test └── test_bashgem.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## BashGems 2 | 3 | 用于增强 Bash 功能,BashGems 通过加载各类有用的函数实现对系统命令的扩展,对标 python 中的 pip 4 | 5 | ## Get Start 6 | 7 | quick install 8 | 9 | ```bash 10 | eval "$(curl https://raw.githubusercontent.com/seveniruby/bashgems/master/bin/bashgems.sh);bashgems_install" 11 | ``` 12 | 13 | ## Use 14 | 15 | ```bash 16 | # 霍格沃兹测试工具包,显示所有的学院提供的帮助命令 17 | hogwarts help 18 | # 更便捷的安装appium 19 | proxy npm install -g appium --verbose 20 | # 显示chromedriver与chrome的版本对应关系和下载地址 21 | chromedriver_list 22 | ``` 23 | 24 | 为了让测试工程师安装自动化测试工具更方便,我们提供了 proxy 命令来确保安装顺利。 25 | 更多功能详见代码。 26 | 27 | ## Maintainers 28 | 29 | 霍格沃兹测试学院 30 | -------------------------------------------------------------------------------- /bin/bashgems.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # ARGS="install list" pp "$@" 4 | pp() { 5 | local key i count=$# 6 | if [ -z "$ARGS" ]; then 7 | echo you should use ARGS=\"x y long\" pp to parse parameters 8 | return 1 9 | fi 10 | ARGS="help $ARGS" 11 | for ((i = 0; i < count; i++)); do 12 | [ -z "$1" ] && shift && continue 13 | for p in $ARGS; do 14 | if [ "$1" = "$p" -o "$1" = "-$p" -o "$1" = "--$p" ]; then 15 | if [ -n "$2" ]; then 16 | eval "$p=\"${2//\"/\\\"}\"" #unset the var and set value to it 17 | else 18 | eval "$p=true" #unset the var and set value to it 19 | fi 20 | fi 21 | done 22 | shift 23 | done 24 | 25 | if [ "$help" = "true" ]; then 26 | echo "${FUNCNAME[1]}() need set parameters" 27 | for p in $ARGS; do 28 | eval echo $p=\${$p} 29 | done 30 | echo 31 | fi 32 | } 33 | 34 | bashgems_install() { 35 | local date=$(date +%Y%m%d) 36 | [ -f /tmp/bashgems_$date.zip ] || wget https://github.com/seveniruby/bashgems/archive/master.zip -O /tmp/bashgems_$date.zip 37 | if which unzip 2>/dev/null; then 38 | unzip -o /tmp/bashgems_$date.zip -d /tmp/bashgems_$date 39 | [ -d ~/.bashgems ] || mkdir ~/.bashgems 40 | find /tmp/bashgems_$date/bashgems-master -maxdepth 1 -mindepth 1 -exec cp -rf {} ~/.bashgems/ \; 41 | elif which git; then 42 | if [ -d ~/.bashgems ]; then 43 | cd ~/.bashgems 44 | git pull 45 | cd $OLDPWD 46 | else 47 | git clone https://github.com/seveniruby/bashgems.git ~/.bashgems 48 | fi 49 | else 50 | echo "you should install unzip or git" 51 | return 1 52 | fi 53 | grep bashgems.sh ~/.bash_profile || echo '[ -f ~/.bashgems/bin/bashgems.sh ] && . ~/.bashgems/bin/bashgems.sh' >>~/.bash_profile 54 | . ~/.bash_profile 55 | [ -n "$BASHGEMS_HOME" ] && echo success 56 | } 57 | 58 | logo() { 59 | : 60 | } 61 | 62 | #default library 63 | bashgems_init() { 64 | [ -f $BASHGEMS_HOME/lib/shellex.sh ] && . $BASHGEMS_HOME/lib/shellex.sh 65 | # mo only for bash 66 | # [ -f $BASHGEMS_HOME/lib/cgi/mo ] && . $BASHGEMS_HOME/lib/cgi/mo 67 | [ -f $BASHGEMS_HOME/lib/hogwarts/hogwarts.sh ] && . $BASHGEMS_HOME/lib/hogwarts/hogwarts.sh 68 | : 69 | } 70 | 71 | ######################### 72 | export BASHGEMS_HOME=$(cd $( [ -n "$BASH" ] && dirname $BASH_SOURCE; [ -n "$ZSH_NAME" ] && dirname $0 )/../ && echo $PWD) 73 | bashgems_init 74 | 75 | for f in "$@"; do 76 | $f 77 | done 78 | -------------------------------------------------------------------------------- /lib/hogwarts/adb.sh: -------------------------------------------------------------------------------- 1 | log=/tmp/adb.log 2 | echo "# $$ "$(date "+%Y/%m/%d %H:%M:%S") >> $log 3 | echo "# ppid: $(ps -o command $(ps -o ppid $$ | tail -1) | tail -1)" >> $log 4 | echo "adb $@" >> $log 5 | if echo "$@" | grep -E "logcat |exec-out |uiautomator runtest" &>/dev/null; then 6 | echo "exec" >> $log 7 | exec /Users/seveniruby/Library/Android/sdk//platform-tools/adb.bak "$@" 8 | elif echo "$@" | grep "dumpsys package io.appium.settings" &>/dev/null; then 9 | echo "mock" >> $log 10 | cat /Users/seveniruby/temp/appium/package.mock | tee -a $log 11 | elif echo "$@" | grep "io\.appium\.settings" &>/dev/null;then 12 | echo "mock" >> $log 13 | echo "11111" | tee -a $log 14 | else 15 | result=$(/Users/seveniruby/Library/Android/sdk//platform-tools/adb.bak "$@") 16 | echo "origin" >> $log 17 | echo "$result" | tee -a $log 18 | fi 19 | echo "" >> $log -------------------------------------------------------------------------------- /lib/hogwarts/hogwarts.sh: -------------------------------------------------------------------------------- 1 | hogwarts() { 2 | echo ' 3 | # __ __ 4 | # / /_ ____ ____ __ ______ ______/ /______ 5 | # / __ \/ __ \/ __ `/ | /| / / __ `/ ___/ __/ ___/ 6 | # / / / / /_/ / /_/ /| |/ |/ / /_/ / / / /_(__ ) 7 | # /_/ /_/\____/\__, / |__/|__/\__,_/_/ \__/____/ 8 | # /____/ 9 | ' 10 | echo "Hogwarts Testing Kit" 11 | echo 12 | 13 | local help install start 14 | ARGS="install start" pp "$@" 15 | 16 | if [ -n "$help" -o "$#" = 0 ]; then 17 | echo "hogwarts provide such functions" 18 | grep "() *{" $BASHGEMS_HOME/lib/hogwarts.sh 19 | return 20 | fi 21 | if [ -n "$install" ]; then 22 | eval hogwarts_install_$install 23 | fi 24 | } 25 | 26 | #use some command with proxy 27 | #proxy npm install -g appium 28 | proxy() { 29 | local http_proxy https_proxy 30 | if curl -s http://www.baidu.com -x http://127.0.0.1:3128 &>/dev/null; then 31 | http_proxy=http://127.0.0.1:3128 https_proxy=$http_proxy "$@" 32 | else 33 | http_proxy=http://39.105.132.200:$(date +6%m%d) https_proxy=$http_proxy "$@" 34 | fi 35 | } 36 | 37 | chromedriver_list() { 38 | curl https://raw.githubusercontent.com/appium/appium/master/docs/en/writing-running-appium/web/chromedriver.md 2>/dev/null | grep "|" | grep -v "2.[0-9] " | grep -v "2.1[0-9]" 39 | echo chromedriver in CN: https://npm.taobao.org/mirrors/chromedriver 40 | echo chromedriver in US: http://chromedriver.storage.googleapis.com/ 41 | echo chromedriver vs chrome version: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md 42 | } 43 | 44 | hogwarts_install_test() { 45 | echo "${FUNCNAME[1]} -> ${FUNCNAME[0]}" 46 | } 47 | hogwarts_install_stf() { 48 | brew install rethinkdb graphicsmagick zeromq protobuf yasm pkg-config 49 | npm install -g stf 50 | } 51 | 52 | Hogwarts_start_stf() { 53 | rethinkdb & 54 | stf local 55 | } 56 | 57 | hogwarts_install_jenkins() { 58 | local image=jenkins/jenkins:lts 59 | docker run -ti --rm --entrypoint="/bin/bash" $image -c "whoami && id" 60 | echo "maybe you need chown -R 1000 ~/jenkins/hogwarts" 61 | docker run -d --name jenkins_hogwarts \ 62 | -p 8080:8080 -p 50000:50000 \ 63 | -v ~/jenkins/hogwarts:/var/jenkins_home \ 64 | $image 65 | echo "you need use such password for init" 66 | docker exec jenkins_hogwarts sh -c 'cat /var/jenkins_home/secrets/initialAdminPassword' 67 | } 68 | 69 | hogwarts_install_appium() { 70 | if which node; then 71 | npm version 72 | else 73 | echo you should install node 8 or above 74 | if which brew; then 75 | brew install node 76 | else 77 | echo you should install brew by /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 78 | fi 79 | fi 80 | which java || echo you should install java 81 | which adb || echo you should install android sdk and put adb directory in your PATH 82 | echo search appium versions 83 | proxy npm view appium versions --json | tail 84 | echo start install appium 85 | proxy npm install -g appium --verbose 86 | echo start install ios testing toolkit 87 | if which brew; then 88 | brew info libimobiledevice || brew install libimobiledevice 89 | brew info ios-webkit-debug-proxy || brew install ios-webkit-debug-proxy 90 | fi 91 | echo start install ios-deploy 92 | npm info ios-deploy || npm install -g ios-deploy --verbose 93 | appium --version 94 | } 95 | 96 | hogwarts_get_capabilitys_android() { 97 | local info=$(adb logcat -d "*:S" "ActivityManager:I" | grep -i displayed | grep -o '[^/ ]*/[^: ]*') 98 | local package=$(echo "$info" | awk -F/ '{print $1}' | tail -1) 99 | local activity=$(echo "$info" | grep $package | head -1 | awk -F/ '{print $2}') 100 | echo " 101 | { 102 | \"platformName\": \"android\", 103 | \"deviceName\": \"hogwarts\", 104 | \"automationName\": \"uiautomator2\", 105 | \"appPackage\": \"$package\", 106 | \"appActivity\": \"$activity\" 107 | } 108 | " 109 | } 110 | 111 | hogwarts_start_avd() { 112 | local name=$(emulator -list-avds | grep $1) 113 | ( 114 | cd $(dirname $(which emulator)) 115 | emulator @$name 116 | ) 117 | } 118 | -------------------------------------------------------------------------------- /lib/shellex.sh: -------------------------------------------------------------------------------- 1 | defineY() { 2 | set | grep "^$1=" 3 | } 4 | 5 | : <$tmp 24 | echo session_name=$s 25 | screen -ArxRL ${s:=$USER} -c $tmp -t ${s:=$USER} 26 | } 27 | screen_ex() { 28 | exscreen "$@" 29 | } 30 | ps_ex() { 31 | ps -o uname,pid,ppid,thcount,ni,pri,psr,pcpu,pmem,rss,vsz,sz,start_time,time,comm,c,command "$@" 32 | } 33 | 34 | parallel() { 35 | [ $# -lt 2 ] && ls | echo parallel 10 "echo $index" && return 36 | local p=$1 37 | shift 38 | local cmd="$@" 39 | echo cmd="$cmd" 40 | echo parallel=$p 41 | while true; do 42 | local count=$(jobs -l | grep Running | wc -l) 43 | echo running count=$count 44 | if ((count >= p)); then 45 | sleep 1 46 | else 47 | echo "run $cmd" 48 | eval $cmd & 49 | fi 50 | done 51 | } 52 | 53 | #key=2 value=1 include="a c" exclude="b" include_and_exclude 54 | include_and_exclude() { 55 | local sep 56 | local key 57 | local value 58 | local include 59 | local exclude 60 | awk -v sep="$sep" \ 61 | -v key="$key" \ 62 | -v value="$value" \ 63 | -v include_keys="$include" \ 64 | -v exclude_keys="$exclude" \ 65 | ' 66 | BEGIN{ 67 | if(sep!="") FS=sep 68 | if(key=="") key=0 69 | if(value=="") value=key 70 | include_count=split(include_keys,include_list," "); 71 | exclude_count=split(exclude_keys,exclude_list," "); 72 | } 73 | { 74 | if(include_count==0){ 75 | if( $0~include_list[i] && black[$key]!=1 ) 76 | {s[$key]=$value} 77 | }else{ 78 | for(i=1;i<=include_count;i++) 79 | if( $0~include_list[i] && black[$key]!=1 ) 80 | {s[$key]=$value} 81 | } 82 | } 83 | { 84 | for(i=1;i<=exclude_count;i++) if( $0~exclude_list[i]) {black[$key]=1; delete s[$key]} 85 | } 86 | END{ 87 | for(k in s) print k, s[k] 88 | } 89 | ' 90 | } 91 | 92 | #f(){ eval local x=$(get_temp_file 2 m 3 ); echo $x; } 93 | get_temp_file() { 94 | local t 95 | 96 | local r 97 | for p in "$@"; do 98 | [ "$p" = "%S" ] && p=$(date +%Y-%m-%dT%H:%M:%S) 99 | [ "$p" = "%M" ] && p=$(date +%Y-%m-%dT%H:%M) 100 | [ "$p" = "%H" ] && p=$(date +%Y-%m-%dT%H) 101 | [ "$p" = "%d" ] && p=$(date +%Y-%m-%d) 102 | 103 | r=$r.$p 104 | done 105 | echo "/tmp/\$FUNCNAME$r" 106 | } 107 | 108 | 109 | #f(){ local log=$(log_file %M) } 110 | log_file() { 111 | local r 112 | for p in "$@"; do 113 | [ "$p" = "%S" ] && p=$(date +%Y-%m-%dT%H:%M:%S) 114 | [ "$p" = "%M" ] && p=$(date +%Y-%m-%dT%H:%M) 115 | [ "$p" = "%H" ] && p=$(date +%Y-%m-%dT%H) 116 | [ "$p" = "%d" ] && p=$(date +%Y-%m-%d) 117 | 118 | r=$r.$p 119 | done 120 | caller 0 | awk -v t="$r" '{print $NF"."$(NF-1)t".log"}' 121 | } 122 | 123 | test_log_file(){ 124 | local log=$(log_file %M) 125 | echo log=$log 126 | } 127 | 128 | 129 | 130 | 131 | #python dict for shell 132 | #dict a 1 133 | #dict a 134 | #key=a dict 135 | #key=a value=1 dict 136 | dict() { 137 | local key k 138 | local value v 139 | [ -z "$key" ] && key="$k" 140 | [ -z "$value" ] && value="$v" 141 | [ -z "$key" ] && key="$1" 142 | [ -z "$value" ] && value="$2" 143 | 144 | for i in ${!_ArrayKeyGlobal[@]}; do 145 | if [ "${_ArrayKeyGlobal[i]}" = "$key" ]; then 146 | if [ -n "$value" ]; then 147 | #overwrite 148 | _ArrayValueGlobal[i]="$value" 149 | elif [ -z "$value" ]; then 150 | #get 151 | echo "${_ArrayValueGlobal[i]}" 152 | fi 153 | return 154 | fi 155 | done 156 | #new 157 | if [ -n "$value" ]; then 158 | #put 159 | _ArrayKeyGlobal+=("$key") 160 | _ArrayValueGlobal+=("$value") 161 | return 162 | elif [ -z "$value" ]; then 163 | #not exist 164 | return 1 165 | fi 166 | 167 | } 168 | 169 | test_dict() { 170 | unset _ArrayKeyGlobal _ArrayValueGlobal 171 | dict a 1 172 | dict b "2 3" 173 | [ "$(dict a)" = 1 ] || echo error 174 | [ "$(dict b)" = "2 3" ] || echo error 175 | 176 | k=a v=1 dict 177 | k=b v="2 3" dict 178 | [ "$(dict a)" = 1 ] || echo error 179 | [ "$(dict b)" = "2 3" ] || echo error 180 | 181 | key=a value=1 dict 182 | key=b value="2 3" dict 183 | [ "$(dict a)" = 1 ] || echo error 184 | [ "$(dict b)" = "2 3" ] || echo error 185 | 186 | dict c && echo error 187 | 188 | } 189 | 190 | #set and get return value 191 | # seveniruby:~ seveniruby$ f(){ 192 | # > echo 1 2 193 | # > return_value 3 4 194 | # > } 195 | # seveniruby:~ seveniruby$ f 196 | # 1 2 197 | # seveniruby:~ seveniruby$ return_value 198 | # 3 4 199 | return_value(){ 200 | [ $# -ge 1 ] && _RETURN_VALUE="$@" || echo $_RETURN_VALUE 201 | } 202 | 203 | 204 | #timestamp 2020-04-16 205 | #format="%Y-%m-%d" timestamp -1d 206 | timestamp() { 207 | local format 208 | [ -z "$format" ] && format="%s" 209 | 210 | local param="${@:-+0}" 211 | local result 212 | 213 | #depend gawk 214 | if echo "$param" | grep '^[+-]' >/dev/null; then 215 | result=$(awk -v diff=$(echo "0$param" | sed 's#d#*3600*24#' | bc) 'BEGIN{print systime()+diff}') 216 | else 217 | result=$(echo "$param" | awk -F ' |-|:' '{for(i=1;i<=6;i++) {if($i=="") $i="00";s=s$i" "};print mktime(s)}') 218 | fi 219 | if [ -n "$format" ]; then 220 | awk -v result="$result" -v format="$format" 'BEGIN{print strftime(format, result)}' 221 | else 222 | echo "$result" 223 | fi 224 | } 225 | 226 | 227 | #echo '{"a": 1, "b": [3,4]}' | json_parse "j=json.loads(sys.stdin.read());print(j['b'])" 228 | json_parse(){ 229 | python -c 'import sys; import os; import json;'"$@" 230 | } -------------------------------------------------------------------------------- /test/test_bashgem.sh: -------------------------------------------------------------------------------- 1 | bashgems_test() 2 | { 3 | BASHGEMS_HOME=$PWD 4 | bashgems_install 5 | ls 6 | bashgems_bgem_source_test 7 | bashgems_bgem_install_test 8 | } 9 | 10 | 11 | --------------------------------------------------------------------------------