使用 .Net Core 查找 linux 机器上的内核数失败并出现错误(未终止的引用字符串) [英] Find number of cores on a linux machine using .Net Core failed with error (Unterminated quoted String)

查看:23
本文介绍了使用 .Net Core 查找 linux 机器上的内核数失败并出现错误(未终止的引用字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .Net Core 中编写了以下代码来检索运行 Ubuntu 操作系统的机器的内核数.

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();

如果我在 putty 上执行该命令会给出预期的结果,但是当我尝试在我的 .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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆