4

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

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"

Following is the code from"transform.sh"

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

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

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"

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.

5
  • Are there any spaces in the arguments you are sending to the bash script? Either way your arguments string should really have quotes around each argument. Check out the last example here for escaping double quotes. Does the bash script execute at all? You could have touch \some\file in the script to see if it's even executing. Commented Oct 14, 2016 at 14:13
  • yes there is space between each argument Commented Oct 14, 2016 at 14:28
  • I mean a space within the argument. Like /home/ubuntu/my folder/myfile.xml When you pass arguments to a shell script in linux it's important to encapsulate those arguments in quotes so they aren't split into multiple arguments. Perhaps, to see what's coming out of the script, change the scripts to: echo "$1" "$2" "$3" > /home/ubuntu/scripttest.txt. Then run your program that executes the script and see what gets kicked into that file. Commented Oct 14, 2016 at 14:31
  • I checked as you suggested,it seems to me that "transform.sh" file is not getting called from code itself.But i have provided the correct path to ".sh" file to the process.Is it right way to call the ".sh' file using process. Commented Oct 14, 2016 at 14:45
  • I don't know ProcessStartInfo object at all. But... My guess after looking through the documentation is to remove the Process. on your line Process.StartInfo.FileName="Path" Then use that ProcessStartInfo in the next line like Process.Start(StartInfo). Check out that example in the link in my first comment also here as it seem very similar to what you are doing. Commented Oct 14, 2016 at 14:51

3 Answers 3

6

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

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.

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

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

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.

Initially code was

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

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
Sign up to request clarification or add additional context in comments.

Comments

0

Late answer, but for me, it worked just by setting the RedirectStandardOutput to true and changing the FileName property like this:

processInfo.FileName = @"C:\Program Files\Git\git-bash.exe"; 
processInfo.RedirectStandardOutput = true;

Comments

0

As an alternative solution, I have created an open-source pure port of Apache FOP version 2.8 that runs in .Net Core.

.Net Core projects can be run in non-windows environments.

Check it out:

FOP.NetCore
https://github.com/sorcerdon/FOP.NetCore

Here is the nuget:

NuGet version (FOP.NetCore)

Since this is a pure port, that means that it can do anything that Apache FOP can do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.