0

i have a problem about bash, command is like:

echo helloworld > hello.txt 
cat <hello.txt >hello.txt  

at first, there are stuff in hello.txt. what i expect is that it should seem nothing happens to hello.txt after executing the command, but there is nothing in hello.txt then.
is it a mechanism of bash, or is there something i did not understand about linux file descripor?
maybe bash establish only one fd for a certain file? am i right?
can you help me?

/br
ruan

ruanhao
  • 4,663
  • 6
  • 28
  • 43
  • 5
    Redirecting output empties the file immediately, before `cat` can read from it. – Barmar Feb 27 '14 at 09:09
  • 1
    Won't work. out to a tmp file and then cat the tmp file to the original file. The redirect operator > will clear out the tmp file and write new content in. – Jayesh Bhoi Feb 27 '14 at 09:29
  • 1
    Basically you are trying to use the same file for both input and output in a single command which wont work.So the moment you issue the cat hello.txt, hello.txt is opened for writing by clearing all its contents. And subsequently there is no text in hello.txt to get the input from. – Gurubaran Feb 27 '14 at 16:22

1 Answers1

0

Use a temporary file

cat hello.txt > /var/tmp/tmpFile
mv -f /var/tmp/tmpFile hello.txt
cat hello.txt
iamsrijon
  • 76
  • 6