sh_config.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 bash'
  90. alias dkinsh='docker exec -it $1 sh'
  91. alias dklog='docker logs -n 300 -f $1'
  92. alias dkinspect='docker inspect $1'
  93. ## go
  94. alias gobuildwinexe='GOOS=windows GOARCH=amd64 go build -o $1.exe $1.go'
  95. alias gobuildlinuxexe='GOOS=linux GOARCH=amd64 go build -o $1 $1.go'
  96. alias gobuildmacexe='GOOS=darwin GOARCH=amd64 go build -o $1 $1.go'
  97. ## version
  98. alias pythonversion='python --version'
  99. alias javaversion='java -version'
  100. alias nodeversion='node -v'
  101. alias centosversion='cat /etc/os-release'
  102. alias ubuntuversion='cat /etc/lsb-release'
  103. alias goversion='go version'
  104. ## ip查看
  105. alias ipremote='curl cip.cc'
  106. function iplocal() {
  107. # 如果是mac 过滤 en0,如果是linux 过滤 eth0
  108. if [[ "$OSTYPE" == "darwin"* ]]; then
  109. ifconfig en0 | grep inet | grep -v inet6
  110. else
  111. ifconfig eth0 | grep inet | grep -v inet6
  112. fi
  113. }
  114. ## 进程相关
  115. alias psgrep='ps aux | grep -v grep | grep'
  116. alias killport='kill -9 $(lsof -t -i:$1)'
  117. alias ps-mem-top6='ps aux --sort=-%mem | awk '\''{print $2, $11, $4, $6/1024 "MB"}'\'' | head -n 6'
  118. # node
  119. alias npminstall='npm install -g --registry=https://registry.npmmirror.com '
  120. # python
  121. alias pipinstall='pip install $1 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com'
  122. # 文件查找相关
  123. function findByName() {
  124. local pattern=$1
  125. find . -name "*$pattern*"
  126. }
  127. function findByContent() {
  128. local pattern=$2
  129. local dir=$1
  130. find $dir -type f -exec grep -l "$pattern" {} +
  131. }
  132. # 备份
  133. function tar_back()
  134. {
  135. time=$(date "+%Y-%m-%d_%H_%M_%S")
  136. tar -zcvf "${1}".${time}.tgz "$1"
  137. ll -h ${1}.${time}.tgz
  138. }
  139. pid-cmd-centos(){
  140. sudo ls -l /proc/$1/cwd | awk '{print $11}'
  141. sudo cat /proc/$1/cmdline
  142. echo ''
  143. }
  144. pid-cmd-ubuntu() {
  145. ls -l /proc/"$1"/cwd | awk '{print $11}'
  146. tr '\0' ' ' </proc/"$1"/cmdline; echo ""
  147. }
  148. function echo-arthas-install() {
  149. echo '
  150. # 判断是否存在 ~/arthas-boot.jar,如果不存在则下载
  151. if [ ! -f ~/arthas-boot.jar ]; then
  152. ( cd ~ && curl -O https://arthas.aliyun.com/arthas-boot.jar )
  153. fi
  154. # 判断是否在k8s里面
  155. if [[ ! -z "$KUBERNETES_SERVICE_HOST" && ! -z "$KUBERNETES_SERVICE_PORT" ]]; then
  156. echo "Running in Kubernetes environment"
  157. java -jar ~/arthas-boot.jar 1 --options json-format=true
  158. else
  159. echo "Not running in Kubernetes environment"
  160. java -jar ~/arthas-boot.jar --options json-format=true
  161. fi
  162. '
  163. }
  164. alias cparthas='echo-arthas-install | pbcopy'
  165. function curl-form() {
  166. curl -d "$2" "$1"
  167. }
  168. function watch-cpudiskmemory() {
  169. watch -n 1 '
  170. echo "===CPU占用摘要:"
  171. top -bn 1 -i -c | grep Cpu
  172. echo "
  173. ===内存占用摘要:"
  174. free -h
  175. echo "
  176. ===磁盘占用摘要:"
  177. df -h /'
  178. }
  179. #123
  180. function vimrc() {
  181. if [[ "$SHELL" =~ "zsh" ]]
  182. then
  183. code ~/.zshrc
  184. else
  185. code ~/.bashrc
  186. fi
  187. }
  188. merge_video_audio() {
  189. if [ "$#" -ne 3 ]; then
  190. echo "错误:参数数量不正确!"
  191. echo "用法:merge_video_audio video.mp4 audio.mp3 output.mp4"
  192. return 1
  193. fi
  194. ffmpeg -i "$1" -i "$2" -c:v copy -c:a copy "$3"
  195. }
  196. rm-all() {
  197. find . -name $1 | xargs -I {} rm -rf {}
  198. }
  199. ### mysql
  200. function echo-mysql-tables() {
  201. echo 'SELECT table_name, table_comment FROM information_schema.tables WHERE table_schema = DATABASE();'
  202. }
  203. function echo-mysql-table-size() {
  204. 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;'
  205. }
  206. function echo-mysql-db-size() {
  207. 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;'
  208. }
  209. function echo-npm() {
  210. echo '
  211. npm i webpack # 在module文件中安装
  212. npm i webpack -S # --save 常用,添加到package
  213. npm i webpack -D # --save-dev
  214. npm i webpack -g --registry=https://registry.npmmirror.com # 全局
  215. npm un webpack -g
  216. yarn add react
  217. yarn remove react
  218. yarn global add umi
  219. '
  220. }
  221. concat_params() {
  222. # 获取传入的参数个数
  223. num_params=$#
  224. # 判断参数个数是否大于等于1
  225. if [ $num_params -ge 1 ]; then
  226. # 使用循环遍历参数,并拼接在一起
  227. concat_str="$1"
  228. for ((i=2; i<=$num_params; i++)); do
  229. concat_str+="%20${!i}"
  230. done
  231. # 打印拼接后的字符串
  232. echo "$concat_str"
  233. else
  234. echo "没有传入参数"
  235. fi
  236. }
  237. function url_encode() {
  238. echo "$1" | sed 's/ /%20/g'
  239. }
  240. function bing() {
  241. ## 如果是windows系统,则使用start,否则使用open,后面的参数可以有多个空格
  242. if [[ "$OSTYPE" == "msys"* ]]; then
  243. search_query=$(concat_params "$@")
  244. start "https://www.bing.com/search?q=$search_query"
  245. else
  246. search_query=""
  247. for query in "$@"; do
  248. search_query+=" $query"
  249. done
  250. search_query=$(url_encode "$search_query")
  251. open "https://www.bing.com/search?q=${search_query}"
  252. fi
  253. }
  254. function mvn_clean_package() {
  255. mvn clean package -Dmaven.test.skip=true
  256. }
  257. ## ska快捷
  258. function ska_push_dev(){
  259. skaffold run -p dev --tail
  260. }
  261. function mvn_push_dev(){
  262. mvn_clean_package
  263. ska_push_dev
  264. }
  265. function mvn_push_test() {
  266. mvn_clean_package
  267. skaffold build -p test -n aimptest
  268. }
  269. function mvn_clean_deploy() {
  270. mvn clean deploy -Dmaven.test.skip=true
  271. }
  272. function show_all_local_ip(){
  273. arp -a
  274. }
  275. alias nodels='fnm ls'
  276. alias nodeuse='fnm use'
  277. alias nodedefault='fnm default'
  278. alias nodeinstall='fnm install'
  279. alias nodelsremote='fnm ls-remote'
  280. alias nodeuninstall='fnm uninstall'
  281. alias catnodeversion='node --version > .node-version'
  282. alias yarnglobaladd='yarn global add'
  283. alias yarnglobalremove='yarn global remove'
  284. alias yarnadd='yarn add'
  285. alias yarnremove='yarn remove'
  286. alias yarninstall='yarn install'
  287. function echosync() {
  288. echo '
  289. source <(curl -s https://git.tianyunperfect.cn/tianyunperfect/sync/raw/master/sh_config.sh)
  290. '
  291. }
  292. alias settitle='wezterm cli set-tab-title '
  293. function btm_linux(){
  294. if [ ! -f "$HOME/btm" ]; then
  295. wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/20/2024/btm/btm -O ~/btm
  296. chmod +x ~/btm
  297. fi
  298. ~/btm --process_memory_as_value
  299. }
  300. # watch 优化
  301. alias watch_alias='watch '
  302. # 函数,每隔2秒执行一次参数命令
  303. function watch_func() {
  304. while true; do
  305. clear
  306. "$@"
  307. sleep 2
  308. done
  309. }
  310. alias gpu_nvidia='nvidia-smi'
  311. alias gpu_amd='rocm-smi --showpids -u -P'