4

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

3 Answers 3

3

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.

4

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
0

I guess that you want to duplicate script input to terminal output, not redirect.

cat myfile.txt | tee /dev/tty | myscript.sh > myfile2.txt

or even simpler

cat myfile.txt; myscript.sh < myfile.txt > myfile2.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .