#!/bin/bash # path去重 export PATH=$(echo $PATH | tr ':' '\n' | cat -n | sort -k2,2 -k1,1n | uniq -f1 | sort -k1,1n | cut -f2- | tr '\n' ':') function targz() { tar -zcvf "${1}".tgz "$1" ll -h ${1}.tgz } function untar() { tar -zxvf "$1" } function sync_config_file() { mkdir -p ~/.config/my_config wget https://git.tianyunperfect.cn/tianyunperfect/sync/raw/master/sh_config.sh -O ~/.config/my_config/sh_config.sh && source ~/.config/my_config/sh_config.sh } # 快捷命令 ## 未分类 alias ll='ls -alh --time-style="+%Y-%m-%d %H:%I:%S"' #du -sh * 2>/dev/null | sort -hr alias du-sh='du -sh * 2>/dev/null | sort -hr' alias du-sh10='du -sh * 2>/dev/null | sort -hr | head -n 10' alias df-h='df -h | grep -v tmpfs | grep -v overlay' # 显示挂载的磁盘 alias sourcevenv='source venv/bin/activate' alias weather='curl wttr.in/beijing' alias dust1='dust -d 1' function gittagpush() { if [ $# -ne 2 ]; then echo "需要两个参数: <变量1> <变量2>" return 1 fi git tag -a $1 -m "$2" git push origin $1 } alias gittags='git --no-pager tag -l -n' function reloadrc() { if [ -n "$ZSH_VERSION" ]; then echo "正在使用zsh" source ~/.zshrc elif [ -n "$BASH_VERSION" ]; then echo "正在使用bash" source ~/.bashrc else echo "无法确定当前 Shell 类型" fi } function install_mcfly() { # 用户目录是否存在 mcfly 文件 if [ ! -f ~/mcfly ]; then wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/file/2024/12/mcfly-v0.9.2-x86_64-unknown-linux-musl.tar.gz tar -zxvf mcfly-v0.9.2-x86_64-unknown-linux-musl.tar.gz rm -rf mcfly-v0.9.2-x86_64-unknown-linux-musl.tar.gz if [ -n "$ZSH_VERSION" ]; then echo "正在使用zsh" echo 'eval "$(mcfly init zsh)"' >>~/.zshrc elif [ -n "$BASH_VERSION" ]; then echo "正在使用bash" echo 'eval "$(mcfly init bash)"' >>~/.bashrc else echo "无法确定当前 Shell 类型" fi fi } # 安装监控用户命令 function install_monitor_user_command() { if [ -n "$ZSH_VERSION" ]; then curl -SL https://git.tianyunperfect.cn/tianyunperfect/note_pub/raw/master/install_monitor_user_command_zsh.sh | sh elif [ -n "$BASH_VERSION" ]; then curl -SL https://git.tianyunperfect.cn/tianyunperfect/note_pub/raw/master/install_monitor_user_command_bash.sh | sh else echo "无法确定当前 Shell 类型" fi } function install_vimrc() { wget -O ~/.vimrc https://git.tianyunperfect.cn/tianyunperfect/note_pub/raw/master/.vimrc } function k8s-get-all-namespace() { kubectl get namespace } ## k8s function k8s-get-default-namespace() { kubectl config view | grep namespace } function k8s-set-default-namespace() { kubectl config set-context --current --namespace="$1" } function k8s-getpod() { k8s-get-default-namespace # 如果有参数,则过滤 if [ ! -z "$1" ]; then kubectl get pods -o wide | grep "$1" else kubectl get pods -o wide fi } alias k8s-descpods='kubectl describe pods' function k8s-getlog() { k8s-get-default-namespace podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}') kubectl logs --tail=300 -f "${podName}" } function k8s-getlogbase() { k8s-get-default-namespace podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}') kubectl logs -c base-jre --tail=300 -f "${podName}" } function k8s-delpod() { kubectl delete pod "$1" --force } function k8s-inpod() { k8s-get-default-namespace podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}') kubectl exec -it "${podName}" -- /bin/bash } alias k8s-topnodes='kubectl top node' alias k8s-toppods='kubectl top pod' alias k8s-getsvc='kubectl get svc' alias k8s-descsvc='kubectl describe svc' alias k8s-deldeployment='kubectl delete deployment' alias k8s-getdeployment='kubectl get deployment' alias k8s-descdeployment='kubectl describe deployment' alias k8s-getdeploymentgrep='kubectl get deployment | grep' alias k8s-getingress='kubectl get ingress' alias k8s-descingress='kubectl describe ingress' alias k8s-getingressgrep='kubectl get ingress | grep' ## docker alias docker-rm-all-image='docker rmi $(docker images -q)' alias docker-rm-all-docker='docker rm $(docker ps -a -q)' alias docker-show-image-prune='docker image ls -f dangling=true' alias docker-image-prune='docker image prune -a' alias dkstats='docker stats' alias dkps='docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"' alias dkpsa='docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"' alias dkrm='docker rm -f' alias dkrmi='docker rmi -f' alias dkimage='docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"' alias dkip='docker inspect --format="{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $(docker ps -aq)' function dkin() { docker exec -it "$1" bash } function dkinsh() { docker exec -it "$1" sh } function dklog() { docker logs -n 300 -f "$1" } function dkinspect() { docker inspect "$1" } alias dkstart_all_exit='docker start $(docker ps -a -q -f status=exited)' ## go # 定义 gobuildwinexe 函数 function gobuildwinexe() { if [ $# -ne 2 ]; then echo "用法: gobuildwinexe <源文件> <输出文件名>" return 1 fi GOOS=windows GOARCH=amd64 go build -o "${2}.exe" "$1.go" } function gobuildlinuxexe() { if [ $# -ne 2 ]; then echo "用法: gobuildlinuxexe <源文件> <输出文件名>" return 1 fi GOOS=linux GOARCH=amd64 go build -o "$2" "$1.go" } function gobuildmacexe() { if [ $# -ne 2 ]; then echo "用法: gobuildmacexe <源文件> <输出文件名>" return 1 fi GOOS=darwin GOARCH=amd64 go build -o "$2" "$1.go" } ## version alias pythonversion='python --version' alias javaversion='java -version' alias nodeversion='node -v' alias centosversion='cat /etc/os-release' alias ubuntuversion='cat /etc/lsb-release' alias goversion='go version' ## ip查看 alias ipremote='curl cip.cc' function iplocal() { # 如果是mac 过滤 en0,如果是linux 过滤 eth0 if [[ "$OSTYPE" == "darwin"* ]]; then ifconfig en0 | grep inet | grep -v inet6 else ifconfig eth0 | grep inet | grep -v inet6 fi } ## 进程相关 alias psgrep='ps aux | grep -v grep | grep' # 根据pod获取pid,示例: ss -tunlp | grep :8080 function getpid-bypod() { if [ -z "$1" ]; then echo "请提供端口号作为参数" return 1 fi ss -tunlp | grep ":$1" } function killport() { if [ -z "$1" ]; then echo "需要一个端口号作为参数" return 1 fi kill -9 $(lsof -t -i:"$1") } function ps-mem-top6() { ps aux --sort=-%mem | awk '{print $2, $11, $4, $6/1024 "MB"}' | head -n 6 } # node alias npminstall='npm install -g --registry=https://registry.npmmirror.com ' # python # 定义 pipinstall 函数 function pipinstall() { if [ -z "$1" ]; then echo "需要一个包名作为参数" return 1 fi pip install "$1" -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com } # 文件查找相关 function findByName() { local pattern=$1 find . -name "*$pattern*" } function findByContent() { local pattern=$1 find . -type f -exec grep -l "$pattern" {} + } # 备份 function tar_back() { time=$(date "+%Y-%m-%d_%H_%M_%S") tar -zcvf "${1}".${time}.tgz "$1" ll -h ${1}.${time}.tgz } pid-cmd-centos() { sudo ls -l /proc/$1/cwd | awk '{print $11}' sudo cat /proc/$1/cmdline echo '' } pid-cmd-ubuntu() { ls -l /proc/"$1"/cwd | awk '{print $11}' tr '\0' ' ' ~/.config/micro/bindings.json { "Alt-q": "Quit" } EOF cd - fi ~/micro "$@" } # 磁盘读写速度测试 function my_disk_speed_test() { # 首先同步缓存,确保测试准确 sync echo 3 >/proc/sys/vm/drop_caches echo "测试写入" # 测试写入速度 (写入1GB数据) dd if=/dev/zero of=./testfile bs=1M count=1024 conv=fdatasync echo "测试读取" # 测试读取速度 dd if=./testfile of=/dev/null bs=1M count=1024 # 测试完成后删除测试文件 rm ./testfile } # 获取未使用的端口 function my_unused_port() { for port in {8000..65535}; do if ! ss -tuln | grep -q ":$port "; then echo $port break fi done } # 查看当前挂载目录文件大小,不查看其他挂载目录 function my_mounted_dir_size() { # 默认当前目录 if [ -z "$1" ]; then dir="." else dir="$1" fi du -hx --max-depth=1 ${dir} | sort -hr } # 设置终端标题 set_title() { printf "\033]2;%s\007" "$1" }