shell
shell学习

在MacOS中的Shell脚本执行sed -i命令报错

MacOS sed -i命令报错

需要删除test.txt中的所有hello world字符串,命令如下, sed -i '/hello world/d' test.txt 但执行时会报错,报错信息如下: sed: 1: "test.txt": undefined label 'est.txt'


原因: Mac中用-i命令进行替换时,强制要求备份,-i要加一个备份文件路径的参数 sed -i 'copypath' '/hello world/d' test.txt 如果不想备份,路径填空就行 sed -i '' '/hello world/d' test.txt

How to decode cmd output correctly?

ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");
startInfo.Arguments = "/c " + URL;
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
p = Process.Start(startInfo);
string xxx = p.StandardOutput.ReadToEnd();

获得标准输出的正确结果,需要增加以下代码

startInfo.StandardOutputEncoding = Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage)

在苹果OSX系统下,使用jenkins shell时判断Unity是否在运行的方法

打包时,如果打包机(苹果OSX系统)的Unity正在运行,则会打包失败。为了尽早捕获这种异常,需要判定Unity是否在运行。 经过一番搜索,还是使用了最原始的判断进程关键字的方法。Unity在mac上运行时,ps 查看进程通常有/xx/xx/Unity -projectPath xxx特征,提取最精简部分,统计进程数量(wc -l输出行数),即可。使用如下方式进行判定,目前准确率还行,用一段时间试试看。

#!/bin/bash
if [ $(ps -ef | grep "Unity -projectPath" | wc -l) = "2" ]; then
    echo "The Unity instance is running, exit now!"
    exit 1
fi

关于bash:在sed命令中转义正斜杠

  1. 如果replace内容不包含/这种写法没问题sed"s/regex/replace/" file
  2. 如果replace内容包含/,则改变分割符号为|,完整写法为 sed"s|regex|replace|" file ,这样就不用为\写转义了

常用模拟器adb连接端口

::夜神安卓模拟器 62001
::逍遥安卓模拟器  21503
::蓝叠安卓模拟器  5555
::雷电安卓模拟器 5555
::天天安卓模拟器  5037
::网易MuMu模拟器  7555
::安卓模拟器大师  54001
::腾讯手游助手   5555

adb connect 127.0.0.1:62001
adb logcat -s Unity