比如说我要替换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. 【转】 为什么我们做分布式使用Redis

    绝大部分写业务的程序员,在实际开发中使用 Redis 的时候,只会 Set Value 和 Get Value 两个操作,对 Redis 整体缺乏一个认知.这里对 Redis 常见问题做一个总结,解决 ...

  2. [原创]基于Zynq AXI-Bram Standalone & Linux 例程

    基于Zynq AXI-Bram Standalone & Linux 例程 待添加完善中

  3. 你不知道的CSS单位

    CSS中大部分属性值都有对应的量词单位,常见的如描述盒模型尺寸的 width, height, margin, padding, border,又比如CSS3中的transform属性的一些值.下面的 ...

  4. [转] 微信小程序之生命周期

    本篇文章介绍小程序的生命周期,由于小程序分为应用和页面两个部分,所以小程序的生命周期就涉及到三个部分,分别是: 应用的生命周期 页面的生命周期 应用的生命周期对页面生命周期的影响 一.应用的生命周期 ...

  5. js查找字符串、js截取

    js查找元素.js查找字符串 let index=data.indexOf(","); js截取.js截取字符串 $("#bankurl_id").val(da ...

  6. 蓝桥杯 全球变暖(dfs)

    标题:全球变暖 [题目描述]你有一张某海域NxN像素的照片,"."表示海洋."#"表示陆地,如下所示: 其中"上下左右"四个方向上连在一起的 ...

  7. pycharm中split的应用

    #input 字符串 “5+9” value = "5+9" v1,v2 = value.split("+")#意思是把加号前后的5和9分别赋值给v1,v2 v ...

  8. Kali Linux常用服务配置教程启动DHCP服务

    Kali Linux常用服务配置教程启动DHCP服务 通过前面的介绍,DHCP服务就配置好了.接下来,用户就可以使用该服务器来获取IP地址了.下面将对前面配置的服务进行测试. 1.启动DHCP服务 如 ...

  9. 2017-11-4—模拟PID电路(参考ADN8834datasheet)

    先贴几张datasheet原图: 这部分都很想了解,最想了解的是这四个zero point.pole point.pole point.zero point是怎么求出来的? 现在S域求出传函?(自动化 ...

  10. 6. 深度克隆_ES7**_arr.includes('孙悟空')

    1. 如何实现深度克隆 利用 JSON 方法 (没办法克隆函数数据) `JSON.parse(JSON.stringify(xxx))` 自定义方法 检查所有数据类型的方法 `Object.proto ...