I am trying to use sed
to replace '0' with the word 'correct' (I am using echo $?
to validate my script and I want to replace the output of echo
). The problem is that I have more text in the output file and sed
is replacing everything and not just "0".
For example:
some command
echo ``date` `>> "file"
echo $? >> "file"
sed -i 's/0/correct/g' /"file"
Any ideas how could I get this working?
EDIT 1 13.02.2014
Output of echo $?
is "0" (what I know is that 0 means everything is OK).
Next thing is that sed -i 's/0/correct/g' /"file"
(I just used "" because somebody may misinterpret it with specific name).
Here is part of the script:
echo `date` | cat >> /file.txt
echo $? | cat >> /file.txt
sed -i 's/0/correct/g' /file.txt
-i
means:
text Insert text, which has each embedded newline preceded by a back‐slash.
From your code snippet, the sed
expression looks correct - it will replace every '0' character with the characters 'correct'. If you only want the output from your echo
command to be updated, you could put the sed
command on the output from echo
, eg:
some command date >> /file.txt echo $? | sed 's/0/correct/' >> /file.txt
(note removing 'echo' and 'cat' commands will have no effect on the output)
In the context you're using the -i
option, it's actually a commandline option, rather than a sed
script command - it's a different section in the man
page:
-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied)
This means it will update the source file (which is precisely what you want in your example).
However, note that in this example, the exit code being checked is the exit code of date
, and not your command. In fact in your example, it's checking the exit code of echo
, which I suspect is even less helpful for you.
A more standard way of performing this check would be along the lines of:
some command result=$? date >> /file.txt if [ $result -eq 0 ] then echo "Command finished successfully" >> /file.txt else echo "Command failed with exit code $result" >> /file.txt fi
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments