比如说我要替换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. HBase2.0.5 WordCount

    待计算的wordCount文件放在HDFS上. wc.txt: hive hadoop hello hello world hbase hive 目标:进行WordCount计算,把结果输出到HBas ...

  2. pandas设置值-【老鱼学pandas】

    本节主要讲述如何根据上篇博客中选择出相应的数据之后,对其中的数据进行修改. 对某个值进行修改 例如,我们想对数据集中第2行第2列的数据进行修改: import pandas as pd import ...

  3. Android读写配置2

    上篇文章采用 Properties 读写配置,各种路径错误,要么没有写入权限. 后来查资料,采用另一种方式读写 SharedPreferencesImpl 直接贴代码 公共类 -- 读写 packag ...

  4. Centos下安装配置Mongodb3.6

    首先更新系统 yum -y update 1.安装Mongodb 编辑Mongodb安装源 vim /etc/yum.repos.d/mongodb-org-3.6.repo 编辑内容如下: [mon ...

  5. VB开发类似IIS简易的WebServer,代码不到100行

    最近遇到三个人问关于VB写网页服务器的问题,所以今天抽时间写一下,演示其实没有多复杂. 代码里自定义的方法只有四个,没有公共变量绕来绕去,该注释的也都注释了. 想扩展更复杂的功能,就需要自己补脑HTT ...

  6. 文件上传(xls)

    function UploadFile(){ var filewj =document.getElementById("filewj").files[0]; //input Id ...

  7. idea 自定义注释模板

    一.类注释模板 打开Preferences Editor -> File and Code Templates -> Files -> Class 效果图: 注释模板 /** * @ ...

  8. centos 搭建git服务器和客户端

    参考资料:http://blog.feehi.com/linux/124.html 1.搭配环境 2.安装git 3.创建git用户 4.创建裸库 5.配置公钥匙 6.客户端clone代码库 1.搭配 ...

  9. mobile_视口

    document.documentElement.clientWidth       不包含滚动条 window.innerWidth                                  ...

  10. PHP使用 strpos() 注意事项

    返回字符出现的第一个位置, 如果字符在被搜索字符串的开头, 则会返回 ‘0’ 因此, 在使用此函数判断 字符串是否包含 某一个字符时  使用: if(strpos('string','str') != ...