sed -i command for in-place editing to work with both GNU sed and BSD/OSX

后端 未结 7 1147
攒了一身酷
攒了一身酷 2020-11-28 04:09

I\'ve got a makefile (developed for gmake on Linux) that I\'m attempting to port to MacOS, but it seems like sed doesn\'t want to cooperate. What I

7条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 04:35

    I came across this issue as well and thought of the following solution:

    darwin=false;
    case "`uname`" in
      Darwin*) darwin=true ;;
    esac
    
    if $darwin; then
      sedi="/usr/bin/sed -i ''"
    else
      sedi="sed -i"
    fi
    
    $sedi 's/foo/bar/' /home/foobar/bar
    

    Works for me ;-), YMMV

    I work in a multi-OS team where ppl build on Windows, Linux and OS X. Some OS X users complained because they got another error - they had the GNU port of sed installed so I had to specify the full path.

提交回复
热议问题