Redirect stdin to stdout

7,773

Solution 1

I cannot understand what you want: $* outputs the positional parameters and you did not supply any, so you got no output (apart from new-line).

cat does not read standard input when it is given a file to list, so myscript.sh | cat myfile.txt has the same effect as myscript.sh ; cat myfile.txt (not strictly true, but correct in terms of input/output).

If you want your script to copy input to output, then it should contain simply cat.

If you want output from $*, then you need to run myscript.sh {parameters}.

And what do you mean by "redirect the stdout in another file rather then myfile2.txt"? You presumably don't mean ... > myfile3.txt.

Solution 2

Try add cat <&0 to your script where you want to carry out the redirection job.

The &0 above represents the stdin file descriptor, and you're essentially putting things you get from stdin to cat.

Alternatively, use the following loop to do it yourself:

while read line
do
  echo "$line"
done
Share:
7,773

Related videos on Youtube

Federico Ponzi
Author by

Federico Ponzi

Computer scientist. Into Distributed Systems and Rust.

Updated on September 18, 2022

Comments

  • Federico Ponzi
    Federico Ponzi almost 2 years

    I've got a script, ex myscript.sh, and it has to redirect to stdout what it gets from stdin (like a cat).

    For example:

    myscript.sh < myfile.txt > myfile2.txt
    

    How to do this?

    Also, how can i redirect the stdout in another file rather then myfile2.txt?

    My tries:

    I tried using:

    echo $*
    

    And it only works using:

    myscript.sh | cat myfile.txt
    

    If i use:

    myscript.sh < myfile.txt
    

    it prints out nothing