2

I have a command line utility which needs a text file (to format the output) as argument. I only need the pure value and no formatting, so I'd like to have a one-line script to get the value.

The text file template.file only contains:

$1

Here's an example of my utility:

vclient -h 10.0.0.131:3002 -t template.file -g getTempKist

What I would like is something like this:

vclient -h 10.0.0.131:3002 -t $(echo '\$1') -g getTempKist

Does anyone know how to use the result of an echo (or maybe an alternative) instead of an external text file?

I hope someone can help.

Markus

reitermarkus
  • 678
  • 1
  • 11
  • 27
  • You can propably use stdin for the input: `echo '$1' | vclient -h 10.0.0.131:3002 -t - -g getTempKist` – hek2mgl Mar 24 '15 at 10:18

3 Answers3

4

You can try two things:

First, you can probably use stdin for the input like this:

echo '$1' | vclient -h 10.0.0.131:3002 -t - -g getTempKist

Some tools support the special value - for filename arguments where - stands for stdin. However this depends on the implementation of the command.

Second thing you can use if your shell (like bash or zsh) supports this is to use process substitution:

vclient -h 10.0.0.131:3002 -t <(echo '$1') -g getTempKist
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Ok, I tried this but the tool just hangs. So this probably isn't implemented in the tool itself. – reitermarkus Mar 24 '15 at 10:45
  • Yeah looks like it isn't. Then you can use the second approach. – hek2mgl Mar 24 '15 at 10:45
  • 1
    Your OS/filesystem may have `/dev/stdin` available to use in place of `-` as the argument to `-t`. – chepner Mar 24 '15 at 15:50
  • @chepner Nice! If you know that the OS is Linux for example but you can't rely on a shell that will support process substitution - then using `/dev/stdin` looks like the most portable approach for use in shell scripts. – hek2mgl Mar 24 '15 at 16:12
  • 1
    I'm using it like this now wrapped in a script file so I can call it with a parameter and reuse it: `/bin/bash -c "/usr/bin/vclient -h 10.0.0.131:3002 -t <(echo '\$R1')` -c $1" – reitermarkus Mar 26 '15 at 13:09
2

If you are trying to use echo as a way to simply avoid creating a temporary file, then you might prefer to try seeing if you can paste directly to stdin. Many tools support this:

$ cat foo.json 
[1,2,3]

$ python -m json.tool foo.json 
[
    1,
    2,
    3
]

$ python -m json.tool /dev/stdin
[1,2,3]  <-- Paste this into the terminal then press Ctrl-D
[
    1,
    2,
    3
]

Thus, for the tool in the OP:

$ vclient -h 10.0.0.131:3002 -t /dev/stdin -g getTempKist
\$1  <-- Paste this into the terminal then press Ctrl-D
dotancohen
  • 30,064
  • 36
  • 138
  • 197
0

using a command surrounded with `` (sorry, code formatting gives me trouble with these chars) first evaluates the result of the command, so surrounding your echo with ` may work.

Dani
  • 161
  • 4
  • Note that `` and the `$()` operator are doing the same thing: [command substitution](http://tldp.org/LDP/abs/html/commandsub.html) – hek2mgl Mar 24 '15 at 10:20
  • Also check this: http://meta.stackexchange.com/questions/55437/how-can-the-backtick-character-be-included-in-code .. Hope it helps :) – hek2mgl Mar 24 '15 at 10:27