在使用.Net Core的Linux机器上查找内核数失败,并出现错误(带引号的字符串未终止) [英] Find number of cores on a linux machine using .Net Core failed with error (Unterminated quoted String)
问题描述
我在.Net Core中编写了以下代码,以检索运行Ubuntu OS的计算机的内核数.
I wrote the following code in .Net Core to retrieve number of cores of machine running a Ubuntu OS.
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/sh",
Arguments = "-c 'grep \"$0\" /proc/cpuinfo | uniq | sed -e \"$1\"' \"cpu cores\" 's/[^0-9]*//g'",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
string line = proc.StandardOutput.ReadToEnd();
如果我在腻子上执行命令,该命令将提供预期的结果,但是当我尝试在.Net Core代码中运行该命令时,该命令失败并显示错误.
The command gives expected result if I execute it on putty but fails with error when I try to run it inside my .Net Core code.
错误:$ 0:1:$ 0:语法错误:引号末尾的字符串
Error: $0: 1: $0: Syntax error: Unterminated quoted string
任何sh和.Net-Core专家都能解释我的代码有什么问题吗?
Any sh and .Net-Core expert who can explain what is wrong with my code?
推荐答案
在参数字符串之前放置 @
,使其成为逐字字符串.现在,您的字符串将反斜杠作为转义序列.
Put an @
before your argument string to make it a verbatim string. Your string now has the backslash as escape sequence.
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/sh",
Arguments = @"-c $'grep \""$0\"" /proc/cpuinfo | uniq | sed -e \""$1\"" \""cpu cores\"" 's/[^0-9]*//g''",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
这篇关于在使用.Net Core的Linux机器上查找内核数失败,并出现错误(带引号的字符串未终止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!