1

Key value of this question is related to use dash-symbol "-" to redirect stdin stream.

I have basic test script within next code:

#!/bin/bash
# This causes the shell to await user input.
file -
# Need to emulate user input here...

Which one of shell-commands can emulate user input in this case?

Thank you.

  • check this post http://stackoverflow.com/questions/2746553/bash-script-read-values-from-stdin-pipe i think it includes an answer to your question – weberik Jan 07 '13 at 15:52
  • Are you asking about `bash` as in the shebang or `dash` as in the question title? – Jonathan Leffler Jan 07 '13 at 16:08
  • They are compatible in this particular aspect anyway; but then you should be asking generally about Bourne `sh` and compatibles. – tripleee Jan 07 '13 at 16:11

2 Answers2

1

A pipeline cmd1 | cmd2 by definition connects the standard output file descriptor of cmd1 as the standard input file descriptor of cmd2.

echo foo | file -

This particular example is hardly useful; the file command doesn't make a lot of sense to run on the kind of ephemeral data you have inside a pipeline. A more useful example runs file on, well, a file. If you absolutely want the file to be standard input, a simple input redirection does that.

file - </path/to/example/file

Or you might be looking for this:

file $(locate example/file)

where locate is an example of a command which prints the full path to a file you might want as the actual input argument to file.

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

You probably want a here document:

#!/bin/bash
# This causes the shell to await user input.
file - <<'EOF'
This is what the user
might have typed
if it wasn't in a here document.
${BASH:=head} $(ls | wc -l)
EOF

The here document continues up to the line containing only the marker word. In the example, the marker word is EOF; you can choose any name you like (! is often used). The quotes around the marker word mean that no shell expansion will be done on the here document, so the last line is not expanded by the shell. If the quotes were missing from (as in <<EOF) then shell expansion occurs. This is useful when you need it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278