Bash“;查找匹配项时出现意外EOF“;错误-错误终止的c#字符串?
我正在尝试使用运行在Linux系统上的.net core 2.2应用程序通过SMS发送一些数据。为此,我使用了Gammu,一个外部程序,我可以调用它并以转义字符串的形式提供文本数据 我相信我使用了正确的引文,格式正确,在正确的地方使用;但是,我看到以下错误输出:Bash“;查找匹配项时出现意外EOF“;错误-错误终止的c#字符串?,c#,bash,gammu,C#,Bash,Gammu,我正在尝试使用运行在Linux系统上的.net core 2.2应用程序通过SMS发送一些数据。为此,我使用了Gammu,一个外部程序,我可以调用它并以转义字符串的形式提供文本数据 我相信我使用了正确的引文,格式正确,在正确的地方使用;但是,我看到以下错误输出: /bin/bash: -c: line 0: unexpected EOF while looking for matching `"' /bin/bash: -c: line 1: syntax error: unexpected e
/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file
我已经隔离了我一直在使用的字符串,这些错误似乎只是由我怀疑可能以某种方式不正确终止的特定字符串引起的。由于c#显然不使用空终止字符,因此它们的存在(或不存在)并不重要,但删除(或保留)它们似乎仍然会引起问题
下面是一个例子来说明这个问题
以下是触发问题的代码。
string smsMessageString = "gammu sendsms TEXT ++4478901234 -text ";
string a1 = "08REEEKP010EEE";
string a2 = testStructure.serial;
//simply proves that a1 and a2 are the "same"
if (a1.Equals(a2))
{
Console.WriteLine(a1 + " is " + a2);
}
//This constructs two strings that should look identical
string smsMessageString2 = smsMessageString + "\"" + a1 + "\"";
string smsMessageString3 = smsMessageString + "\"" + a2 + "\"";
//Both strings should then be executed and two SMSs sent
Console.WriteLine(smsMessageString2);
var output = smsMessageString2.Bash();
Console.WriteLine(smsMessageString3);
output = smsMessageString3.Bash();
这是我用来运行bash命令的助手函数。
public static class ShellHelper
{
public static string Bash(this string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
}
上面的代码生成以下输出:
08REEEKP010EEE is 08REEEKP010EEE
gammu sendsms TEXT ++4478901234 -text "08REEEKP010EEE"
If you want break, press Ctrl+C...
gammu sendsms TEXT ++4478901234 -text "08REEEKP010EEE"
/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file
一个成功发送,另一个抛出错误。字面上唯一的区别是文本字符串,但我看不出问题在哪里
我认为a1和a2“应该”完全相同,因为a2是我观察到的a1填充内容的副本,两者具有相同的长度,并且.equals似乎返回“true”。所以看起来好像两个字符串匹配,但它们会导致不同的行为,所以它们显然不可能是相同的
填充我的结构的代码如下所示:
//The string's origin is a series of bytes, which is often padded by zeros.
//[0x30, 0x38, 0x52, 0x45, 0x45, 0x45, 0x4b, 0x50, 0x30, 0x31, 0x30, 0x45, 0x45, 0x45, 0x00, 0x00, 0x00, 0x00]
tempArray = new byte[18]; //This is the maximum that this value could be
Array.Copy(myBytesAbove, indexToStartFrom, tempArray, 0, 18); //It could start from a different place in the packet
serial = System.Text.Encoding.UTF8.GetString(tempArray); //Get a string from these bytes
//The following code was added to strip out all of the \0 characters
int index = serial.IndexOf("\0"); //There could be other crud copied after a \0
if (index > 0)
{
serial = serial.Substring(0, index); // Take the first string before the first \0
//serial = serial.Substring(0, index+1); //Same result, even if I just leave one of the \0 characters, which shouldn't be necessary..
}
我还尝试创建了两个char[]数组,用.ToCharArray()存储这两个字符串,它们也是相同的。我试过了。Trim(),.ToString(),但运气不好。考虑到我已经在正确的地方引用了足够多的话,我对这个问题可能是什么感到难以置信
感觉好像我的“串行”字符串没有正确终止-但我如何验证和/或纠正它?对,修复了它。事实证明,我需要替换此代码:
int index = serial.IndexOf("\0");
if (index > 0)
{
serial = serial.Substring(0, index);
}
使用中的代码,如果要修剪所有\0
cmd = cmd.TrimEnd('\0');
…如果要在第一次\0.之后停止,请输入此代码
int index = cmd.IndexOf('\0');
if (index >= 0)
cmd = cmd.Remove(index);
希望能帮助其他人,呃,遇到这种怪事 您是否尝试使用UseShellExecute=true?是的,这会产生“未处理的异常:System.InvalidOperationException:流程对象必须将UseShellExecute属性设置为false才能重定向IO流。”。如果这样可以解决问题,我会很高兴,但我更担心的是,我无法观察或测试两个看似相同的字符串之间的差异(其中一个有效,另一个无效),这是一个非常有趣的问题。我只能假设当您尝试替换\“to\\\”-尝试Console.WriteLine(escapedArgs)时出错;在您替换它之后,或者检查已经运行的bash命令(它可能运行类似于
gammu sendsms TEXT++4478901234-TEXT“08REEEKP010EEE”“
),因此WriteLine显示\“。。但这两种情况都是如此。唉。