简体   繁体   English

Bash“在寻找匹配时出现意外的 EOF”错误 - 不正确终止的 c# 字符串?

[英]Bash "unexpected EOF while looking for matching" error - Improperly terminated c# string?

I am trying to send some data via SMS with a .net core 2.2 app, running on a Linux system.我正在尝试使用在 Linux 系统上运行的 .net core 2.2 应用程序通过 SMS 发送一些数据。 To do this I am using Gammu, an external program that I can call and supply with my text data in an escaped string.为此,我使用了 Gammu,这是一个外部程序,我可以调用它并提供转义字符串中的文本数据。

I believe that I have used the correct quotations, formatted correctly and used in the correct places;我相信我使用了正确的引语,格式正确并用在了正确的地方; however, I am seeing the following error output:但是,我看到以下错误输出:

/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file

I have isolated the strings that I have been using and these errors appear to be being caused by only a specific string that I suspect may be improperly terminated somehow.我已经隔离了我一直在使用的字符串,这些错误似乎只是由我怀疑可能以某种方式不正确终止的特定字符串引起的。 Since, apparently, c# doesn't use null terminating characters, their presence (or absence) shouldn't really matter, yet removing (or keeping) them seems to still cause an issue.显然,c# 不使用空终止字符,因此它们的存在(或不存在)应该无关紧要,但删除(或保留)它们似乎仍然会导致问题。

The following is an example to illustrate the issue;下面是一个例子来说明这个问题;

Here's the code that triggers the issue.这是触发问题的代码。

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

Here's the helper function I'm using to run a command with 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;
    }
}

The code above produces the following output:上面的代码产生以下输出:

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

One successfully sends, the other throws an error.一个成功发送,另一个抛出错误。 Literally the only difference is the string of text, but I can't see where the issue is.从字面上看,唯一的区别是文本字符串,但我看不出问题出在哪里。

I believe a1 and a2 "should" be exactly the same, as a2 is a copy of what I observed a1 was populated with, both share the same length and .equals appears to return "true".我相信 a1 和 a2 “应该”完全相同,因为 a2 是我观察到的 a1 填充内容的副本,两者共享相同的长度并且 .equals 似乎返回“true”。 So it appears as though two strings match, yet they cause different behaviour so they clearly can't be the same.所以看起来好像两个字符串匹配,但它们会导致不同的行为,所以它们显然不能相同。

The code for populating my structure is as follows:填充我的结构的代码如下:

//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..
    }

I've also tried creating two char[] arrays to store both strings with a .ToCharArray() - and these are identical, too.我还尝试创建两个 char[] 数组来使用 .ToCharArray() 存储两个字符串 - 这些也是相同的。 I've tried .Trim(), .ToString() with no luck.我试过 .Trim(), .ToString() 没有运气。 I'm just incredibly lost as to what the issue could be, given that I have included enough quotes - and in the right places.鉴于我已经包含了足够多的引号 - 并且在正确的位置,我只是非常不知道问题可能是什么。

It feels as though my "serial" string just isn't terminated properly - but how can I verify and/or correct that?感觉好像我的“串行”字符串没有正确终止 - 但我该如何验证和/或更正呢?

Right, fixed it.对了,修好了。 Turns out that I need to replace this code:原来我需要替换这段代码:

int index = serial.IndexOf("\0");
if (index > 0)
   {
     serial = serial.Substring(0, index); 
    }

With code from this answer , if you want to trim all the \\0s使用此答案中的代码,如果您想修剪所有 \\0

cmd = cmd.TrimEnd('\0');

...or this code if you want to just stop after the first \\0.. ...或者这个代码,如果你想在第一个 \\0..

int index = cmd.IndexOf('\0');

if (index >= 0)

cmd = cmd.Remove(index);

Hope that helps anyone else who er, comes across this weirdness!希望能帮助其他遇到这种奇怪现象的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM