欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

linux alias 命令别名并输出其原始命令

程序员文章站 2022-06-22 15:58:16
alias 功能固然好用,但在快捷方便的同时,隐藏了原始命令的结节。久而久之,不记得原始命令是什么了,因些,我想到了实现原命令输出,以作为提示。先看效果图吧。 /etc/bash.bashrc 文件追加代码如下: ### 开始自定义 ### #环境变量 PROGRAMS=/usr/local PHP ......

alias 功能固然好用,但在快捷方便的同时,隐藏了原始命令的结节。久而久之,不记得原始命令是什么了,因些,我想到了实现原命令输出,以作为提示。先看效果图吧。

 

linux alias 命令别名并输出其原始命令

 

/etc/bash.bashrc 文件追加代码如下:
### 开始自定义 ###

#环境变量
programs=/usr/local
php=$programs/php7.4
zookeeper=$programs/zookeeper
java_home=$programs/java8

path=$path:$java_home/bin:$php/bin:$zookeeper/bin

#history 配置
export histsize=1000; histtimeformat='%f '; histcontrol="ignoredups"
export histignore="ls:ll:pwd:history"

#定义编辑器
alias open="code"
alias open-bashrc="command 'code /etc/bash.bashrc' && green '>> [ctrl + win + o]'"
alias close-bashrc="command 'source /etc/bash.bashrc'"
alias open-phpini="open  $php/lib/php.ini"
alias open-readme="open readme.md"

alias tar-download="tar -c ~/downloads -xvf"
alias tar-programs="sudo tar -c $programs -xvf"
alias tar-it="tar -xvf"

#查看
alias port="command 'sudo netstat -nltp | grep'"
alias process="command 'ps -ef | grep'"


#切换目录
alias cd-soft="cd /mnt/hgfs/soft"
alias cd-develop="cd /mnt/hgfs/develop"
alias cd-programs="cd /usr/local"
alias cd-download="cd ~/downloads"

#apache
alias apache-start="sudo apachectl start"

#mysql
alias mysql-stop="sudo service mysqld stop"
alias mysql-start="sudo service mysqld start"

#nginx
alias nginx-start="sudo nginx -s start"
alias nginx-stop="sudo nginx -s stop"


#停止多个相同进程
function kills(){
	ps -ef| grep $1 |grep -v grep |awk '{print $2}' | xargs kill -9
}

#批量运行 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

}


function docker-image(){

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

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

}

function docker-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 docker-container(){

	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
            echo $param
            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

}


function red(){ echo -e "\033[31m$1\033[0m";}
function green(){ echo -e "\033[32m$1\033[0m";}

function command(){

	local command="$1" 
	local param="$2"

	red ">> $command $param"
	eval $command $param
}

#非标准的查看版本写这里
function version(){
	local name="$1"
	local version='--version'
	case "$name" in
		'java' ) version='-version';;
	esac
	command $name $version
}