I have a bash script which reads a file having multiple line of text and executes a command on those lines. Sometimes the command work, but sometimes it fails. I want to append a pound sign (#) at the start of the lines of text where the command worked successfully and lines where the command failed they should as it is so that they can be retried in next run of the script. Lines which starts from # doesn't get executed.
My current script looks something like this -
COUNT=1
while read -r LINE; do
if [[ ($LINE != \#*) && !(-z "${LINE// }") ]] ; then
echo "${LINE}"
# execute "$LINE"
if [ $? -eq 0 ]; then
echo OK # Append pound (#) at the start of the line
else
echo FAIL # Keep the line in the source file
fi
fi
COUNT=$(( $COUNT + 1 ))
done < $SRC_FILE
As you can see the condition is already there, I just need to replace those echo OK
and echo FAIL
line of code. I tried this answer, but it removes all the lines in the end, I don't want that, I want to keep the failed lines in the file.
I tried using sed
, but it appear to do nothing -
sed -n "${COUNT}s/^/#/" $SRC_FILE >> $SRC_FILE
Is there any way to do that?
PS: I want to make it work on bash and zsh on both Linux and Mac terminal, especially Mac zsh.
Update:
I tried the following command
sed "${COUNT}s/.*/#&/" $SRC_FILE >$SRC_FILE
It removes all the line from the source file and make it empty, but if I change the output file, it works, although adds the # in the last line only, since it's still reading from the original source file.
#
at the start of non-empty lines that does not already have a#
at the start of the line? – Kusalananda May 08 '19 at 15:46#
at the start of lines which succeed in running the command so that I can skip them in the next run. Lines which already have#
in start are anyway not processed. – noob May 08 '19 at 15:51