Вопрос

$ cat file 
aa
Baa
CaaD
$ sed -i -e 's/aa/AA/' file
$ echo $?
0
$ cat file
AA
BAA
CAAD
$ sed -i -e 's/aa/AA/' file
$ echo $?
0

This sed command exit status in both the cases whether it modifies or not is same and it is returning the same exit status as 0. I would like to have it as 0 if it changes and 1 in case if haven't modified anything.

If grep command return 0 if it found some string and return 1 if it does not , Same way if sed command modifies the file it has to return 0 and sed command doesn't modify anything then it has to return 1

I tried to use sed internal 'q' command but it didn't helped. Please help me.

Это было полезно?

Решение

The answer is "because that's how sed is implemented"

If you want to see if any changes were made

sed -i.bak -e 's/aa/AA/' file
if diff -q file file.bak >/dev/null; then
    echo no changes
else
    echo something changed
fi
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top