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
.
CodePudding user response:
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
CodePudding user response:
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.