I want to write a shell script that takes data from standard input, writes it to a file, and then does something with it.
For this question, suppose my script should accept standard input, write it to in.txt , then output the string "foo" from it and write the output to out.txt .
I wrote this script.
cat > in.txt grep foo in.txt > out.txt
As explained in some of the answers below, you could simply use
tee in.txt | grep foo > out.txt
What if this is some other command instead of grep that cannot be read from standard input? Does it become actual use of the cat then?
Here is one such example with chmod .
cat > in.txt chmod -v 600 in.txt > out.txt
The requirement that entry and exit be available in the files after the script completes.
I would like to know if my code uses useless use of cat , or if it is a perfectly valid scenario when cat can be called like this:
Also, is there a way to rewrite this code without using cat so that it doesn't make useless of any command useless?
Note. I'm interested in answers that apply to any POSIX compatible shell, not just Bash or Linux.
source share