2

I wanted to execute the following shell script commands '.sh' from either c# or through command prompt (which I can further execute it via c#)

I usually use the following commands to execute my .sh file .

The commands I do it manually are

a) Open cygwin (which inturn calls C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -). This will open cygwin console

b) Go to the working directory where I need to execute the .sh file using

cd <dir in unix format>

c) execute the .sh script file

sh <path to my .sh file in unix format>

But, I would like to automate these steps using command line/ c# (i.e to execute without opening console/apps like mintty.exe or bash.exe)

Note : The examples provided in How do i start Mintty and run a script file? didnt help me since they just close after opening without executing my commands.

lit
  • 14,456
  • 10
  • 65
  • 119
giboo
  • 103
  • 11
  • 1
    Your question is currently too broad, it does not indicate that you have a specific coding problem. You have idicated that you would like to perform a task from either `C#` or `cmd.exe`, please choose one, attempt the task yourself and if you are unable to make it work as intended [edit your question](https://stackoverflow.com/posts/51244045/edit) showing us the failing code and expalining what happened which did not match your intent. – Compo Jul 09 '18 at 11:08
  • since I am currently using both batch scripts and c# I wanted to perform this in either of this and then I will integrate. I have tried in both c# and cmd... but couldnt find a solution working. Thats the reason I mentioned that if its not possible through c# , and if I get to know how to do it through .cmd, I will call the batch commands through C# (as a process etc) – giboo Jul 09 '18 at 11:14
  • 1
    You are still supposed to add the code you have tried and failed to make work within your question, otherwise you're requesting somebody else to write it for you; _and that is off topic_! – Compo Jul 09 '18 at 11:38

2 Answers2

1

You can run a script directly with bash, using C:\cygwin\bash.

example: c:\cygwin\bin\bash --login "C:\foo.bsh"

Hagai Wild
  • 1,904
  • 11
  • 19
  • I am getting the same issue. Looks like its executing something, but doesnt generate the result file in the output dir. – giboo Jul 09 '18 at 11:28
  • I suspect your output file is in your cygwin user's home directory. The `--login` option in bash execute both your `.bash_profile` and `.bashrc`, and sets the current directory to your home directory. Without tbe `--login` option to bash, only `.bashrc` is executed, so if you need the effects of `.bash_profile`, you must save your current directory, source the profile script and maybe restore your current directory. – Doug Henderson Jul 10 '18 at 02:57
1

If you want also the mintty window (useful when using vt100 escape sequences), you could use a batch wrapper file.

@echo off
setlocal EnableDelayedExpansion
set "script=%~1"
set "cygwin=C:\cygwin64\bin"
set "path=!cygwin!;!path!"
mode con lines=80 cols=120

REM C:\cygwin\bin\mintty
start "cygwin window" !cygwin!\mintty.exe -i /cygdrive/c/Windows/System32/compstui.dll,53 --exec "!script!"

You should prefix (at least) the cygwin/bin directory to the path variable, else you get strange results

The -i ...compstui.dll,53 is only for changing the icon in the taskbar/window

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thanks. That worked. An minor correction I did C:\cygwin\bin instead of C:\cygwin64\bin in my batch file (for my pc configuration) and I am planning to call this batch file using C# ... – giboo Jul 09 '18 at 11:56