Batch macro not working - batch-file

I'm trying to create a batch program (here called pldl) to make downloading a playlist of songs with youtube-dl easier, the program is
youtube-dl -o "%(playlist_index)s. %(title)s.%(ext)s" -x --audio-format "mp3" %1
which is supposed to just take the first argument (%1) and add it to this long command, so running command_name "playlist_url" would download it in the location I ran it. Unfortunately, it throws an error instead (echo is on for debugging)
E:\Path\To\Music>pldl "https://www.youtube.com/playlist?list=PLSdoVPM5WnndV_AXWGXpzUsIw6fN1RQVN"
E:\Path\To\Music>youtube-dl -o "(title)s.1
Usage: youtube-dl [OPTIONS] URL [URL...]
youtube-dl: error: You must provide at least one URL.
Type youtube-dl --help to see a list of all options.
E:\Path\To\Music>
What's going on here? Why is the command not run properly as seen in the echo? Also is there possible a better way of doing this (tying a long command with argument to a short name) sorry for the low quality post just need this fast.

The script-parser tries to expand the variables:
%(playlist_index)s. %
%(ext)s" -x --audio-format "mp3" %
Which are likely undefined. Because there is nothing to expand, part of the command-line parameter is stripped before being passed to the youtube-dl program. Use double percent signs instead:
youtube-dl -o "%%(playlist_index)s. %%(title)s.%%(ext)s" -x --audio-format "mp3" "%~1"
Quoting and escaping
The percent sign (%) is a special case. On the command line, it does
not need quoting or escaping unless two of them are used to indicate a
variable, such as %OS%. But in a batch file, you have to use a double
percent sign (%%) to yield a single percent sign (%). Enclosing the
percent sign in quotation marks or preceding it with caret does not
work.

Related

how to pass "=" as part of argument to .cmd or .bat file from bit-bash for windows

in git-bash shell that I have open in windows10, this test
./test.cmd 123=456
returns
123
Contents of test.cmd are :
echo %1
I've tried ./test.cmd 123\=456 and 123^456 and doesn't work.
If I run ./test.cmd 123=456 from a cmd shell, it works fine and outputs 123=456.
I can escape other special characters, just not =
$ ./test.bat "123^^^&456"
C:\Users\551527>echo 123&456
123&456
551527#S29C0N4 MINGW64 ~
$ ./test.bat "123^^^=456"
C:\Users\551527>echo 123
123
What's so different about = ?
First problem: the equal sign is a delimiter in cmd.exe
If you start a batch file with a delimiter character this simply splits arguments.
myBatch.bat three;argu=ments
-- %1 = three
-- %2 = argu
-- %3 = ments
-- %* = three;argu=ments
To avoid the splitting you have to use quotes (carets doesn't work here).
myBatch.bat "three;argu=ments"
-- %1 = "three;argu=ments"
-- %~1 = three;argu=ments
Second problem: git-bash works different with arguments
bash has different rules for arguments, it decides what is a single argument and removes surrounding quotes
Using
./myBatch.bat "three;argu=ments"
is parsed by bash, a single argument is detected and then cmd.exe is called with this single argument like:
cmd /c myBatch.cmd three;argu=ments
At this point, it's obvious that you can't build a complex command line, that works for cmd.exe and also from bash.
It's possible to build solutions for concrete problems, but it's always a pain.

Batch file: <part of command line> was unexpected at this time

Trying to execute a command inside a batch file and drop the result into a variable.
Here's what I would execute interactively:
curl -d "username=someUser" http://someServer/trusted
The result is a 15-20 character alpha-numeric string.
Here's my attempt at doing same in a batch file:
FOR /f %AA IN('curl -d \"username=someUser\" http://someServer/trusted') DO ECHO %AA
Error returned is:
//someServer/Trusted') was unexpected at this time
I thought I was dealing with some sort of escaping issue, so I added the \ symbols in front of my quotes. From what I've read, the : symobol in http doesn't need to be escaped, but it's interesting to me that's where the failure appears to "start".
Any ideas on this? I'm on Win8.1, FYI
for-variables have one-char-variables (%A instead of %AA)
you missed a space between IN and (
in a batch-file you have to double the percent-signs for the for-variable (%%A instead of %A)
echo handles only one line, blanks brake it, you would need to pipe it maybe?

Need to clarify the use of pipe in batch file for a specific context

I am working on NSCA to integrate passive alerts and checks from remote machines and applications with Nagios. On the internet, I have found a part of batch file code which could help perform it. However I don't understand the use of the pipe at the last line. I know how the pipe works but I cannot see how the output echo %1;%2;%3;"%4" will be using as input of values %NSCA_BIN% -H %NSCA_SERVER% -p 5667 -to 10 -c %NSCA_CFG% -d ; .
set NSCA_HOME="D:\Nagios\BIN\GATEWAY"
set NSCA_BIN=%NSCA_HOME%\send_nsca.exe
set NSCA_CFG=%NSCA_HOME%\send_nsca.cfg
set NSCA_SERVER="192.168.10.110"
echo %1;%2;%3;"%4" | %NSCA_BIN% -H %NSCA_SERVER% -p 5667 -to 10 -c %NSCA_CFG% -d ;
Can you enlighten to clarify this point (about the meaning of the use of this pipe) for me please?
Thanks in advance
In this case, it looks like D:\Nagios\BIN\GATEWAY\send_nsca.exe takes input from the user when run from a command line. This script uses the values in %1 - %4 as the inputs as if the user had typed it.
The variables %1, %2, %3, and %4 are the first four space-delimited arguments provided on the command line to the script.
The part after the pipe evaluates to:
D:\Nagios\BIN\GATEWAY\send_nsca.exe -H 192.168.10.110 -p 5667 -to 10 -c D:\Nagios\BIN\GATEWAY\send_nsca.cfg -d ;
Documentation for send_nsca says that it takes data from standard input consisting of Nagios Service Host, Nagios Service Description, Status Number, and Plugin Output separated by tabs or a delimiter specified with the -d switch. The command above sets the delimiter to ;.
The part before the pipe, echo %1;%2;%3;"%4", takes the four command line arguments, separates them with colons, quotes the last one, and feeds them as input to send_nsca. That means you'd call the script like this:
scriptname.bat NagiosServiceHost NagiosServiceDescription StatusNumber PluginOutput
The purpose of the script appears to be to save you the trouble of having to type out the path to send_nsca and all the command line arguments, and simply provide the input, which is useful if you use this command multiple times.
I seems to me, though, that there's an error in the way the batch file is written. Nothing I see in the docs specifies that the fourth field of the input (plugin output) needs to be quoted, so I presume that the idea behind quoting %4 is to allow the plugin output specified on the command line to contain spaces. However, this won't work, because the batch file assigns space-delimited tokens from the command line to the %# variables, so %4 will only contain the fourth token, and everything after a space that follows it will be ignored.
I think the quotes should be taken out, and if you need to supply an argument with spaces, quote it on the command line at the time you invoke the batch file, like this:
scriptname.bat NagiosServiceHost NagiosServiceDescription StatusNumber "Plugin Output With Spaces"
This would set %4 to "Plugin Output With Spaces" (yes, the quotes are included in the value of the variable -- which is what you want).

Open Cygwin on a path passed as the first parameter to bash.exe

I'm going mad trying to achieve this. I read a lot of questions over Stack Overflow, but they didn't work. I'm trying to open a tab on console2 that opens Cygwin on the path passed as a parameter to it.
For cmd.exe, is quite easy:
cmd.exe %1
For Cygwin, this looks really hard:
bash --login -i -c 'cd `cygpath \'D:\Program Files\'`;exec bash'
The problem here is that with path without spaces it works well, with spaces, it doesn't work. Also, I don't know how to pass a param to it, maybe $1 or %1?
Edit 1:
I'm almost there, I created this batch file that should be run instead of bash.exe directly:
#echo off
set CHERE_INVOKES=%CD%
set TORUN="D:\Program Files\Cygwin\bin\bash.exe" --login -i -c 'cd "%CHERE_INVOKES%"; exec bash'
echo %TORUN%
call %TORUN%
PAUSE
This works with all paths except C: (and D:), the reason? Windows is stupid, and instead of having a path called C:, it has a path called C:!!! So, while all paths ends without a backslash, the first path ends with it, driving me mad!
The following command works for me:
c:\cygwin\bin\bash --login -i -c "cd '%~1'; exec /bin/bash.exe"
Where %~1 expands %1 removing any surrounding quotes (") — see help for in command prompt.
See also: chere package in Cygwin, and ConEmu terminal :)
Here is the solution:
#echo off
set CHERE_INVOKES=%CD%
::Remove trailing slash if required
IF %CHERE_INVOKES:~-1%==\ SET CHERE_INVOKES=%CHERE_INVOKES:~0,-1%
set TORUN="D:\Program Files\Cygwin\bin\bash.exe" --login -i -c 'cd "%CHERE_INVOKES%"; exec bash'
call %TORUN%
I added this code from this question: Remove Trailing Slash From Batch File Input
::Remove trailing slash if required
IF %CHERE_INVOKES:~-1%==\ SET CHERE_INVOKES=%CHERE_INVOKES:~0,-1%
In this way I can use this batch file to open Console2 Cygwin on the current path.

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.
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? :-)
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.)
Here's another solution (workaround?) I've found:
first, ensure an environment variable defines the pipe character, for example:
set PIPE="|"
later, run the command specifying the above defined environment variable name:
"c:\(...)\devenv.com" foo.sln /build Debug%PIPE%Win32
That does the job even if there are multiple wrappers between the caller and the callee. I'm now using it with a very long chain of wrappers:
Python/Linux -> VirtualBox guest's executeProcess -> Cmd/Windows -> devenv.com
(cross posted to: How to pass a quoted pipe character to cmd.exe?)
Escaping a piping character in the Windows scripting language is done with a caret (^). I just had to do this the other day. I know this is old, but I thought I would post what I found in case others ran across this, like I did.
I'd consider going the easy route, and passing a placeholder-token instead - "$P", and then replace it within the CMD/Batch file; e.g. using the 'UnxUtils' SEd command to do the replacement:
For /F "usebackq delims=" %%r in (`Echo %Cmd% ^| sed -e "s/$P/|/g"`) do #Set Cmd2=%%r
REM Now run the command, with the proper pipe symbol in place
%Cmd2%
So having passed the command arg/CMD script args - "git status $P wc -l".
Interesting! What does escaping the | do?
Do these work?
echo "Debug|Win32"
echo "qt.sln" /BUILD 'Debug|Win32'

Resources