alias 功能固然好用,但在快捷方便的同时,隐藏了原始命令的结节。久而久之,不记得原始命令是什么了,因些,我想到了实现原命令输出,以作为提示。先看效果图吧。
/etc/bash.bashrc 文件追加代码如下:
#环境变量 PASSWORD=root PROGRAM=/usr/local PACKAGE=/e/package PHP=$PROGRAM/php ZOOKEEPER=$PROGRAM/zookeeper REDIS=$PROGRAM/redis PATH=$PATH:$PHP/bin:$ZOOKEEPER/bin:$REDIS/bin #history 配置 export HISTSIZE=1000; HISTTIMEFORMAT='%F '; HISTCONTROL="ignoredups" export HISTIGNORE="ls:ll:pwd:history" alias reload="source /etc/bash.bashrc" alias command-list="compgen -ac" alias on="execute 'reboot'" alias off="execute 'poweroff'" ### 切换目录 ### alias cd-program="cd /usr/local" alias cd-package="cd $PACKAGE" alias cd-test="cd /e/develop/test" alias search="execute 'apt search'" alias update="execute 'sudo apt update && apt list --upgradable'" alias upgrade="execute 'sudo apt -y upgrade'" alias dpkg-list="dpkg --list | grep" alias remove="execute 'sudo apt -y --purge remove'" alias autoremove="execute 'sudo apt autoremove && sudo apt autoclean'" function color(){ local color="$1" local colors=("-red", "-green", "-yellow", "-blue", "-white", "-black") if [[ "${colors[@]}" =~ $color ]];then shift 1; fi case "$color" in '-red') echo -e "\033[31m$@\033[0m";; '-green') echo -e "\033[32m$@\033[0m";; '-yellow') echo -e "\033[33m$@\033[0m";; '-blue') echo -e "\033[34m$@\033[0m";; '-white') echo -e "\037[32m$@\033[0m";; '-black') echo -e "\033[30m$@\033[0m";; *) echo -e "$@";; esac } function execute(){ color -red ">> $@"; eval "$@"; } #写入日志 function log(){ local file="$1" local text="$2" if [ -n "$text" ]; then text=$file file='debug' fi echo -e "$text" >> /tmp/"$file.log"; } # 打印参数列表 function args(){ local index=1 for arg in "$@"; do echo "arg${index}: '$arg'" let index+=1 done } ### 系统管理 ### function -system(){ local cmd="$1" local option="$2" case "$cmd" in 'system-update') execute 'sudo update-manager -d';; 'ps') execute 'ps -ef | grep' $option execute 'sudo netstat -nltp | grep' $option ;; 'change-shell') sudo dpkg-reconfigure dash;; *) echo "未知命令" ;; esac } function service(){ alias service=`which $FUNCNAME` local cmd="$1" local option="$2" case "$cmd" in 'pkill') execute "sudo pkill -9 $option" ;; 'nginx') if [ "$option" = 'start' ];then execute "sudo nginx" elif [ "$option" = 'restart' ];then execute "sudo nginx -s stop && sudo nginx" else execute "sudo nginx -s $option" fi ;; 'php' | 'php-fpm') case "$option" in 'start') execute "sudo php-fpm";; 'stop') execute "sudo pkill -9 php-fpm";; 'restart') execute "sudo pkill -9 php-fpm && sudo php-fpm";; esac ;; *) eval sudo service "$@" ;; esac unalias $FUNCNAME } ### 打开文件或目录 ### function open(){ option="$1" param="$2" if [ -z "$option" ];then option=$PWD; fi if [ "$option" == "-conf" ];then case "$param" in 'hosts') execute "code /etc/hosts" ;; 'php') execute "code $PHP/lib/php.ini";; 'php-fpm') execute "code $PHP/etc/php-fpm.conf" ;; 'nginx') execute "code /usr/local/nginx/conf/nginx.conf" ;; esac else mime=`file --mime-type $option | awk '{print $2}'` #如果是符号链接 if [ $mime == 'inode/symlink' ];then option=`readlink -e $option` mime=`file --mime-type $option | awk '{print $2}'` fi if [ -n "`echo $mime | grep -E '^(text/|inode/x-empty)'`" ];then code $option elif [ "$mime" = 'inode/directory' ];then nautilus $option elif [ "$mime" != 'cannot' ]; then #默认方式打开 xdg-open $option else color -green "新建文件: $option" code $option fi fi } function show(){ local name="$1" local option="$2" #默认为 system 内容 if [ -z "$option" ];then option=$name name="system" fi if [ "$name" == "system" ];then case "$option" in '-version') execute 'lsb_release -a' ;; '-zombie') execute "ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]'" ;; "-status") execute 'sudo systemctl status' ;; *) execute 'lsb_release -a' ;; esac else case "$option" in '-version') execute $name '--version' || execute $name '-v' || execute $name '-V' || execute $name '-version' || execute $name ;; *) execute $name '--help' || execute $name '-h' || execute $name '-?' || execute $name '-help' || execute $name ;; esac fi } function where(){ local name="$1" color -red "当前命令:" && which $name color -red "命令列表:" && type -a $name color -red "相关目录:" && whereis $name } function questions(){ local question="$1" case "$question" in 'settings') #系统设置打不开 sudo apt-get install gnome-control-center #设置里只有很少的几个图标 sudo apt-get install unity-control-center ;; esac #open /etc/xdg/user-dirs.defaults #open ~/.config/user-dirs.dirs } #批量运行服务 #start "redis-server" 100 105 .conf function start(){ local server=$1 local start=$2 local stop=$3 local ext=$4 if [ -z "$ext" ];then local ext=".conf"; fi if [ -z "$stop" ];then local stop=$start; fi while(( $start <= $stop )) do execute "$server $start$ext" let "start++" done } function install(){ software=$1 ext=${software##*.} if [ "$ext" == "deb" ];then execute 'sudo dpkg -i' $software else execute 'sudo apt install -y' $software fi } function get-install(){ software=$1 cd $PACKAGE case "$software" in '-h' | '-help' | '--help') echo "安装列表:docker docker-compose redis lua zookeeper" ;; '-init' | '--init') 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 package='redis-6.0.0.tar.gz' sudo wget "http://download.redis.io/releases/${package}" && tar -xvf $package && trash $package && cd $(echo $package | cut -d . -f1)* && make BUILD_TLS=yes && sudo make PREFIX=$PROGRAM/redis install ;; 'lua') local package='lua-5.4.0.tar.gz' curl -R -O http://www.lua.org/ftp/${package} && \ tar -xvf $package && trash $package && cd $(echo $package | cut -d . -f1)* && make all test ;; 'mysql') sudo apt update && sudo apt install mysql-server && sudo systemctl status mysql && sudo mysql_secure_installation && sudo mysql #ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password'; #GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password'; ;; "zookeeper") local package='apache-zookeeper-3.6.2-bin.tar.gz' curl -R -O https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.2/${package} && \ tar -xvf $package && trash $package && cd $(echo $package | cut -d . -f1)* ;; *) color -red "没有安装步骤" ;; esac } function docker(){ #获取系统中的命令位置赋值给一个别名,后面调用这个别名,就是系统调用,$FUNCNAME 是当前函数名 alias docker=`which $FUNCNAME` local cmd="$1" local param="$2" case "$cmd" in 'stop') if [ -z "$param" ];then param='$(docker ps -q)' fi execute "docker stop" "$param" ;; 'start') if [ -z "$param" ];then param='$(docker container ls -aq)' fi execute "docker container start" "$param" ;; 'pid') if [ -z "$param" ];then param='$(docker container ls -q)' fi execute "docker inspect --format '{{.Name}}: {{ .State.Pid }}'" "$param" ;; 'ip') if [ -z "$param" ];then param='$(docker container ls -q)' fi execute "docker inspect --format='{{.Name}}: {{ .NetworkSettings.IPAddress }}'" "$param" ;; 'rm') if [ "$param" == 'all' ];then param='$(docker container ls -aq)' fi execute "docker container rm -f" "$param" ;; 'image-clear') execute 'docker image rm' '$(docker image ls -f "dangling=true" -q)' ;; 'exec') execute "docker container exec -it $param /bin/bash" ;; 'evn') execute "docker run $param env" ;; *) #调用系统命令 eval docker "$@" ;; esac #再把别名给干掉 unalias $FUNCNAME }
联系客服