# 判断文件目录是否存在
dir="/tmp/workspace"
if [ ! -d "$dir" ]; then
# mkdir /myfolder
# ...
fi
# 判断文件是否存在并具备可执行权限
folder="/bin/bash"
if [ ! -x "$folder" ]; then
# exec ...
fi
# -f 参数判断 $file 是否存在
file="/tmp/lock"
if [ ! -f "$file" ]; then
touch "$file"
fi
# -n 判断一个变量是否有值
var=""
if [ ! -n "$var" ]; then
echo "$var is empty"
exit 0
fi
# 判断两个变量是否相等
if [ "$var1" = "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi
# 更多参考 https://www.cnblogs.com/jjzd/p/6397495.html
awk and sed
# 删除最后一个字段
$ minikube kubectl -- get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ' | awk '{$NF="";print}'
# File Content as below, file.txt:
$ cat file.txt
Mike Harrington:[510] 548-1278:250:100 :25
Christian Dobbins:[408] 538-2358:155:90 :78
Susan Dalsass:[206] 654-6279:250:60 :49
Archie McNichol:[206] 548-1348:250:100 :200
$ awk -F: 'OFS=":"{$NF="";print}' file.txt
Mike Harrington:[510] 548-1278:250:100 :
Christian Dobbins:[408] 538-2358:155:90 :
Susan Dalsass:[206] 654-6279:250:60 :
Archie McNichol:[206] 548-1348:250:100 :
# 删除最后一个分隔符
$ awk -F: 'OFS=":"{$NF="";print}' file.txt | sed 's/:$//g'
Mike Harrington:[510] 548-1278:250:100
Christian Dobbins:[408] 538-2358:155:90
Susan Dalsass:[206] 654-6279:250:60
Archie McNichol:[206] 548-1348:250:100
shell map
# https://stackoverflow.com/questions/6047648/associative-arrays-error-declare-a-invalid-option
# 查看 bash 的版本
bash --version
# 使用 array 实现 hashmap 的功能
# 在 bash v3 版本中不支持 hashmap 的声明
array=(
'hello::world.'
'nice::to meet you'
)
for index in "${array[@]}" ; do
KEY="${index%%::*}"
VALUE="${index##*::}"
echo "$KEY - $VALUE"
done
# 在 bash v4 中支持 map
declare -A hashmap
hashmap["key"]="value"
hashmap["key2"]="value2"
echo -e "${hashmap[\"key\"]}"
for key in ${!hashmap[@]}; do echo $key; done
for value in ${hashmap[@]}; do echo $value; done
Java Libs
Lombok
开发工具
Arthas: Java 线上诊断工具
项目模版
实践
UML
Vagrant
HashiCorp Vagrant provides the same, easy workflow regardless of your role as a developer, operator, or designer. It leverages a declarative configuration file which describes all your software requirements, packages, operating system configuration, users, and more.
Vagrant 是一个非常好用的工具,仅仅声明一个定义文件就可以获得一个相同的环境;在开发阶段利用 Vagrant 可以获得相同的开发环境,屏蔽不同环境带来的差异;Vagrant 底层是依赖于 VM 虚拟机的能力