server-alias-install:快速为 Linux 系统配置自定义 alias 引用,并添加响应历史命令搜索的箭头上下键的inputrc配置

作用:一键操作,通过SSH为远程主机添加alias别名引用,免去手动添加自加载配置文件 ~/.bash_profile ~/.bashrc 的麻烦;通用的需要频繁使用的alias可以预先在本地模板文件/v/bin/server_alias内进行定义,脚本会推送到服务器后自动进行source引用。

另:自动修改~/.inputrc配置,以便自动响应上下箭头搜索历史命令,比如 输入 wget按向上箭头,终端窗口就会从历史命令中检索出上一条匹配wget的命令wget https://www.baidu.com -O /dev/null

#!/bin/bash 
SCRIPTPATH=$(realpath $0)
#SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
#SCRIPTPATH=$(dirname $(readlink -f "$0"))

display_usage() {
	echo -e "$SCRIPTPATH\n"
    echo -e "\t快速为 Linux 系统配置自定义 alias 引用."
	echo -e "\tserver-alias模板位置:/v/bin/server_alias "
    echo -e "\nUsage:\n\tserver-alias-install [hostname]"
	echo -e "Example:\n\tserver-alias-install racknerd"
}
# if less than two arguments supplied, display usage
if [  $# -lt 1 ]
then
    display_usage
    exit 1
fi

# check whether user had supplied -h or --help . If yes display usage
if [[ ( $* == "--help") ||  $* == "-h" ]]
then
    display_usage
    exit 0
fi

shFile=/tmp/server-alias.$$
trap "rm -f $shFile" 0

cat > $shFile <<EOF
#!/bin/bash
SCRIPTPATH=\$(realpath \$0)
trap "rm -f \$SCRIPTPATH" 0
grep -E "(\$HOME|~)/.server_alias" ~/.bashrc >/dev/null 2>&1
if [ \$? -ne 0 ];
then
  echo "source ~/.server_alias">>~/.bashrc
fi
EOF

chmod a+x $shFile
#cat $shFile
#exit 0 
ssh $1 'uname -a'
scp /v/bin/server_alias $1:~/.server_alias
#拷贝本地inputrc到远程服务器,以实现上下箭头搜索补全历史命令。Ctrl+左右箭头按单词为单位移动光标
scp /v/bin/server_inputrc $1:~/.inputrc
scp ~/.curlrc $1:~/.curlrc
scp $shFile $1:$shFile
ssh $1 'bash '$shFile
echo -e "Done..."

依赖的代码:
/v/bin/server_alias:

alias ll='ls -l --show-control-chars --color'
alias lt='ls -t --show-control-chars --color'
alias catuuid='cat /proc/sys/kernel/random/uuid'
alias novim='vim -u /dev/null'
alias wanip='curl -sS http://v.ynit.top/ipfull/'
alias installsz='yum --help &>/dev/null&&yum install -y lrzsz||apt-get install -y lrzsz'
alias installrz=installsz
alias installwget='yum --help &>/dev/null&&yum install -y wget||apt-get install -y wget'
alias installcurl='yum --help &>/dev/null&&yum install -y curl||apt-get install -y curl'
alias installrsync='yum --help &>/dev/null&&yum install -y rsync||apt-get install -y rsync'

/v/bin/server_alias:

# To the extent possible under law, the author(s) have dedicated all 
# copyright and related and neighboring rights to this software to the 
# public domain worldwide. This software is distributed without any warranty. 
# You should have received a copy of the CC0 Public Domain Dedication along 
# with this software. 
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. 

# base-files version 4.2-3

# ~/.inputrc: readline initialization file.

# The latest version as installed by the Cygwin Setup program can
# always be found at /etc/defaults/etc/skel/.inputrc

# Modifying /etc/skel/.inputrc directly will prevent
# setup from updating it.

# The copy in your home directory (~/.inputrc) is yours, please
# feel free to customise it to create a shell
# environment to your liking.  If you feel a change
# would be benifitial to all, please feel free to send
# a patch to the cygwin mailing list.

# the following line is actually
# equivalent to "\C-?": delete-char
"\e[3~": delete-char

# VT
"\e[1~": beginning-of-line
"\e[4~": end-of-line

# kvt
"\e[H": beginning-of-line
"\e[F": end-of-line

# rxvt and konsole (i.e. the KDE-app...)
"\e[7~": beginning-of-line
"\e[8~": end-of-line

# VT220
"\eOH": beginning-of-line
"\eOF": end-of-line

# Allow 8-bit input/output
set meta-flag on
set convert-meta off
set input-meta on
set output-meta on
# 忽略大小写 
set completion-ignore-case on

#$if Bash
  # Don't ring bell on completion
  #set bell-style none

  # or, don't beep at me - show me
  #set bell-style visible

  # Filename completion/expansion
  #set completion-ignore-case on
  #set show-all-if-ambiguous on

  # Expand homedir name
  #set expand-tilde on

  # Append "/" to all dirnames
  #set mark-directories on
  #set mark-symlinked-directories on

  # Match all files
  #set match-hidden-files on

  # 'Magic Space'
  # Insert a space character then performs
  # a history expansion in the line
  #Space: magic-space
#$endif


#shell 自动根据前缀查找补全历史命令
#http://blog.phpdr.net/shell-%e8%87%aa%e5%8a%a8%e6%a0%b9%e6%8d%ae%e5%89%8d%e7%bc%80%e6%9f%a5%e6%89%be%e5%8e%86%e8%a1%a5%e5%85%a8%e5%8f%b2%e5%91%bd%e4%bb%a4.html
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[1~": beginning-of-line
"\e[4~": end-of-line 
#以下配置左右箭头的动作。是以字符为单位跳转还是一单词为单位跳转
"\e[C": forward-char
"\e[D": backward-char
#"\e[C": forward-word
#"\e[D": backward-word
# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
# https://superuser.com/questions/488157/how-do-i-make-ctrl-arrow-keys-move-forward-backward-a-word-at-a-time-in-cygwin-b
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

posted @ 2021-11-29 14:52  晴云孤魂  阅读(89)  评论(0编辑  收藏  举报