sed command with -i option failing on Mac, but works on Linux

前端 未结 12 2102
情歌与酒
情歌与酒 2020-11-22 05:04

I\'ve successfully used the following sed command to search/replace text in Linux:

sed -i \'s/old_link/new_link/g\' *

However,

12条回答
  •  萌比男神i
    2020-11-22 05:55

    Here is an option in bash scripts:

    #!/bin/bash
    
    GO_OS=${GO_OS:-"linux"}
    
    function detect_os {
        # Detect the OS name
        case "$(uname -s)" in
          Darwin)
            host_os=darwin
            ;;
          Linux)
            host_os=linux
            ;;
          *)
            echo "Unsupported host OS. Must be Linux or Mac OS X." >&2
            exit 1
            ;;
        esac
    
       GO_OS="${host_os}"
    }
    
    detect_os
    
    if [ "${GO_OS}" == "darwin" ]; then
        sed -i '' -e ...
    else
        sed -i -e ...
    fi
    

提交回复
热议问题