比如说我要替换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

另一种方法是在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

mac上sed -i 执行失败报错的更多相关文章

  1. Cocoapods pod update执行失败报错CocoaPods was not able to update the `master` repo.2019的解决

    很久没动pod,最近更新发现: CocoaPods报CocoaPods was not able to update the `master` repo. If this is an unexpect ...

  2. mac上使用gitlab拉项目报错Permissions 0644 for ...

    解决办法:执行命令sudo chmod 0600 /Users/***(电脑名)/.ssh/id_rsa

  3. refiling失败报错Invalid function: org-preserve-local-variables

    refiling失败报错Invalid function: org-preserve-local-variables,原因: elc,不太清楚 解决办法: 删除org??目录下的elc文件 https ...

  4. 数据库执行sql报错Got a packet bigger than 'max_allowed_packet' bytes及重启mysql

    准备在mysql上使用数据库A,但mysql5经过重装后,上面的数据库已丢失,只得通过之前备份的A.sql重新生成数据库A. 1.执行sql报错 在执行A.sql的过程中,出现如下错误:Got a p ...

  5. HQL语句中数据类型转换,及hibernate中createQuery执行hql报错

    一.HQL语句中数据类型转换: 我们需要从数据库中取出序号最大的记录,想到的方法就是使用order by子句进行排序(desc倒序),然后取出第一个对象,可是当初设计数据库时(我们是在原来的数据库的基 ...

  6. RedisCluster的rename机制失败报错,解决又是数据倾斜问题

    需求说明:spring session中的用户session更新是更新key的名字,所以对于key的操作时需要用newkey 替换oldkey value值只允许存在一个,这里用到rename就很合适 ...

  7. SVN 执行cleanup报错:Cleanup failed to process the following paths

    SVN 执行cleanup报错:Cleanup failed to process the following paths 先来说下这个错误的原因:用SVN在使用过程中,各种原因中途取消或中断,导致需 ...

  8. Git上传代码遇到的报错

    Git上传代码遇到的报错 1.git上传代码卡住(Total 7072 (delta 2508), reused 6844 (delta 2376), pack-reused 0) git confi ...

  9. 执行mysqld_safe报错:mysqld does not exist or is not executable

    执行mysqld_safe报错: [root@edu data]# /usr/local/mysql5.7/bin/mysqld_safe --user=mysql160427 12:41:28 my ...

随机推荐

  1. SpringBoot整合Dubbo,并实现dubbo实现动态调用

    在一些业务场景中,CP定单提交过来,需要提交到不同的通道进行业务处理 本文通过Dubbo以定义一个interface,各个通道方来实现这个接口.通过group来区分不同的通道 有需要的同学可以下载 示 ...

  2. linux服务器文件授权命令

    分两部分改属主和属组权限:更改权限,递归方式 chmod -R 755 /var/www/html/test.com更改属主,递归chown -R apache:apache /var/www/htm ...

  3. 大数据小白系列 —— MapReduce流程的深入说明

    上一期我们介绍了MR的基本流程与概念,本期稍微深入了解一下这个流程,尤其是比较重要但相对较少被提及的Shuffling过程. Mapping 上期我们说过,每一个mapper进程接收并处理一块数据,这 ...

  4. BZOJ.5092.[Lydsy1711月赛]分割序列(高维前缀和)

    题目链接 \(Description\) \(Solution\) 首先处理\(a_i\)的前缀异或和\(s_i\).那么在对于序列\(a_1,...,a_n\),在\(i\)位置处分开的价值为:\( ...

  5. CSS文字垂直居中的一些问题

    说到CSS文字垂直居中,很多初学者都喜欢用调整行高等于div高度的方式来达到效果, div { height:30px; line-height:30px; } 但其实这么做会遇到一个问题:多行文本溢 ...

  6. php实现根据字符串生成对应数组的方法

    先看看如下示例: <?php $config = array( 'project|page|index' => 'content', 'project|page|nav' => ar ...

  7. 使用Eclipse中的SVN提交代码遇到的问题

    问题: Previous operation has not finished; run 'cleanup' if it was interrupted svn: Commit failed (det ...

  8. (51)Wangdao.com第七天_JavaScript 编写位置及输出语句

    JavaScript 编写位置 编写在html内部标签的属性中 不推荐使用,因为结构和行为耦合,不便于维护 主要有  <button onclick="alert('点我干哈!');& ...

  9. 使用Java Low Level REST Client操作elasticsearch

    Java REST客户端有两种风格: Java低级别REST客户端(Java Low Level REST Client,以后都简称低级客户端算了,难得码字):Elasticsearch的官方low- ...

  10. 十三、事务、连接池 、ThreadLocal 、BaseServlet自定义Servlet父类 、 DBUtils à commons-dbutils

    l 事务 l 连接池 l ThreadLocal l BaseServlet自定义Servlet父类(只要求会用,不要求会写) l DBUtils à commons-dbutils 事务 l 事务的 ...