sh_config.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # 快捷命令
  2. ## 未分类
  3. alias ll='ls -alh --time-style="+%Y-%m-%d %H:%I:%S"'
  4. #du -sh * 2>/dev/null | sort -hr
  5. alias du-sh='du -sh * 2>/dev/null | sort -hr'
  6. alias sourcevenv='source venv/bin/activate'
  7. alias weather='curl wttr.in/beijing'
  8. alias dust1='dust -d 1'
  9. function gittagpush() {
  10. if [ $# -ne 2 ]; then
  11. echo "需要两个参数: <变量1> <变量2>"
  12. return 1
  13. fi
  14. git tag -a $1 -m "$2"
  15. git push origin $1
  16. }
  17. alias gittags='git --no-pager tag -l -n'
  18. function reloadrc() {
  19. if [[ "$0" =~ "zsh" ]]
  20. then
  21. source ~/.zshrc
  22. else
  23. source ~/.bashrc
  24. fi
  25. }
  26. ## k8s
  27. function k8s-getnamespace() {
  28. kubectl config view | grep namespace
  29. }
  30. function k8s-setnamespace() {
  31. k8s-getnamespace
  32. kubectl config set-context --current --namespace="$1"
  33. }
  34. function k8s-getpod() {
  35. k8s-getnamespace
  36. # 如果有参数,则过滤
  37. if [ ! -z "$1" ]; then
  38. kubectl get pods -o wide | grep "$1"
  39. else
  40. kubectl get pods -o wide
  41. fi
  42. }
  43. alias k8s-descpods='kubectl describe pods'
  44. function k8s-getlog() {
  45. k8s-getnamespace
  46. podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}')
  47. kubectl logs --tail=300 -f "${podName}"
  48. }
  49. function k8s-getlogbase() {
  50. k8s-getnamespace
  51. podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}')
  52. kubectl logs -c base-jre --tail=300 -f "${podName}"
  53. }
  54. alias k8s-delpod='kubectl delete pod ${1} --force'
  55. function k8s-inpod() {
  56. k8s-getnamespace
  57. podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}')
  58. kubectl exec -it "${podName}" -- /bin/bash
  59. }
  60. alias k8s-topnodes='kubectl top node ${1}'
  61. alias k8s-toppods='kubectl top pod ${1}'
  62. alias k8s-getsvc='kubectl get svc'
  63. alias k8s-descsvc='kubectl describe svc'
  64. alias k8s-deldeployment='kubectl delete deployment'
  65. alias k8s-getdeployment='kubectl get deployment'
  66. alias k8s-descdeployment='kubectl describe deployment'
  67. alias k8s-getdeploymentgrep='kubectl get deployment | grep'
  68. alias k8s-getingress='kubectl get ingress'
  69. alias k8s-descingress='kubectl describe ingress'
  70. alias k8s-getingressgrep='kubectl get ingress | grep'
  71. ## docker
  72. alias docker-rm-all-image='docker rmi $(docker images -q)'
  73. alias docker-rm-all-docker='docker rm $(docker ps -a -q)'
  74. alias docker-show-image-prune='docker image ls -f dangling=true'
  75. alias docker-image-prune='docker image prune -a'
  76. alias dkstats='docker stats'
  77. alias dkps='docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"'
  78. alias dkpsa='docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"'
  79. alias dkrm='docker rm -f $1'
  80. alias dkrmi='docker rmi -f $1'
  81. alias dkimage='docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"'
  82. alias dkip='docker inspect --format="{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $(docker ps -aq)'
  83. alias dkin='docker exec -it $1 /bin/bash'
  84. alias dklog='docker logs -n 300 -f $1'
  85. alias dkinspect='docker inspect $1'
  86. ## go
  87. alias gobuildwinexe='GOOS=windows GOARCH=amd64 go build -o $1.exe $1.go'
  88. alias gobuildlinuxexe='GOOS=linux GOARCH=amd64 go build -o $1 $1.go'
  89. alias gobuildmacexe='GOOS=darwin GOARCH=amd64 go build -o $1 $1.go'
  90. ## version
  91. alias pythonversion='python --version'
  92. alias javaversion='java -version'
  93. alias nodeversion='node -v'
  94. alias centosversion='cat /etc/os-release'
  95. alias ubuntuversion='cat /etc/lsb-release'
  96. alias goversion='go version'
  97. ## ip查看
  98. alias ipremote='curl cip.cc'
  99. function iplocal() {
  100. # 如果是mac 过滤 en0,如果是linux 过滤 eth0
  101. if [[ "$OSTYPE" == "darwin"* ]]; then
  102. ifconfig en0 | grep inet | grep -v inet6
  103. else
  104. ifconfig eth0 | grep inet | grep -v inet6
  105. fi
  106. }
  107. ## 进程相关
  108. alias psgrep='ps aux | grep -v grep | grep'
  109. alias killport='kill -9 $(lsof -t -i:$1)'
  110. alias ps-mem-top6='ps aux --sort=-%mem | awk '\''{print $2, $11, $4, $6/1024 "MB"}'\'' | head -n 6'
  111. # node
  112. alias npminstall='npm install -g --registry=https://registry.npmmirror.com '
  113. # python
  114. alias pipinstall='pip install $1 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com'
  115. # 文件查找相关
  116. function findByName() {
  117. local pattern=$1
  118. find . -name "*$pattern*"
  119. }
  120. function findByContent() {
  121. local pattern=$2
  122. local dir=$1
  123. find $dir -type f -exec grep -l "$pattern" {} +
  124. }
  125. # 备份
  126. function tar_back()
  127. {
  128. time=$(date "+%Y-%m-%d_%H_%M_%S")
  129. tar -zcvf "${1}".${time}.tgz "$1"
  130. ll -h ${1}.${time}.tgz
  131. }
  132. pid-cmd-centos(){
  133. sudo ls -l /proc/$1/cwd | awk '{print $11}'
  134. sudo cat /proc/$1/cmdline
  135. echo ''
  136. }
  137. pid-cmd-ubuntu() {
  138. ls -l /proc/"$1"/cwd | awk '{print $11}'
  139. tr '\0' ' ' </proc/"$1"/cmdline; echo ""
  140. }
  141. function echo-arthas-install() {
  142. echo '
  143. # 判断是否存在 ~/arthas-boot.jar,如果不存在则下载
  144. if [ ! -f ~/arthas-boot.jar ]; then
  145. ( cd ~ && curl -O https://arthas.aliyun.com/arthas-boot.jar )
  146. fi
  147. # 判断是否在k8s里面
  148. if [[ ! -z "$KUBERNETES_SERVICE_HOST" && ! -z "$KUBERNETES_SERVICE_PORT" ]]; then
  149. echo "Running in Kubernetes environment"
  150. java -jar ~/arthas-boot.jar 1 --options json-format=true
  151. else
  152. echo "Not running in Kubernetes environment"
  153. java -jar ~/arthas-boot.jar --options json-format=true
  154. fi
  155. '
  156. }
  157. alias cparthas='echo-arthas-install | pbcopy'
  158. function curl-form() {
  159. curl -d "$2" "$1"
  160. }
  161. function watch-cpudiskmemory() {
  162. watch -n 1 '
  163. echo "===CPU占用摘要:"
  164. top -bn 1 -i -c | grep Cpu
  165. echo "
  166. ===内存占用摘要:"
  167. free -h
  168. echo "
  169. ===磁盘占用摘要:"
  170. df -h /'
  171. }
  172. #123
  173. function vimrc() {
  174. if [[ "$SHELL" =~ "zsh" ]]
  175. then
  176. code ~/.zshrc
  177. else
  178. code ~/.bashrc
  179. fi
  180. }
  181. merge_video_audio() {
  182. if [ "$#" -ne 3 ]; then
  183. echo "错误:参数数量不正确!"
  184. echo "用法:merge_video_audio video.mp4 audio.mp3 output.mp4"
  185. return 1
  186. fi
  187. ffmpeg -i "$1" -i "$2" -c:v copy -c:a copy "$3"
  188. }
  189. rm-all() {
  190. find . -name $1 | xargs -I {} rm -rf {}
  191. }
  192. ### mysql
  193. function echo-mysql-tables() {
  194. echo 'SELECT table_name, table_comment FROM information_schema.tables WHERE table_schema = DATABASE();'
  195. }
  196. function echo-mysql-table-size() {
  197. echo 'select table_name,concat(round(sum(DATA_LENGTH/1024/1024),2),"MB") as data from information_schema.TABLES where table_schema =(SELECT DATABASE()) group by table_name;'
  198. }
  199. function echo-mysql-db-size() {
  200. echo 'select table_schema,concat(round(sum(DATA_LENGTH/1024/1024),2),"MB") as data from information_schema.TABLES where table_schema not in ("information_schema","mysql","performance_schema","sys") group by table_schema;'
  201. }
  202. function echo-npm() {
  203. echo '
  204. npm i webpack # 在module文件中安装
  205. npm i webpack -S # --save 常用,添加到package
  206. npm i webpack -D # --save-dev
  207. npm i webpack -g --registry=https://registry.npmmirror.com # 全局
  208. npm un webpack -g
  209. yarn add react
  210. yarn remove react
  211. yarn global add umi
  212. '
  213. }
  214. concat_params() {
  215. # 获取传入的参数个数
  216. num_params=$#
  217. # 判断参数个数是否大于等于1
  218. if [ $num_params -ge 1 ]; then
  219. # 使用循环遍历参数,并拼接在一起
  220. concat_str="$1"
  221. for ((i=2; i<=$num_params; i++)); do
  222. concat_str+="%20${!i}"
  223. done
  224. # 打印拼接后的字符串
  225. echo "$concat_str"
  226. else
  227. echo "没有传入参数"
  228. fi
  229. }
  230. function url_encode() {
  231. echo "$1" | sed 's/ /%20/g'
  232. }
  233. function bing() {
  234. ## 如果是windows系统,则使用start,否则使用open,后面的参数可以有多个空格
  235. if [[ "$OSTYPE" == "msys"* ]]; then
  236. search_query=$(concat_params "$@")
  237. start "https://www.bing.com/search?q=$search_query"
  238. else
  239. search_query=""
  240. for query in "$@"; do
  241. search_query+=" $query"
  242. done
  243. search_query=$(url_encode "$search_query")
  244. open "https://www.bing.com/search?q=${search_query}"
  245. fi
  246. }
  247. function mvn_clean_package() {
  248. mvn clean package -Dmaven.test.skip=true
  249. }
  250. ## ska快捷
  251. function ska_push_dev(){
  252. skaffold run -p dev --tail
  253. }
  254. function mvn_push_dev(){
  255. mvn_clean_package
  256. ska_push_dev
  257. }
  258. function mvn_push_test() {
  259. mvn_clean_package
  260. skaffold build -p test -n aimptest
  261. }
  262. function mvn_clean_deploy() {
  263. mvn clean deploy -Dmaven.test.skip=true
  264. }
  265. function show_all_local_ip(){
  266. arp -a
  267. }
  268. alias nodels='fnm ls'
  269. alias nodeuse='fnm use'
  270. alias nodedefault='fnm default'
  271. alias nodeinstall='fnm install'
  272. alias nodelsremote='fnm ls-remote'
  273. alias nodeuninstall='fnm uninstall'
  274. alias catnodeversion='node --version > .node-version'
  275. alias yarnglobaladd='yarn global add'
  276. alias yarnglobalremove='yarn global remove'
  277. alias yarnadd='yarn add'
  278. alias yarnremove='yarn remove'
  279. alias yarninstall='yarn install'
  280. function echosync() {
  281. echo '
  282. source <(curl -s https://git.tianyunperfect.cn/tianyunperfect/sync/raw/master/sh_config.sh)
  283. '
  284. }