diff --git a/source/_posts/linux/2.3、shell(4)-使用技巧.md b/source/_posts/linux/2.3、shell(4)-使用技巧.md index c12a9bd..576a1b6 100644 --- a/source/_posts/linux/2.3、shell(4)-使用技巧.md +++ b/source/_posts/linux/2.3、shell(4)-使用技巧.md @@ -26,4 +26,50 @@ while read LINE do echo $LINE done < test.txt -``` \ No newline at end of file +``` + +#### 通用解压函数 +`$1`表示函数接收到的第一个参数 +```bash +extract() { + if [ -f $1 ] ; then + case $1 in + *.tar.bz2) tar xjf $1 ;; + *.tar.gz) tar xzf $1 ;; + *.bz2) bunzip2 $1 ;; + *.rar) unrar e $1 ;; + *.gz) gunzip $1 ;; + *.tar) tar xf $1 ;; + *.tbz2) tar xjf $1 ;; + *.tgz) tar xzf $1 ;; + *.zip) unzip $1 ;; + *.Z) uncompress $1 ;; + *.7z) 7z x $1 ;; + *) echo "$1 cannot be extracted via extract()" ;; + esac + else + echo "$1 is not a valid file" + fi +} +``` +执行`extract 文件名`就可以解压任意格式的压缩文件了, 比如**extract nodejs.tar.gz** +可以把上述函数添加到`~/.bashrc`当中, 使用bash作为shell的时候会自动先执行该文件 +这样每次都可以使用了 +一些命令的别名, 也可以配置在这里面 + +#### 命令的别名 +`alias`命令用于给指定的命令组合指定别名 +比如 +```bash +alias ls='ls --color=auto' +alias ll="ls --color -al" +alias grep='grep --color=auto' + +# 查看当前时间 +alias now='date "+%Y-%m-%d %H:%M:%S"' +``` +此时执行`ll`就相当于执行`ls --color -al` + +如果别名覆盖了原本的命令 ( 比如上面的ls ) +想使用原本的命令可以在前面加`\` +也就是`\ls` \ No newline at end of file