1

In Bash, the variable - represents stdin for the command and is extremely useful for piping to commands. I was wondering if there was an equivalent in Windows batch files, and I can't seem to find an answer.

Here's what I've traditionally done when absolutely necessary (and it is if statements where it is most useful):

<command> | set /p tmpvar=
if "%tmpvar%"=="whatever" rem stuff

I feel it would be much quicker and easier to do something like what you could do in Bash. Here's an equivalent script in Bash, which I'm wanting something a bit simpler like this one for Windows batch (DOS compatibility is not required).

<command> | if ("-" == "whatever" ) # stuff

Is there an equivalent simpler than the first example above? It would make things a lot easier.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Claudia
  • 1,197
  • 15
  • 30
  • I don't recognize that as a Bash script at all. What is it supposed to do? – Fred Foo Nov 15 '13 at 13:39
  • 3
    Sometimes it is best to say what you are trying to do rather than ask how to do something a specific way. See: [What is the XY Problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Gray Nov 15 '13 at 13:42

3 Answers3

5

In Bash, the variable - represents stdin

It actually doesn't. - as a shorthand for standard input/output is a convention in Unix commands, but the commands interpret it themselves; the shell has nothing to do with it. echo - just prints a dash.

The closest equivalent to this convention in DOS/Windows is the special filename CON, which is interpreted as the console. I don't think it helps with pipes, though -- like on Unix, the commands themselves should have some way of telling them to read from standard input.

dbenham
  • 127,446
  • 28
  • 251
  • 390
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
3

The following design pattern you claim to use can never work in Windows batch:

<command> | set /p tmpvar=
if "%tmpvar%"=="whatever" rem stuff

Each side of the pipe is executed in its own temporary CMD.EXE thread - the tmpvar does get set within the thread, but then vanishes once the pipe completes. See jeb's answer to "Why does delayed expansion fail when inside a piped block of code?".

The most common way to process the output of a command within batch is to use a FOR /F loop. Type help for, or for /? for a description. It is a complicated command. It can process the output of a command if the IN clause is enclosed in single quotes like IN ('someCommend'). It executes the DO clause once for each line of output. It also can parse each line into tokens, keeping select tokens. By default it processes the first token only, using <space> and <tab> as token delimiters. By default, it will also ignore lines that begin with ; due to the default eol option. There are various options to specify the delimiters, which tokens to capture, and the eol.

For example, the following will simply echo each line of output, except empty lines and lines beginning with ; will be ignored:

for /f "delims=" %%A in ('someCommand') do echo( %%A
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • +1. Just the bottom line example was missed: `for /f "delims=" %%A in ('someCommand') do if "%%A"=="whatever" rem stuff` – Aacini Nov 15 '13 at 18:21
  • EDIT - Changed ECHO to ECHO( in case there are lines that contain only spaces or tabs. – dbenham Nov 15 '13 at 18:36
1

In Bash, the variable - represents stdin for the command

Not necessary true. - is interpreted by specific programs or commands, not shell.

cat - reads stdin.

cd - or perl -e "print @ARGV;" - behaves differently.

Jokester
  • 5,501
  • 3
  • 31
  • 39