How to avoid a bash script from failing when -e option is set?

前端 未结 3 647
误落风尘
误落风尘 2021-01-03 19:24

I have a bash script with -e option set, which fails the whole script on the very first error.

In the script, I am trying to do an ls on a

3条回答
  •  悲&欢浪女
    2021-01-03 19:41

    one solution would be testing the existence of the folder

    function myLs() {
        LIST=""
        folder=$1
        [ "x$folder" = "x" ] && folder="."
        [ -d $folder ] && LIST=`ls $folder`
        echo $LIST
    }
    

    This way bash won't fail if $folder does not exist

提交回复
热议问题