Returning a boolean from a Bash function

前端 未结 10 1023
余生分开走
余生分开走 2020-12-07 07:02

I want to write a bash function that check if a file has certain properties and returns true or false. Then I can use it in my scripts in the \"if\". But what should I retur

10条回答
  •  没有蜡笔的小新
    2020-12-07 07:51

    For code readability reasons I believe returning true/false should:

    • be on one line
    • be one command
    • be easy to remember
    • mention the keyword return followed by another keyword (true or false)

    My solution is return $(true) or return $(false) as shown:

    is_directory()
    {
        if [ -d "${1}" ]; then
            return $(true)
        else
            return $(false)
        fi
    }
    

提交回复
热议问题