suppress shell script error messages

前端 未结 6 1302
深忆病人
深忆病人 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:13

    We can use 2> /dev/null to suppress the output error and || true to ensure a success exit status:

    rm foo
    => rm: cannot remove ‘foo’: No such file or directory
    
    rm foo 2> /dev/null
    echo $?
    => 1
    
    rm foo 2> /dev/null || true
    echo $?
    => 0
    

    If you are using the command in a shell script, makefile, etc, maybe you need this.

提交回复
热议问题