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

查看:23
本文介绍了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批处理"文件,即transform.bat",但是由于我们无法在 linux 环境中使用批处理"文件,因此我们创建了 shell 脚本文件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 文件.如果我直接从终端"运行 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# 核心应用程序使 shell 脚本在 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 脚本文件.在通过网络进行大量搜索并尝试不同的解决方案后,我在 如何使用 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),即FOP.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天全站免登陆