[TOC]
## 說明
在使用linux時,我們有很多時候可以把自己經常用到的一些腳本做成自己的指令,這樣使得我們在用戶全局都可以使用自定義的指令,那么實現自定指令的方法有哪些呢,今天在這里根據自己的經驗稍微總結一下。 <br/>
## 方法一:環境變量法
熟悉linux的都知道,大部分發行版都會判斷用戶目錄下是否有`bin`目錄,如果有就會將這個目錄加入環境變量,也就是說,我們可以將一些腳本寫好放到這個目錄下,也就是`$HOME/bin`目錄下,這樣我們就可以在終端直接調用腳本了,上述判斷bin目錄是否存在的部分一般會放在`$HOME/.profile`,我的系統下這部分內容如下: <br/>
```bash
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ]; then
PATH="$HOME/bin:$PATH"
fi
```
<br/>
當然,如果之前目錄中沒有bin目錄,我們自己手動在`$HOME`目錄下建立`bin`,這個時候需要我們注銷后再登錄才可以,或者執行`source $HOME/.profile`。
## 方法二:alias方法
bash下我們可以使用`alias`來為一些簡單的命令定義別名,一般這個命令都放在`$HOME/.bashrc`文件下,如下所示: <br/>
```bash
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
#[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
#alias grep='grep --color=auto'
#alias fgrep='fgrep --color=auto'
#alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
#mywine
alias mwine='WINEPREFIX=/home/yhp/.mywine/deepin_mywine deepin-wine'
#個人華為云服務器
alias mhw_server='ssh yhp@139.159.243.162'
#掛載個人華為云服務器的 /home/yhp 文件夾到本地 /home/yhp/hw_work_space
alias mhw_mount='sshfs yhp@139.159.243.162:/home/yhp /home/yhp/hw_work_space'
alias mhw_umount='fusermount -u /home/yhp/hw_workspace'
#添加更人密碼到個人密碼管理庫
alias mpasswd='sudo bash ~/.hp_passwd/hp_passwd.sh'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# Set LS_COLORS environment by Deepin
if [[ ("$TERM" = *256color || "$TERM" = screen* || "$TERM" = xterm* ) && -f /etc/lscolor-256color ]]; then
eval $(dircolors -b /etc/lscolor-256color)
else
eval $(dircolors)
fi
```
## 方法三:建立fish shell一樣的函數機制
上面兩種方式不太好管理,而fish shell中自定義指令是使用函數的方式來實現的,一條自定義指令可以對應一個函數,于是我們可以借助fish shell的這種思路來在bash上實現。實現方法如下: <br/>
**建立放置函數的文件夾**
我的方法是在`$HOME`下建立`.bash_func`文件夾,可使用命令 `mkdir -p $HOME/.bash_func`實現。
<br/>
**在$HOME/.bashrc下添加加載函數的代碼**
在`$HOME/.bashrc`后面添加以下部分,
```bash
if [ -d "$HOME/.bash_func" ]
then
if [[ $(ls $HOME/.bash_func | wc -c ) -gt 0 ]];then
for flist in $(ls $HOME/.bash_func)
do
. $HOME/.bash_func/$flist
done
fi
fi
```
這樣以來,我們在`~/.bash_func/`下建立文件寫函數,啟動終端后,就能自動加載函數,之后就可以做為自定義的指令使用。 <br/>
比如,我們建立`t_func`文件,文件內容如下:
```
function t_func(){
echo "hello,this is a demo!"
}
```
然后我們新打開一個終端,注意要新打開,或者你可以先把shell切換到sh,再切換到bash,使得.vimrc文件被加載,然后我們終端輸入`t_func`可以看到效果! <br/>
需要注意的是,要寫成函數的形式,雖然一個文件中可以寫多個函數,但是建立一個文件寫一個函數,一個函數就是一條自定義指令,這樣方便管理!
## 總結比較
| 方法 | 優點 | 缺點|
|:----------:|:--------------:|:-------------:|
|環境變量法|管理方便,實現簡單|fork了子模塊,注定有些你想要實現的實現起來可能比較復雜|
|alias法|簡單,明了 |如果要實現復雜的,將要寫腳本,在賦別名,不好管理|
|函數法|管理方便,實現簡單,函數在終端啟動時就加載完畢|過多的函數可能造成啟動終端較慢,單應該沒有明顯的遲鈍|