Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sed 命令在 macOS 执行报错 #3

Open
ZhangChaoWN opened this issue Apr 19, 2020 · 5 comments
Open

sed 命令在 macOS 执行报错 #3

ZhangChaoWN opened this issue Apr 19, 2020 · 5 comments

Comments

@ZhangChaoWN
Copy link
Contributor

 % sed -i 's/services.gradle.org/downloads.gradle-dn.com/g' ./gradle/wrapper/gradle-wrapper.properties
错误提示:sed: 1: "./gradle/wrapper/gradle ...": invalid command code .

原因:macOS 的 sed 与 linux 有差异,-i开关后面有一个必填参数 extension。
解决方法:提供一个空的extension参数,如:

sed -i '' 's/services.gradle.org/downloads.gradle-dn.com/g' ./gradle/wrapper/gradle-wrapper.properties
@sinkcup
Copy link
Owner

sinkcup commented Apr 23, 2020

mac sed 和 GNU sed 不同,这样改的话,Linux 就无法执行了,参考:https://github.com/sinkcup/laravel-demo/blob/6e643b862b8ff1f4b2718b4316f5efcf2920bf97/phpunit.sh#L14

判断要写好几行,最简单的办法就是:

brew install gnu-sed

@sinkcup
Copy link
Owner

sinkcup commented Apr 26, 2020

@ZhangChaoWN gnu-sed 也不好,其他 mac 脚本运行时可能报错……
最近刚开始用 mac,发现不如 WSL 好用。因为原则是:开发人员的系统尽量接近服务器,Linux 符合,而 mac 不太符合。

@ZhangChaoWN
Copy link
Contributor Author

这里还有一种方法:https://stackoverflow.com/a/22247781,用perl:perl -pi -e s,foo,bar,g target.file,在mac和linux都可以正确的运行,而且主流linux和mac都预装了perl。

@ZhangChaoWN
Copy link
Contributor Author

mac sed 和 GNU sed 不同,这样改的话,Linux 就无法执行了,参考:https://github.com/sinkcup/laravel-demo/blob/6e643b862b8ff1f4b2718b4316f5efcf2920bf97/phpunit.sh#L14

判断要写好几行,最简单的办法就是:

brew install gnu-sed

个人感觉 sed --version 的输出内容主要是给人类看的,不是特别适合当作 API 使用

@sinkcup
Copy link
Owner

sinkcup commented Jul 1, 2020

发现了一个好方案,对 mac 进行标准化:https://github.com/fabiomaia/linuxify
@ZhangChaoWN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants