ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[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法|简单,明了 |如果要实现复杂的,将要写脚本,在赋别名,不好管理| |函数法|管理方便,实现简单,函数在终端启动时就加载完毕|过多的函数可能造成启动终端较慢,单应该没有明显的迟钝|