sh_config.sh 9.6 KB

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