[英]Convert Windows file format to Linux format
I am using Linux machine for development and then deploying my script files to Azure function app which is a Windows machine. 我使用Linux计算机进行开发,然后将脚本文件部署到Windows计算机的Azure函数应用中。
Then I am copying shell script files to wasb from Azure function app. 然后,我将外壳脚本文件从Azure函数应用程序复制到wasb。
I am getting following error when running shell script in Edge node on Azure HDI cluster 在Azure HDI群集的Edge节点中运行Shell脚本时出现以下错误
$'\r': command not found
My script files are not executed properly. 我的脚本文件未正确执行。
What is the best way to convert dos2unix option in C# Azure function? 在C#Azure函数中转换dos2unix选项的最佳方法是什么?
You don't have to do anything special to read a file that contains Unix-style newlines ( \\n
) instead of \\r\\n
. 您无需执行任何特殊操作即可读取包含Unix风格的换行符( \\n
)而不是\\r\\n
。 .NET IO methods treat both as newlines. .NET IO方法将两者都视为换行符。
You could write 你可以写
var lines-File.ReadAllLines("myUnixText.txt");
or 要么
using(var reader=File.OpenText("myUnixText.txt"))
{
string line;
while( (line=reader.ReadLine()) !=null)
{
// Do something
}
}
to read lines whether the line ending is \\r
or \\n
读取行尾是\\r
还是\\n
To prove it : 为了证明这一点:
var numbers = new[]{1,2,3,4,5,6};
var lines=String.Join("\n",numbers);
File.WriteAllText("myUnixText.txt",lines);
var newLines=File.ReadAllLines("myUnixText.txt");
Debug.Assert(newLines.Length==6);
Even though only a single string was written, File.ReadAllLines
read 6 lines from the file 即使只写入了一个字符串, File.ReadAllLines
从文件读取6行
I am calling Dos2Unix(filePath) method before copying file from Azure function to WASB. 在将文件从Azure函数复制到WASB之前,我正在调用Dos2Unix(filePath)方法。
Call method:- 通话方式:-
Dos2Unix(D:\home\site\repository\sample.sh);
Following method actually works for me in C# Azure function. 以下方法实际上在C#Azure函数中对我有用。
private void Dos2Unix(string fileName)
{
const byte CR = 0x0D;
const byte LF = 0x0A;
byte[] data = File.ReadAllBytes(fileName);
using (FileStream fileStream = File.OpenWrite(fileName))
{
BinaryWriter bw = new BinaryWriter(fileStream);
int position = 0;
int index = 0;
do
{
index = Array.IndexOf<byte>(data, CR, position);
if ((index >= 0) && (data[index + 1] == LF))
{
// Write before the CR
bw.Write(data, position, index - position);
// from LF
position = index + 1;
}
}
while (index >= 0);
bw.Write(data, position, data.Length - position);
fileStream.SetLength(fileStream.Position);
}
}
Update 1:- 更新1:-
Here is my code to upload files(*.sh, *.jar, *.img, etc) to blob storage. 这是我的代码,用于将文件(* .sh,*。jar,*。img等)上传到Blob存储。
public bool UploadBlobFile(string containerName, string blobName, string filePath)
{
try{
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
// convert dos2unix format
Dos2Unix(filePath);
using (var fileStream = System.IO.File.OpenRead(filePath))
{
blob.UploadFromStream(fileStream);
}
return true;
} catch (Exception e) {
log.Info("Exception: " + e);
return false;
}
}
It seems that you have Windows style line endings (\\r\\n) - you need to change them to unix style (\\n). 看来您有Windows样式行的结尾(\\ r \\ n)-您需要将它们更改为Unix样式(\\ n)。
Refer: '\\r': command not found and Convert DOS line endings to Linux line endings in vim 请参考: '\\ r':找不到命令,并且在vim中将DOS行尾转换为Linux行尾
There is a tool in sourceforge that can do it. sourceforge中有一个工具可以做到这一点。 Is has been actualized recently a runs very well. 最近已经实现了一个很好的运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.