[英]Bash "unexpected EOF while looking for matching" error - Improperly terminated c# string?
我正在嘗試使用在 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 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);
希望能幫助其他遇到這種奇怪現象的人!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.