自动补全命令,就是在我们输入命令的时候使用Tab键可以补全命令,或展示可能的命令

如nginx目录下

我们看到有以下文件

1. 只有一条命令满足的情况

 输入部分指令后,按下tab键,如果只有一个命令满足,则会补全完整的命令

2. 有多条命令满足的情况

 此时我们按下Tab键,不会完成补全,这是如果我们连续按下两次Tab键,就会把所有复合的命令都展示出来

 这时我们继续完善命令即可。在完善时可以充分使用Tab键自动补全

如何补全我们自己写的脚本命令呢?

我们找一个场景吧:

一个服务器上启动多个tomcat

在x_install文件夹中放了多个服务

 

1.首选需要我们自己编写命令脚本:

这里我们用tomcat的命令脚本举例,包含了启动、暂停、重启、状态、日志等功能

#!/bin/bash
BUILD_ID=DONTKILLME
TOMCAT_HOME=/opt/tomcat
serviceNames=$(ls /opt/tomcat/x_install/  |grep Service)
commands='start stop restart status log errlog'
RETVAL="0"

serviceName=$1
if [ x"$1" = x ]; then 
	echo '请输入服务名:'$serviceNames
	exit 1
fi

isService=$(echo $serviceNames | grep "${serviceName}")
if [[ "$isService" = "" ]]; then
	echo '无效服务名'
	echo '服务名:'$serviceNames
	exit 1
fi

if [ x"$2" = x ]; then 
	echo '请输入执行命令:'$commands
	exit $RETVAL
fi

isCommand=$(echo $commands | grep "$2")
if [[ "$isCommand" = "" ]]; then
	echo '无效执行命令'
	echo '命令:'$commands
	exit $RETVAL
fi

export CATALINA_HOME=$TOMCAT_HOME
export CATALINA_BASE=$CATALINA_HOME/x_install/$serviceName

num=0
if [ x"$3" != x ]; then
num=$3
fi


start() {
ps aux |grep $CATALINA_BASE/conf |grep -v "grep" >/dev/null
if [ $? -eq 0 ];then
   echo  $serviceName" is running"
else      
   echo "tomcat is down"
   
   find / |grep "/jre/lib/rt.jar" |grep -v "grep" >/dev/null
	if [ $? -eq 0 ];then
		javahome=$(find / |grep "/jre/lib/rt.jar" |head -1 |awk '{print $1}')
		export JRE_HOME=${javahome%*/lib/rt.jar}
		export PATH=$JRE_HOME/bin:$PATH
		export CLASSPATH=.:$JRE_HOME/lib
		if [[ "$serviceName" = "ReviewService" ]]; then
			echo $serviceName' 设置运行内存1024m'
			export JAVA_OPTS='-Xms128m -Xmx1024m'
		elif [[ "$serviceName" = "BiddingService" ]]; then
			echo $serviceName' 设置运行内存1024m'
			export JAVA_OPTS='-Xms128m -Xmx1024m'
		elif [[ "$serviceName" = "OpenBiddingService" ]]; then
			echo $serviceName' 设置运行内存1024m'
			export JAVA_OPTS='-Xms128m -Xmx1024m'
		else
			export JAVA_OPTS='-Xms128m -Xmx512m'
		fi
	else
		  echo "未找到java环境"
		  exit $RETVAL
	fi
   
   if [ $num != 0 ]; then
   echo "开启调试:端口[$num]"
	declare -x CATALINA_OPTS="-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$num" 
   fi
   
   $CATALINA_HOME/bin/startup.sh
   if [ $? -eq 0 ];then
      echo $serviceName" start successfull"
   else
      echo $serviceName" start fail" 
   fi
fi
}

stop(){
  
  ps aux |grep $CATALINA_BASE/conf |grep -v "grep" >/dev/null
  if [ $? -eq 0 ];then
      echo  $serviceName" is running"
      echo "now start stop "$serviceName
	  $CATALINA_HOME/bin/shutdown.sh
      tomcatpid=$(ps aux |grep $CATALINA_BASE/conf |grep -v "grep"|awk '{print $2}')
	  if [ x"$tomcatpid" != x ]; then
	    kill -9 $tomcatpid
	  fi
      echo $serviceName" already stoped"
  else
      echo $serviceName" is stoped"
  fi
}

log(){
	if [ $num != 0 ]; then
		tail -n $num $CATALINA_BASE/logs/catalina.out
	else
		tail -f $CATALINA_BASE/logs/catalina.out
	fi
}

errlog(){
	if [ $num != 0 ]; then
		tail -n $num $CATALINA_BASE/logs/catalina.out |grep Exception
	else
		tail -f $CATALINA_BASE/logs/catalina.out |grep Exception
	fi
}

status(){
  ps aux |grep $CATALINA_BASE |grep -v "grep" >/dev/null
  if [ $? -eq 0 ];then
      echo  $serviceName" is running"
  else
      echo $serviceName" is stoped"
  fi
}

# See how we were called.

usage()
{
   echo "Usage: $1{服务名称} $2 {start|stop|restart|status|log|errlog}"
   RETVAL="2"
}

case "$2" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    log) 
        log 
        ;;
    errlog) 
        errlog 
        ;;
    *)
      usage
      ;;
esac

exit $RETVAL

把脚本放到我们自己自定义命令的目录下

2.在任意目录下可以访问:

我们可以选择使用软连接的方式

#ln -s /opt/tomcat/tomcat.sh /usr/sbin/tomcat

到这里,  你就可以在任意目录下 使用  tomcat 命令了

到这里我们使用Tab键,只能补全对应文件夹下的文件,不满足我们的要求

3.编写bash_completion脚本:

_tomcat()
{
  local cur prev serviceNames commands
  COMPREPLY=()

  cur="${COMP_WORDS[COMP_CWORD]}"
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  serviceNames=$(ls /opt/tomcat/x_install/  |grep Service)
  commands='start stop restart status log errlog'

  serviceNames_str=`echo ${serviceNames[@]} | sed 's/ /\n/g'| sort -u`
  if ((COMP_CWORD == 1)); then
	COMPREPLY=( $(compgen -W "${serviceNames_str[*]}" -- $cur) )
  fi
  
  if ((COMP_CWORD == 2)); then
	COMPREPLY=( $(compgen -W "${commands[*]}" -- $cur) )
  fi
}
complete -F _tomcat tomcat

4. 切换目录

#cd /etc/bash_completion.d


上传脚本文件 tomcat.bash

5. 生效自动补全脚本

#source /usr/share/bash-completion/bash_completion

6. 这时就可以自动提示我们的命令了

 tomcat 命令后  第一个参数会提示指定目录的文件夹,在自定脚本中设置的serviceNames

 第二个参数会提示需要的命令

 

 

 详解:

_tomcat   定义函数名

cur="${COMP_WORDS[COMP_CWORD]}"       我们正在输入的词

prev="${COMP_WORDS[COMP_CWORD-1]}"    命令中上一个词

serviceNames  存放我们需要自动补全的服务(文件夹名称)

commands   可以使用的命令集合

COMP_CWORD  第几个词

COMPREPLY=( $(compgen -W "${serviceNames_str[*]}" -- $cur) )  输出或自动补全

complete -F _tomcat tomcat    指定当命令为tomcat时,使用_tomcat 补全命令

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐