Change a string and undo this change make the file empty

116 views Asked by At

In my script to compile my program in XCode, I need to change the config file from

<string>${NAME}</string> to <string>My New Name</string>

And after finish, redo this changes. So this line work:

cat $PLIST | sed "s/\${NAME}/${NEW_NAME}/" > $PLIST  
--> Build my program
cat $PLIST | sed "s/${NEW_NAME}/\${NAME}/" > $PLIST  

But, sometime my file $PLIST is empty! And I don't know why. Is this a file protection? How can I force the file to be write?

1

There are 1 answers

0
FatalError On BEST ANSWER

Sounds like maybe a race condition. cat is trying to read the file, but your I/O redirection clobbers it. Sometimes it gets clobbered first and you get an empty file. If you're using GNU sed you can use the -i flag to change the file in place. Otherwise, you should probably use a temporary file.

Ex (GNU):

sed -i -e "s/${NEW_NAME}/\${NAME}/" "$PLIST"

or

sed -e "s/${NEW_NAME}/\${NAME}/" "$PLIST" > "${PLIST}.tmp" && mv -f "${PLIST}.tmp" "$PLIST"