zoukankan      html  css  js  c++  java
  • 设置mac默认使用GNU工具,比如使用GNU cat而不是BSD cat

    mac和linux都是unix-like OS, 命令是有很多相似之处, 但也有很多不同之处. 比如cat命令, mac不支持-A选项. 使用起来不够方便. 这是因为mac的命令工具来自于BSD, 而linux默认是GNU. 让我决心换到GNU工具的是sed, 同样的sed命令, mac就运行不成功, 这是因为GNU实现了很多扩展. 类似情况的还有grep, 或许还有awk, 但awk我用得不多, 没感觉到区别.

    好了, 废话不多说, 直接说怎么从mac的工具转到linux工具.

    第一种方法(维护起来麻烦, 不建议)

    1. brew install coreutils findutils gnu-tar gnu-sed gawk gnutls gnu-indent gnu-getopt grep. 其实在做完这步以后, 不喜欢折腾的人可以不做下一步, 因为gcat就可以用GNU cat了. 类似有ggrep. 但是我同时装了linux虚拟机, 有时候需要shell脚本既在mac上执行, 又在linux上执行, 希望名称上可以更统一. 于是做下一步:
    2. 在~/.bash_profile中添加
    gnu_path="/usr/local/opt/coreutils/libexec/gnubin:/usr/local/opt/findutils/libexec/gnubin:/usr/local/opt/gnu-tar/libexec/gnubin:/usr/local/opt/gnu-sed/libexec/gnubin:/usr/local/opt/gawk/libexec/gnubin:/usr/local/opt/gnu-indent/libexec/gnubin:/usr/local/opt/grep/libexec/gnubin"
    PATH=$gnu_path:$PATH
    MANPATH="/usr/local/opt/coreutils/libexec/gnuman:/usr/local/opt/findutils/libexec/gnuman:/usr/local/opt/gnu-tar/libexec/gnuman:/usr/local/opt/gnu-sed/libexec/gnuman:/usr/local/opt/gawk/libexec/gnuman:/usr/local/opt/gnu-indent/libexec/gnuman:/usr/local/opt/grep/libexec/gnuman":$MANPATH
    
    export PATH
    export MANPATH
    

    打开的新的session, readlink -e which cat, 以及man cat, 应该就是GNU了

    第二种方法(初始设置麻烦一些, 但是后续添加非常方便)

    对于bin和man page分别用不同的方法

    bin
    #!/usr/bin/env bash
    print_usage() {
      cat <<- EOF
    use gnu tools without prefix "g" on mac, for example, "cat" rather than "gcat"
    EOF
    exit 0
    }
    if [[ $1 =~ -h|--help ]]; then
    	print_usage
    fi
    gnu_bin_path=/usr/local/gnubin
    if [[ ! -d $gnu_bin_path ]]; then
    	mkdir $gnu_bin_path
    fi
    for i in /usr/local/opt/*/libexec/gnubin/*
    do
    	ln -s $i $gnu_bin_path
    done
    echo "finish"
    

    sudo bash执行这段脚本.

    然后在~/.bash_profile中添加:

    PATH=/usr/local/gnubin/:$PATH
    export PATH
    
    man page

    添加这段脚本在~/.bash_profile中:

    join_str() {
    	sep="$1"
    	shift
    	first_word="$1"
    	shift
    	printf "%s" "$first_word" "${@/#/$sep}"
    }
    
    manpath=(/usr/local/opt/**/libexec/gnuman/)
    joined_manpath=`join_str : ${manpath[@]}`
    MANPATH=$joined_manpath:$MANPATH
    export MANPATH
    unset manpath
    unset joined_manpath
    
    如何维护?

    man page不需要做什么, 对于bin, 再次执行上面的脚本就可以了.

  • 相关阅读:
    【BZOJ】1710: [Usaco2007 Open]Cheappal 廉价回文
    【BZOJ】3302: [Shoi2005]树的双中心 && 2103: Fire 消防站 && 2447: 消防站
    【BZOJ】1706: [usaco2007 Nov]relays 奶牛接力跑
    【Atcoder】CODE FESTIVAL 2017 qual A D
    【BZOJ】3038: 上帝造题的七分钟2 && 3211: 花神游历各国
    【BZOJ】1707: [Usaco2007 Nov]tanning分配防晒霜
    【BZOJ】1754: [Usaco2005 qua]Bull Math
    【BZOJ】1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
    【BZOJ】1828: [Usaco2010 Mar]balloc 农场分配(经典贪心)
    【BZOJ】1709: [Usaco2007 Oct]Super Paintball超级弹珠
  • 原文地址:https://www.cnblogs.com/Tokubara/p/14319342.html
Copyright © 2011-2022 走看看