Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include pipe character in an argument to a batch file from a bash script?

I have a shell script that I want to execute this line:

qtvars.bat vsstart "qt.sln" /BUILD "Debug|Win32"

This works fine (though I had to modify qtvars.bat, but that's beside the point). The problem is that I want the command to execute to be in a variable: EDIT: This doesn't work either, if I type it into bash. Previously I was typing it into cmd.exe, which hardly made for a fair comparison.

command="qtvars.bat"
args="vsstart"

$command $args "qt.sln" /BUILD "Debug|Win32"

Now it chokes on the pipe! I get this message:

'Win32' is not recognized as an internal or external command,
operable program or batch file.

I've tried a bunch of forms of escaping the quotes and/or pipe, all to no avail. Interestingly, it works when it's an executable rather than a batch file, e.g.:

command="devenv.exe"
args=""

$command $args "qt.sln" /BUILD "Debug|Win32"

Thanks for any ideas.

like image 664
Owen Avatar asked Feb 20 '09 19:02

Owen


People also ask

What does %% A mean in batch?

%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.

What is pipe character in Bash?

In bash (and most *nix shells) the | (pipe) symbol takes the output from one command and uses it as the input for the next command.

How do I pass a command line argument to a batch file?

Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

What is $@ in Bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

I know you "escape" the pipe character in a batch file with the ^ character, so...

echo ^| Some text here ^|

Would display...

| Some text here |

I don't know whether that would help you in this instance? Maybe try prepending each pipe character with a ^ and see what happens? :-)

like image 103
Valiante Avatar answered Sep 18 '22 11:09

Valiante


This is a classic case of double-escaping, where both bash and CMD.EXE need to be instructed to ignore the special | (pipe) character.

Try the following:

$command $args "qt.sln" /BUILD '"Debug|Win32"'

This will be the equivalent of you typing, at a CMD.EXE prompt:

qtvars.bat vsstart qt.sln /BUILD "Debug|Win32"

Using the above, you are essentially forcing the passing of the double-quotes on to CMD.EXE (instead of bash eating them away.) The outermost single quotes instruct bash not to interpret or touch in any way what's inside them; the inner double-quotes instruct CMD.EXE to ignore any special characters (the pipe in this case) within.

Alternatively, you can also try:

$command $args "qt.sln" /BUILD 'Debug\|Win32'

This should be the equivalent of you typing, at a CMD.EXE prompt:

qtvars.bat vsstart qt.sln /BUILD Debug\|Win32

Note the use of single quotes (!), which ensure that bash will not interpret the \ (and, instead, will pass it as-is to CMD.EXE.)

like image 37
vladr Avatar answered Sep 20 '22 11:09

vladr