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
另一种方法是在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 执行失败报错的更多相关文章
- 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 ...
- mac上使用gitlab拉项目报错Permissions 0644 for ...
解决办法:执行命令sudo chmod 0600 /Users/***(电脑名)/.ssh/id_rsa
- refiling失败报错Invalid function: org-preserve-local-variables
refiling失败报错Invalid function: org-preserve-local-variables,原因: elc,不太清楚 解决办法: 删除org??目录下的elc文件 https ...
- 数据库执行sql报错Got a packet bigger than 'max_allowed_packet' bytes及重启mysql
准备在mysql上使用数据库A,但mysql5经过重装后,上面的数据库已丢失,只得通过之前备份的A.sql重新生成数据库A. 1.执行sql报错 在执行A.sql的过程中,出现如下错误:Got a p ...
- HQL语句中数据类型转换,及hibernate中createQuery执行hql报错
一.HQL语句中数据类型转换: 我们需要从数据库中取出序号最大的记录,想到的方法就是使用order by子句进行排序(desc倒序),然后取出第一个对象,可是当初设计数据库时(我们是在原来的数据库的基 ...
- RedisCluster的rename机制失败报错,解决又是数据倾斜问题
需求说明:spring session中的用户session更新是更新key的名字,所以对于key的操作时需要用newkey 替换oldkey value值只允许存在一个,这里用到rename就很合适 ...
- SVN 执行cleanup报错:Cleanup failed to process the following paths
SVN 执行cleanup报错:Cleanup failed to process the following paths 先来说下这个错误的原因:用SVN在使用过程中,各种原因中途取消或中断,导致需 ...
- Git上传代码遇到的报错
Git上传代码遇到的报错 1.git上传代码卡住(Total 7072 (delta 2508), reused 6844 (delta 2376), pack-reused 0) git confi ...
- 执行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 ...
随机推荐
- 金蝶K/3 跟踪语句_业务单据
跟踪语句_业务单据_BOM select * from t_TableDescription where Ftablename like '%ICBOM%' order by FFieldName o ...
- ionic 3 热更新 Hot Code Push
最近用ionic 3 做的app业务做的差不多了,突然想到以后app如果有更新该怎么搞?想到我们的app后期更新应该不大,,最多就是改改bug和增加下用户体验,如果只有一些小的更新,然后提交各个应用商 ...
- 4337: BJOI2015 树的同构
题解: 树的同构的判定 有根树从根开始进行树hash 先把儿子的f进行排序 $f[i]=\sum_{j=1}^{k} { f[j]*prime[j]} +num[i]$(我没有仔细想这样是不是树是唯一 ...
- .net core2.x - Identity - 简介
- call、apply、bind
***call,apply,bind 替换this 何时: 只要this不是想要的都可用call,apply,bind替换 选择: call/apply: *调用*函数,在调用时,*临时*替换函数中的 ...
- BZOJ3257 [Zjoi2014]力 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8762639.html 题目传送门 - BZOJ3527 题意 给出长度为$m$的序列$q_{1..m}$,让你输 ...
- KaliLinuxNetHunter教程下载相关资源
KaliLinuxNetHunter教程下载相关资源 当用户将刷机工具准备完后,则需要下载ROM包.ROM是ROM image(只读内存镜像)的简称,常用于手机定制系统.一般手机刷机的过程,就是将只读 ...
- bzoj3124: [Sdoi2013]直径 树形dp two points
题目链接 bzoj3124: [Sdoi2013]直径 题解 发现所有直径都经过的边 一定在一条直径上,并且是连续的 在一条直径上找这段区间的两个就好了 代码 #include<map> ...
- 手机号验证正则表达式+Demo(亲测完毕)
以下为本人亲测过的验证手机号格式的demo,需要的小伙伴拿走不谢~<!DOCTYPE html><html><head><meta charset=" ...
- SpringMVC用到的jar包
SpringMVC用到的jar包 自己搭建一个SpringMVC框架时需要用到相应的jar包,参考下载网址: http://repo.spring.io/release/org/springframe ...