Start Perl script via "Windows Command" through Cygwin - windows

i have the following issue:
I try to start a perl script from the windows scheduler through cygwin
Steps i do:
Call process.bat file
In the process.bat i call bash with the parameter for the perl script
Symptoms:
If i call "perl scriptpath" directly from cygwin it works like a charm
If i call the script from the windows cmd with bash it will not work.
Command: C:\cygwin\bin\bash.exe --login /cygdrive/c/scripts/testscript.pl
It prints the following:
Line 3: use: command not found
Line 4: use: command not found
Can't find file Test
Script:
#!/usr/bin/perl
use strict;
use warnings;
print "Test";
Probably i'm making only a small mistake and cannot see it. It seems to interpret it with the windows cmd instead of the perl.

The parameter passed to bash will be treated as a Bash script, not a Perl script. There is no reason to use Bash in this case - just invoke Perl directly:
C:\cygwin\bin\perl.exe /cygdrive/c/scripts/testscript.pl
If you really want to do it your way - calling a cmd script which calls a Bash script which calls a Perl script - then you would need to write a Bash script to invoke your Perl script:
#!/bin/sh
/cygdrive/c/scripts/testscript.pl
And pass that Bash script as the parameter when you invoke bash.

You might like to use option -c to have bash execute a command, like this:
C:\cygwin\bin\bash.exe --login -c /cygdrive/c/scripts/testscript.pl
From the bash's man-page:
-c string
If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

Related

bash ignores shebang when running file passed as argument

How can I have bash respect the shebang when passing a file as an argument?
eg:
hello.py
#!/usr/bin/env python
print("Hello Python")
When passed to bash:
$ bash hello.py
hello.py: line 2: syntax error near unexpected token `"Hello Python"'
hello.py: line 2: `print("Hello Python")'
In the environment I’m using I unfortunately can’t execute hello.py directly, it has to be done by supplying args to bash.
Don't run scripts with an explicit interpreter. Always execute them by just typing the script name; that way the interpreter listed in the shebang will be used.
$ ./hello.py
This will require that the script is executable so make sure to do that if you haven't already.
$ chmod +x hello.py
If you have to run bash then use -c to pass a full command and stop it from trying to read it as a bash script:
$ bash -c './hello.py'

Pass command text to `bash` from `sh` without a script file?

I am trying to run a single command using bash in a sh script. There is no way to use bash for the script, I have to use sh. However, I need to run a bash-only command in sh.
Basically, I want something like the following:
bash --command_in "echo foobar"
Is this possible? I don't want to make a second script file just to run that one command in bash (like bash my_script.bash).
Derp, it's the -c flag. This wasn't easy to Google, and the --help is prety brief.

Passing arguments to perl using bash variables

Ordinarily I would invoke Perl and supply required arguments from within a bash script simply using:
perl script.pl arg1
However there are cases when I want to store both the perl script directory and the arguments in bash variables:
PERLDIR = "/example/directory/script.pl"
ARG1 = "40"
When trying to call the perl script using:
perl "$PERLDIR"
It works, however when trying to provide the argument i'm not sure of the syntax to utilise. If I use:
perl "$PERLDIR $ARG1" it'll attempt to open the directory:
/example/directory/script.pl 40
And throw an error.
Is there a way to do this and if so, how?
You should use:
perl "$PERLDIR" "$ARG1"
When you use many variables in one string enclosed with " it becomes one argument.

Pass command to cygwin shell script

I can't get the following to work:
c:\cygwin64\bin\bash -c /cygdrive/c/myscript.sh myargument
Specifically, myscript.sh is NOT getting "myargument" passed in as $1.
The script works exactly as I want it to if I hard-code "myargument" inside the script.
It's been 5 years since I have done any shell scripting and rust has accumulated!
The option -c is not used with scripts
Try this instead:
c:\cygwin64\bin\bash /cygdrive/c/myscript.sh myargument

How to pass arguments to a bash script running under MinGW on Windows

I have a bash script which takes two arguments .
On OS X I can invoke the script as follows:
./my-script arg1 arg2
and both arguments are passed to the script.
I have MinGW installed on Windows and when I invoke the script as follows:
my-script arg1 arg2
the arguments are not passed to the script.
However if I invoke it as follows:
bash my-script arg1 arg2
the arguments are passed to the script.
Is there is another way to pass the arguments without having to do this?
In OS X/Linux the terminal itself is bash and that can process shell script. But in the case of windows it does not use bash for terminal that is why you want install mingw to get bash itself. So if you want to execute scipt you want to execute bash and then input the script. So in command prompt you have to give bash my-script arg1 arg2. In windows explorer you can set *.sh extension to bash so by clicking on script it will execute bash. But you can't give arguments for scripts. In conclusion it is not possible to execute shell scripts without giving bash.
For windows my suggestion is to go with powershell scripts which is new or old batch scripts.

Resources