Shell脚本文件(.sh)无法从Linux上的c#内核运行 [英] Shell Script File(.sh) does not run from c# core on linux

查看:172
本文介绍了Shell脚本文件(.sh)无法从Linux上的c#内核运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从c#核心应用程序运行".sh"文件.但是它似乎无法正常运行.这是我的情况.

I am trying to run ".sh" file from c# core application.But it doesn't seem to be running properly.Here is my scenario.

我正在研究Linux环境下托管的.Net核心项目.我们正在尝试在我们的项目中使用"Apache FOP"创建"PDF".在这里,我创建了一个"shell脚本"文件"transform.sh",该文件内部调用了具有所需参数的"fop".由于在Windows计算机上进行了开发,因此我们测试了相同的usinf"batch"文件,即"transform.bat",但是由于我们无法在Linux环境中使用批处理"文件,因此我们创建了外壳脚本文件"transform.sh"

I am working on .Net core project which is hosted on Linux environment.We are trying to create "PDF" in our project for which we have used "Apache FOP". Here i have created one "shell script" file "transform.sh" which internally calls "fop" with required parameters.Since developement is being done on windows machine we tested the same usinf "batch" file i.e. "transform.bat",but since we cannot use the "batch" file on linux enviornment we have created shell script file "transform.sh"

以下是来自"transform.sh"的代码

Following is the code from"transform.sh"

./fop -xml $1 -xsl $2 -pdf $3

以下是我从中调用的"shell脚本文件"的C#代码

Following is C# code from which i am calling the "shell script file

    var process = new Process
                        {
                            StartInfo = new ProcessStartInfo
                            {
                                UseShellExecute = false,
                                RedirectStandardOutput = true,
                                Arguments = string.Format("{0} {1} {2}", XML_filename, XSL_filename, output)                                
                            }
                        };

    process.StartInfo.FileName = "Path to shell script file";
    process.Start();
    process.WaitForExit();

上面的代码没有给出任何错误,但也没有创建pdf文件.如果我直接从"Terminal"运行shell脚本文件,则可以正常工作并创建pdf文件.

Above code doesnot give any error but it also does not create the pdf file.If i directly run the shell script file from "Terminal" it works fine and create pdf file.

 ./transform.sh "/home/ubuntu/psa//PdfGeneration/ApacheFolder/XMLFolder/test.xml" "/home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl" "/home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/t444t.pdf"

如果我做错了什么请告诉我?如何通过C#核心应用程序使Sheel脚本在linux上运行.谢谢.

Please let me know if there is something wrong i am doing?How can i make the sheel script run on linux through C# core application. Thanks.

推荐答案

我能够解决此问题,只是以为我应该将解决方案放在这里,以便将来对其他人有帮助...

I was able to solve the issue,just thought that i should put my solution here so that it may help others in future...

如问题中所述,我无法在Linux机器上通过Shell脚本生成PDF文件.按照"@JNevill"的建议进行调试后,我了解到Shell脚本文件并未从.net进程本身调用

As mentioned in Question i was not able to generate the PDF file through shell script on linux machine.After debugging as suggested by "@JNevill" I came to understand that the shell script file was not getting called from .net process itself.

所以我的首要任务是制作通过.Net Process调用的shell脚本文件.经过大量的Net搜索并尝试了不同的解决方案之后,我在中获得了解决方案,该如何在终端中使用C#执行命令(单声道).

So my first task was to make the shell script file called through .Net Process. After lots of searching through Net and trying out different solutions i got solution at How to perform command in terminal using C#(Mono).

因此更改了我的调用流程的代码,如下所示,

So changed my code of calling the process as follow,

var command = "sh";
var myBatchFile = //Path to shell script file
var argss = $"{myBatchFile} {xmlPath} {xsltPath} {pdfPath}"; //this would become "/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/transform.sh /home/ubuntu/psa/PdfGeneration/ApacheFolder/XMLFolder/test.xml /home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl /home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/test.pdf"

var processInfo = new ProcessStartInfo();
processInfo.UseShellExecute = false;
processInfo.FileName = command;   // 'sh' for bash 
processInfo.Arguments = argss;    // The Script name 

process = Process.Start(processInfo);   // Start that process.
var outPut = process.StandardOutput.ReadToEnd();
process.WaitForExit();

更改代码后,.sh"文件被执行,我能够生成PDF文件.

After changing the code ,the ".sh" file got executed and i was able to generate the PDF file.

还需要更改正在调用Apache FOP文件的".sh"文件(即(transform.sh))的脚本.

Also script of the ".sh" file i.e. (transform.sh) which was calling Apache FOP file i.e. "FOP.sh" also needed to be changed.

最初的代码是

./fop -xml $1 -xsl $2 -pdf $3

我进行了如下更改(更改是为了提供FOP文件的完整路径)

Which i changed as follow,(Change was to give full path of the FOP file)

/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/fop -xml $1 -xsl $2 -pdf $3

这篇关于Shell脚本文件(.sh)无法从Linux上的c#内核运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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