Skip to content

Commit fbc7aa8

Browse files
committed
add zsh support
1 parent abc4edd commit fbc7aa8

File tree

3 files changed

+224
-52
lines changed

3 files changed

+224
-52
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
2-
31
## BashGems
4-
这是TesterHome TTF旗下的一个开源项目,用于增强Bash功能,BashGems通过加载各类有用的函数实现对系统命令的扩展
2+
3+
用于增强 Bash 功能,BashGems 通过加载各类有用的函数实现对系统命令的扩展,对标 python 中的 pip
54

65
## Get Start
6+
77
quick install
8+
89
```bash
910
eval "$(curl https://raw.githubusercontent.com/seveniruby/bashgems/master/bin/bashgems.sh);bashgems_install"
1011
```
1112

1213
## Use
14+
1315
```bash
1416
# 霍格沃兹测试工具包,显示所有的学院提供的帮助命令
1517
hogwarts help
@@ -18,13 +20,10 @@ proxy npm install -g appium --verbose
1820
# 显示chromedriver与chrome的版本对应关系和下载地址
1921
chromedriver_list
2022
```
21-
为了让测试工程师安装自动化测试工具更方便,我们提供了proxy命令来确保安装顺利。
22-
更多功能详见代码。
2323

24-
## More
25-
- TesterHome: https://testerhome.com
26-
- TTF: https://testerhome.com/topics/15522
27-
- BashGems: https://github.com/seveniruby/bashgems.git
24+
为了让测试工程师安装自动化测试工具更方便,我们提供了 proxy 命令来确保安装顺利。
25+
更多功能详见代码。
2826

2927
## Maintainers
28+
3029
霍格沃兹测试学院

bin/bashgems.sh

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bashgems_install() {
4242
if [ -d ~/.bashgems ]; then
4343
cd ~/.bashgems
4444
git pull
45-
cd $OLDPWD
45+
cd $OLDPWD
4646
else
4747
git clone https://github.com/seveniruby/bashgems.git ~/.bashgems
4848
fi
@@ -56,26 +56,22 @@ bashgems_install() {
5656
}
5757

5858
logo() {
59-
echo '
60-
# __ __ __
61-
# / /____ _____/ /____ _____/ /_ ____ ____ ___ ___
62-
# / __/ _ \/ ___/ __/ _ \/ ___/ __ \/ __ \/ __ `__ \/ _ \
63-
# / /_/ __(__ ) /_/ __/ / / / / / /_/ / / / / / / __/
64-
# \__/\___/____/\__/\___/_/ /_/ /_/\____/_/ /_/ /_/\___/
65-
'
59+
:
6660
}
61+
62+
#default library
6763
bashgems_init() {
68-
logo
69-
echo "TesterHome: https://testerhome.com"
70-
echo "TTF: https://testerhome.com/topics/15522"
71-
echo "BashGems: https://github.com/seveniruby/bashgems.git"
72-
echo
7364
[ -f $BASHGEMS_HOME/lib/shellex.sh ] && . $BASHGEMS_HOME/lib/shellex.sh
74-
[ -f $BASHGEMS_HOME/lib/hogwarts.sh ] && . $BASHGEMS_HOME/lib/hogwarts.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
7568
:
7669
}
7770

7871
#########################
79-
[ -z "$BASHGEMS_HOME" ] && export BASHGEMS_HOME=~/.bashgems
80-
bashgems_init "$@"
81-
:
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

lib/shellex.sh

Lines changed: 203 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
tmp=/tmp/${USER}_tmp
2-
31
defineY() {
4-
set | grep "^$1="
2+
set | grep "^$1="
53
}
64

75
: <<sss_help
@@ -11,10 +9,10 @@ sss -u dinglh
119
sss -s session_name
1210
sss_help
1311
exscreen() {
14-
local s
15-
ARGS="tmp s" pp "$@"
12+
local s
13+
ARGS="tmp s" pp "$@"
1614

17-
echo -e '
15+
echo -e '
1816
multiuser on
1917
hardstatus on
2018
hardstatus alwayslastline
@@ -23,31 +21,210 @@ exscreen() {
2321
hardstatus string "%{= kG}%-w%{.cW}%n %t%{-}%+w %=%{= kG} %H %{..Y} %Y/%m/%d %c"
2422
logfile '"/tmp/${USER}_${s:=$USER}.log"'
2523
' >$tmp
26-
echo session_name=$s
27-
screen -ArxRL ${s:=$USER} -c $tmp -t ${s:=$USER}
24+
echo session_name=$s
25+
screen -ArxRL ${s:=$USER} -c $tmp -t ${s:=$USER}
2826
}
2927
screen_ex() {
30-
exscreen "$@"
28+
exscreen "$@"
3129
}
3230
ps_ex() {
33-
ps -o uname,pid,ppid,thcount,ni,pri,psr,pcpu,pmem,rss,vsz,sz,start_time,time,comm,c,command "$@"
31+
ps -o uname,pid,ppid,thcount,ni,pri,psr,pcpu,pmem,rss,vsz,sz,start_time,time,comm,c,command "$@"
3432
}
3533

3634
parallel() {
37-
[ $# -lt 2 ] && ls | echo parallel 10 "echo $index" && return
38-
local p=$1
39-
shift
40-
local cmd="$@"
41-
echo cmd="$cmd"
42-
echo parallel=$p
43-
while true; do
44-
local count=$(jobs -l | grep Running | wc -l)
45-
echo running count=$count
46-
if ((count >= p)); then
47-
sleep 1
48-
else
49-
echo "run $cmd"
50-
eval $cmd &
51-
fi
52-
done
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"}'
53121
}
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+
}

0 commit comments

Comments
 (0)