-1

So as an input, i get echo [number]. For example: echo 5. I need to get as output the sequence of numbers from 1 to [number]. So if I get 5 as input, I need: 1 2 3 4 5 (all on a separate line).

I know I can use seq 5 to get 1 2 3 4 5 but the issue is that I need to use pipes.

So the final command should be like this: echo 5 | seq [number] which should give 1 2 3 4 5 as output. My issue is that I don't know how to get the output from echo as my input for seq.

  • Please [edit] your question and add more details or clarification. Do you mean that you have one program/script that sends a number to stdout and you want to pipe the output to a script that reads the value from stdin and uses it for `seq`? Is it guaranteed that the output is exactly one line with one integer number? Do you need an error handling for invalid input? – Bodo Oct 12 '21 at 17:23

2 Answers2

0

You can use the output of the echo command as follows:

seq $(echo 5)

In case you're dealing with a variable, you might do:

var=5
echo $var
seq $var
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • But the problem is that I don't know what the number will be, it can be any number which I don't know beforehand. The only thing I get is: "echo [number] | ..." and I need to add the ... – Mr_BananaPants Oct 12 '21 at 14:17
  • Where is that value coming from? Do you have the possibility to catch the `echo` in an output file? ... – Dominique Oct 12 '21 at 14:29
0

Assuming that echo 5 is an example replacement of an unknown program that will write a single number to stdout and that this output should be used as an argument for seq, you could use a script like this:

file seqstdin:

#!/bin/sh
read num
seq "$num"

You can use it like

echo 5 | ./seqstdin

to get the output

1
2
3
4
5

You can also write everything in a single line, e.g.

echo '5'| { read num; seq "$num"; }

Notes:
This code does not contain any error handling. It uses the first line of input as an argument for seq. If seq does not accept this value it will print an error message.
I did not use read -r or IFS= because I expect the input to be a number suitable for seq. With other input you might get unexpected results.

Bodo
  • 9,287
  • 1
  • 13
  • 29