sh_config.sh 8.9 KB

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