4 ms·
I think I've run into the first issue you describe, and I'm having a hell of a time trying to understand it. Would you mind taking a look at my example and help
by maksimum 9y ago
I think I've run into the first issue you describe, and I'm having a hell of a time trying to understand it. Would you mind taking a look at my example and helping me out?
Consider the following:
cd /tmp/
echo -e "hello world\nhello world\n:)" >> hello.txt
cat hello.txt
Outputs:
hello world
hello world
:)
Then running
bash -c 'sed s/"hello"/"hiiii"/ hello.txt | tee hello.txt'
cat hello.txt
yields
hiiii world
hiiii world
:)
Reseting to the original `hello.txt` and running
ssh localhost -t 'cd /tmp && sed s/"hello"/"hiiii"/ hello.txt | tee hello.txt'
cat hello.txt
yields an empty file.
Replacing the command with
ssh localhost -t 'cd /tmp && sed s/"hello"/"hiiii"/ hello.txt >> hello.txt'
yields
hello world
hello world
:)
hiiii world
hiiii world
:)
I'm trying to figure this out so I can publish a script to set up a test env for a package I'm trying to publish, and somewhat stuck on this step...
- mkl 9y agoYou're using hello.txt as both an input file and an output file, which seems like it's just asking for race-condition problems. What about using sed -i to make changes to the file, and not doing any bash redirection? Alternatively, if the real problem is more complex than that, try using a different name for the output file and rename after it's finished.
- half-kh-hacker 9y agoThe `>>` operator in use is /appending/ to the file. Also, as mentioned in a sibling comment, your hello.txt is both an input and output.