I have a one-liner Perl search and replace that looks roughly like this:
perl -p -i -e 's/foo/bar/' non-existent-file.txt
Because the file doesn't exist (which isn't intentional, but this is part of an automated build script, so I want to protect against that), Perl exits with this error:
Can't open non-existent-file.txt: No such file or directory.
However, the exit code is still zero:
echo $?
0
Am I doing something wrong? Should I be modifying my script, or the way I'm invoking Perl? I was naively assuming that because Perl couldn't find the file, it would exit with a non-zero code.
You can force error by dying,
perl -p -i -e 'BEGIN{ -f $ARGV[0] or die"no file" } s/foo/bar/' non-existent-file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With