Shell的技巧与窍门

在~/.bashrc中添加

#cd和ls两条命令合二为一;并在指定目录不存在时显示错误信息。
cl() {
        local dir="$1"
        local dir="${dir:=$HOME}"
        if [[ -d "$dir" ]];then
                cd "$dir" > /dev/null; ls -al
        else
                echo "bash: cl: $dir: Directory not found"
        fi
}

#显示错误代码,设置trap拦截最后一个程序运行的非零返回码
EC() {
       echo -e '\e[1;33m'code $?'\e[m\n'

}
trap EC ERR

#仅输入路径时自动切换到目录
shopt -s autocd
#修复cd的拼写错误,仅在交互式shell中
#shopt -s cdspell
#如果系统管理员并经常使用root权限,不允许通过重定向shell输出覆盖现有的常规文件,
#shopt -s noclobber

#通过https://ipinfo.io获取bash中公网IP地址或主机名的详细信息
ipinfo() {
    if grep -P "(([1-9]\d{0,2})\.){3}(?2)" <<< "$1"; then
         curl ipinfo.io/"$1"
    else
        ipawk=($(host "$1" | awk '/address/ { print $NF }'))
        curl ipinfo.io/${ipawk[1]}
    fi
    echo
}
 

猜你喜欢

转载自blog.csdn.net/taoxicun/article/details/127304468