zoukankan      html  css  js  c++  java
  • mac上sed -i 执行失败报错

    比如说我要替换version.txt文件中的version=1.1 为version=1.2,比如test.txt文件内容如下:

    version=1.1

    此时我们会使用sed来替换,如果是涉及比较多的处理,我们会采用脚本实现,比如sed_shell.sh脚本内容如下:

    #!/bin/bash

    if [ "x$1" == "x" ]; then
        echo please input new version && exit
    else
        old_version=`cat version.txt |grep version |awk -F "=" '{print $2}'` #获取老的版本号
        new_version=$1
        echo old_version=$old_version and new_version=$new_version
        sed -i s/$old_version/$new_version/g version.txt  #替换老版本号为新版本号
    fi

    linux环境下:执行sh sed_shell.sh "1.2" 命令就可以把verison.txt的老版本号换成新版本号。

    但是mac上执行就会报错“invalid command code C”,查看mac sed 发现如下:

    说白了,就是需要一个中间文件来转换下,比如我们上面的sed命令在mac上可以替换成sed -i  n.tmp s/$old_version/$new_version/g version.txt  ,其实执行这条的时候会生成一个version.txt_n.tmp文件,这个不需要的文件,执行后删除即可。

    我们可以采用uname命令来判断当前系统是不是mac,如果"$(uname)" == "Darwin",就表明是mac/ios系统。

    所以完整的同时兼容linux和mac/ios的脚本sed_shell.sh如下:

    #!/bin/bash

    if [ "x$1" == "x" ]; then #没有输入参数,报错退出
        echo please input new version && exit
    else
        old_version=`cat version.txt |grep version |awk -F "=" '{print $2}'`
        new_version=$1
        echo old_version=$old_version and new_version=$new_version
        if [ "$(uname)" == "Darwin" ];then #ios/mac系统
            echo "this is Mac,use diff sed"
            sed -i n.tmp s/$old_version/$new_verison/g version.txt  #如果不备份,可以只给空,即sed -i  " " s/$old_version/$new_verison/g version.txt ,但是不能省略
            rm *.tmp
        else
            sed -i s/$old_version/$new_version/g version.txt  #linux系统
        fi
    fi

     sed -i n.tmp s/$old_version/$new_verison/g version.txt 会导致新增一个文件version.txtn.tmp  可以把n.tmp换成空,

    即sed -i  " " s/$old_version/$new_verison/g version.txt ,就不会有新增文件

    另一种方法是在mac上安装gun-sed:

    export xsed=sed
    if [ "$(uname)" == "Darwin" ];then #mac系统
        echo "alias sed to gsed for Mac, hint: brew install gnu-sed"
        export xsed=gsed
    fi

    #后面使用xsed代替sed执行替换动作,

    xsed -i s/$old_version/$new_version/g version.txt

  • 相关阅读:
    18个Java开源CMS系统一览
    冒泡排序
    数据挖掘十大经典算法
    开源Java CMS建站程序推荐
    Oracle
    Oracle Procedure returning Ref Cursor in Entity Framework 4
    Field_II
    SharePoint Video Library
    ORACLE 导入dmp文件
    System.Diagnostics.Process.Start()。它的作用是调用外部的命令
  • 原文地址:https://www.cnblogs.com/zndxall/p/10456929.html
Copyright © 2011-2022 走看看