Passing arguments to bash script from CMD prompt

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing arguments to bash script from CMD prompt

John Brown
I am trying to do this:

C:\> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg1 arg2 ...

The script executes, but it does not receive the arguments. The script
prints the output that is printed when it is called without arguments.
Is it possible to run a script with arguments like this?

Regards,
John Brown.

------------------------------------------------------------------------------

_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Passing arguments to bash script from CMD prompt

Peter Rockett
On 27/08/15 10:50, John Brown wrote:
I am trying to do this:

C:\> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg1 arg2 ...

The script executes, but it does not receive the arguments. The script
prints the output that is printed when it is called without arguments.
Is it possible to run a script with arguments like this?

Regards,
John Brown.

Does placing the script pathname and its arguments in inverted commas help?

I am just speculating that the cmd process is spawning an instance of a bash process, which is then running the script. Except I suspect the bash invocation is looking in its own process space for any script arguments... and finding none - they are in the space of the cmd process. Hence, my suggestion to try grouping the script name and arguments into one 'entity' in the hope that they get passed to bash as a job lot.

P.

------------------------------------------------------------------------------

_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Passing arguments to bash script from CMD prompt

John Brown
Peter Rockett <p.rockett@...> writes:

>
>     On 27/08/15 10:50, John Brown wrote:
> >       I am trying to do this:
> >        C:\> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg1 arg2
> >        ...
> >        The script executes, but it does not receive the arguments.
> >        Is it possible to run a script with arguments like this?
>
>     Does placing the script pathname and its arguments in inverted
>     commas help?

If you mean:
C:\> c:\mingw\msys\1.0\bin\bash.exe '/path/to/script arg1 arg2'
or
C:\> c:\mingw\msys\1.0\bin\bash.exe "/path/to/script arg1 arg2"
the result is the same in both cases:
/usr/bin/bash: /path/to/script arg1 arg2: No such file or directory.

However, the reverse (executing a Windows .cmd batch file in a MSYS
window) works. That is:
$ c:/windows/syswow64/cmd.exe /c "/path/to/script.cmd arg1 arg2"
prints "arg1 arg2" when script.cmd is
echo %1 %2.

Regards,
John Brown.


------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Passing arguments to bash script from CMD prompt

Keith Marshall-3
In reply to this post by John Brown
On 27/08/15 10:50, John Brown wrote:
> I am trying to do this:
>
> C:\> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg1 arg2 ...

If you are trying to do this directly from cmd.exe, without a properly
initialized MSYS process context, as set up by msys.bat, then be aware
that such usage is unsupported; YMMV, but if it doesn't work, then it
perhaps shouldn't come as too much of a surprise.

> The script executes, but it does not receive the arguments. The script
> prints the output that is printed when it is called without arguments.
> Is it possible to run a script with arguments like this?

Try quoting, behind a -c flag:

  C:\> c:\mingw\msys\1.0\bin\bash.exe -c '/path/to/script arg1 arg2 ...'

or perhaps:

  C:\> c:\mingw\msys\1.0\bin\bash.exe -c "/path/to/script arg1 arg2 ..."

but again, be aware that this is still very much a foray into the realm
of unsupported usage.

--
Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Passing arguments to bash script from CMD prompt

Roger Wells
On 08/27/2015 11:09 AM, Keith Marshall wrote:

> On 27/08/15 10:50, John Brown wrote:
>> I am trying to do this:
>>
>> C:\> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg1 arg2 ...
>
> If you are trying to do this directly from cmd.exe, without a properly
> initialized MSYS process context, as set up by msys.bat, then be aware
> that such usage is unsupported; YMMV, but if it doesn't work, then it
> perhaps shouldn't come as too much of a surprise.
>
>> The script executes, but it does not receive the arguments. The script
>> prints the output that is printed when it is called without arguments.
>> Is it possible to run a script with arguments like this?
>
> Try quoting, behind a -c flag:
>
>   C:\> c:\mingw\msys\1.0\bin\bash.exe -c '/path/to/script arg1 arg2 ...'
>
> or perhaps:
>
>   C:\> c:\mingw\msys\1.0\bin\bash.exe -c "/path/to/script arg1 arg2 ..."
>
> but again, be aware that this is still very much a foray into the realm
> of unsupported usage.
>

maybe a problem with msys's bash?
on my W7/cygwin64 setup this script (args.sh):

#!/bin/bash

echo nargs = $#
for arg in $@
do
    echo arg = $arg
done

being invoked in a cmd window as:

C:\Users\roger>
c:\cygwin64\bin\bash.exe c:\cygwin64\home\roger\src\args.sh arg1 arg2 arg3

produces output:

nargs = 3
arg = arg1
arg = arg2
arg = arg3

HTH



--
Roger Wells, P.E.
leidos
221 Third St
Newport, RI 02840
401-847-4210 (voice)
401-849-1585 (fax)
[hidden email]

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Passing arguments to bash script from CMD prompt

Eli Zaretskii
In reply to this post by John Brown
> From: John Brown <[hidden email]>
> Date: Thu, 27 Aug 2015 05:50:34 -0400
>
> I am trying to do this:
>
> C:\> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg1 arg2 ...
>
> The script executes, but it does not receive the arguments.

I think you need to use the -c option to Bash, and quote the rest.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Passing arguments to bash script from CMD prompt

Keith Marshall-3
In reply to this post by Roger Wells
On 27/08/15 16:24, Roger Wells wrote:
> maybe a problem with msys's bash?

I don't know about that.  This two line foo.sh (equivalent to your example):

  echo argc = $#
  for argv; do echo arg = $argv; done

when invoked from cmd.exe on my WinXP VM, as:

  c:\mingw\msys\1.0\bin\bash /e/foo.sh this is a test

results in the output:

  argc = 4
  arg = this
  arg = is
  arg = a
  arg = test

Notwithstanding that it does work, in this case, the usage remains
unsupported.

--
Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [SOLVED]Passing arguments to bash script from CMD prompt

John Brown
In reply to this post by Keith Marshall-3
Keith Marshall <keithmarshall@...> writes:

>
> On 27/08/15 10:50, John Brown wrote:
> > I am trying to do this:
> >
> > C:\> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg1 arg2 ...
>
>
> > The script executes, but it does not receive the arguments. The script
> > prints the output that is printed when it is called without arguments.
> > Is it possible to run a script with arguments like this?
>
> Try quoting, behind a -c flag:
>
>   C:\> c:\mingw\msys\1.0\bin\bash.exe -c '/path/to/script arg1 arg2 ...'

Sorry everyone. False alarm. Unsupported or not, it *does* work, and -c is
not necessary. Neither is it necessary to quote anything, so:
c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg 1 arg2 works.
I passed an unknown parameter to the script.

It is unlikely to make my computer explode, so I will use it. Thanks to all.

Regards,
John Brown.


------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: [SOLVED]Passing arguments to bash script from CMD prompt

Keith Marshall-3
On 27/08/15 18:25, John Brown wrote:
> Sorry everyone. False alarm. Unsupported or not, it *does* work, and
> -c is not necessary. Neither is it necessary to quote anything, so:
>
> c:\mingw\msys\1.0\bin\bash.exe /path/to/script arg 1 arg2
>
> works. I passed an unknown parameter to the script.
>
> It is unlikely to make my computer explode, so I will use it. Thanks
> to all.

Fair enough.  Your choice; your responsibility if you do experience any
adverse consequence.  Do please be aware that it may not always work as
you expect; here's an example that doesn't, for me, (even with -c):

  c:\mingw\msys\1.0\bin\bash uname -a
  /usr/bin/bash: uname: No such file or directory

  c:\mingw\msys\1.0\bin\bash -c 'uname -a'
  /usr/bin/bash: uname: command not found

Of course, uname is an executable, not a script, so we'd expect the -c
form to be needed anyway.  We can circumvent the "command not found"
problem, by giving the full path (from the MSYS root):

  c:\mingw\msys\1.0\bin\bash -c '/bin/uname -a'
  MINGW32_NT-5.1 ... 1.0.17(0.48/3/2) 2011-04-24 23:39 i686 Msys

(I don't have MSYS in my default windows PATH, because MSYS tools should
be run from an MSYS shell, _not_ from cmd.exe).  However, being mindful
of my earlier comment about process context initialization for the MSYS
shell, we may emulate what msys.bat does, like this:

  c:\mingw\msys\1.0\bin\bash --login -c 'uname -a'
  MINGW32_NT-5.1 ... 1.0.17(0.48/3/2) 2011-04-24 23:39 i686 Msys

Do note that this assumes running on 32-bit windows; when run on 64-bit
windows, msys.bat respawns itself under SysWoW64, so you would be well
advised to ensure that you invoke any MSYS process in like manner.

--
Regards,
Keith.

------------------------------------------------------------------------------
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe