suppress shell script error messages

前端 未结 6 1305
深忆病人
深忆病人 2020-12-24 10:30

In my shell script I got these lines:

rm tempfl.txt
rm tempfl2.txt

If these do not exist I get the error messages:

rm: temp         


        
6条回答
  •  醉酒成梦
    2020-12-24 11:23

    Adding to the answers above: It is probably a better idea to keep error messages (like permission denied or some such). Just test existence of the file before deleting it:

    [ -f file.txt ] && rm file.txt
    

    This assumes a Bourne like shell, e.g., bash. The above has the additional benefit that it won't try to delete a directory, something rm can't do.

提交回复
热议问题