shell 工具函数

 平时管理 linux 系统,一遍又一遍重复命令,太枯燥了。于是自己封装了一些好用的函数。

不想直接修改 /etc/bash.bashrc 文件,于是通过 source 引入我自定义的 bashrc.sh 文件。

$vim  /etc/bash.bashrc

# 最后加上引用文件

source /e/develop/shell/bashrc.sh#!/bin/bash

#!/bin/bash

#环境变量
programs=/usr/local
php=$programs/php
zookeeper=$programs/zookeeper
redis=$programs/redis
JAVA_HOME=$programs/java8


PATH=$PATH:$JAVA_HOME/bin:$php/bin:$zookeeper/bin:$redis/bin

#history 配置
export HISTSIZE=1000; HISTTIMEFORMAT='%F '; HISTCONTROL="ignoredups"
export HISTIGNORE="ls:ll:pwd:history"

alias dir-mod="sudo chmod -R"
alias reload="source /etc/bash.bashrc"
alias command-list="compgen -ac"

### 解压文件 ###
alias tar-programs="sudo tar -C $programs -xvf"

### 切换目录 ###
alias cd-programs="cd /usr/local"
alias cd-download="cd ~/Downloads"

alias search="apt search"
alias install='sudo apt install -y'
alias upgrade='sudo apt upgrade -y'
alias purge='sudo apt-get purge'
alias update="command 'sudo apt update && apt list --upgradable'"
alias autoclean="command 'sudo apt-get autoremove && sudo apt-get autoclean'"


#解压到 Downloads 目录,并删除压缩包,切换到Downloads 目录
function tar-download(){

    local file="$1"
    
    cd ~/Downloads  && /
    tar -C ~/Downloads -xvf $file && /
    trash ./$file && /
}

#写入日志
function log(){
    local file="$1"
    local text="$2"
    if [ -n "$text" ]; then
        text=$file
        file='debug'
    fi
    echo -e "$text" >> /tmp/"$file.log";
}

function red(){ echo -e "33[31m$133[0m";}
function green(){ echo -e "33[32m$133[0m";}

function command(){
    local command="$1";local param1="$2"; local param2="$3"
    red ">> $command $param1 $param2"
    eval $command $param1 $param2
}

# 打印参数列表
function args(){

    local index=1
    for arg in $@; do
        echo "arg${index}: '$arg'"
        let index+=1
    done

}



### 系统管理 ###
function it(){

    local cmd="$1"
    local option="$2"

    case "$cmd" in
        'system-update') command 'sudo update-manager -d';;
        'ps')   command 'ps -ef | grep' $option
                command 'sudo netstat -nltp | grep' $option
        ;;
        'change-shell')  sudo dpkg-reconfigure dash;;

        'nginx')     
                if [ "$option" = 'start' ];then
                    command "sudo nginx"
                else
                    command "sudo nginx -s $option"
                fi
        ;;
        'apache')   command "sudo apache $option";;
        'mysql')    command "sudo service mysql $option";;
        'pkill')    command "sudo pkill -9 $option" ;;
        'php')
                    case "$option" in
                        'start')  command "sudo php-fpm";;
                        'stop')   command "sudo pkill -9 php-fpm";;
                    esac
        ;;

        *)          echo "未知命令"
        ;;
    esac

}



### 打开文件 ###
function open(){

    local option="$1"
    local param="$2"
    case "$option" in
        'hosts')    command "code /etc/hosts" ;;
        'vhost')    command "code /usr/local/apache/conf/extra/httpd-vhosts.conf" ;;
        'php')      command "code $php/lib/php.ini";;
        'apache')   command "code /usr/local/apache/conf/httpd.conf" ;;
        'nginx')    command "code /usr/local/nginx/conf/nginx.conf" ;;
        'readme')   command "code README.md" ;;
        'php-fpm')  command "code $php/etc/php-fpm.conf" ;;
        'log')      
                if [ -z "$param" ]; then
                    param='debug'
                fi
                command "code /tmp/$param.log" #打开日志
        ;;

        *)          
            local mime=`file --mime-type $option |awk '{print $2}' `

            if [ -n "`echo $mime |grep '^text/'`" ];then
                code $option
            elif [ "$mime" != 'cannot' ]; then
                red "$mime: 非文本类型";
            else
                green "新建文件: $option"
                code $option
            fi
        ;;
    esac
   
}


function show(){

    local name="$1"
    local option="$2"

    case "$option" in
        '-v'|'-V'|'--version'|'-version'|'version')
            command $name '--version' || command $name '-v' || command $name '-V' || command $name '-version' || command $name ;;
        '-h'|'-H'|'--help'|'-help'|'help')
            command $name '--help' || command $name '-h' || command $name '-?'  || command $name '-help' || command $name ;;
        
        *)
            red "当前命令:"
            which $name
            red "命令列表:"
            type -a $name
            red "相关目录"
            whereis $name
        ;;
            
    esac

}


function auto-install(){

    local cmd="$1"

    case "$cmd" in
        'init')
            sudo apt-get purge firefox firefox-* && 
            sudo apt install net-tools tcl-tls gcc curl && 
            sudo apt-get install libpcre3 libpcre3-dev && 
            sudo apt-get install zlib1g-dev && 
            sudo apt-get install apt-transport-https  ca-certificates  gnupg-agent software-properties-common
        ;;
        'docker')

            sudo apt-get install docker docker-engine docker.io containerd runc && 
            sudo usermod -aG docker $USER && 
            docker --version
        ;;
        'docker-compose')
            sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && 
            sudo chmod +x /usr/local/bin/docker-compose && 
            docker-compose --version
        ;;
        'redis')

            local name='redis-6.0.0'
            sudo wget "http://download.redis.io/releases/${name}.tar.gz" && 
            tar -xvf ${name}.tar.gz && 
            trash ${name}.tar.gz && 
            cd $name && 
            make BUILD_TLS=yes && 
            sudo make PREFIX=$programs/redis install
        ;;
        *) red 'error!';;
    esac

}



########################################################################


function dock-image(){

    local cmd="$1"
    local param="$2"

    case "$cmd" in
        'clean')
            command 'docker image rm' '$(docker image ls -f "dangling=true" -q)' ;;
    esac

}

function dock-volume(){

    local cmd="$1"
    local param="$2"

    case "$cmd" in
        'rm')
            if [ -z "$param" ];then
                param='$(docker volume ls -q)'
            fi
            command "docker volume rm" "$param" ;;
        'clean')
            command "docker volume prune" ;;
    esac
}


function dock(){

    local cmd="$1"
    local param="$2"

    case "$cmd" in
        'stop')
            if [ -z "$param" ];then
                param='$(docker ps -q)'
            fi
            command "docker stop" "$param" ;;
        'start')
            if [ -z "$param" ];then
                param='$(docker container ls -aq)'
            fi
            command "docker container start" "$param" ;;
        'ip')
            if [ -z "$param" ];then
                param='$(docker container ls -aq)'
            fi
            command "docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'" "$param" ;;
        'rm')
            if [ -z "$param" ];then
                param='$(docker container ls -aq)'
            fi
            command "docker container rm -f" "$param" ;;
        'exec')
            command "docker container exec -it $param /bin/bash" ;;

    esac

}



#批量运行 redis 服务
function redis-start(){
    local start=$1
    local end=$2
    local dir=$3
    if [ -z "$dir" ];then
            dir=''
    fi
    while(( $start<=$end ))
    do
        redis-server $dir$start.conf 
        let "start++"
    done

}
原文地址:https://www.cnblogs.com/zbseoag/p/12815486.html