Bash函数:返回布尔被用于如 [英] bash functions: return boolean to be used in if

查看:175
本文介绍了Bash函数:返回布尔被用于如的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写检查文件是否具有一定的属性和返回true或false bash函数..然后我可以在如果用它在我的脚本。但我应该返回?

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 return?

function myfun(){ ... return 0; else return 1; fi;}

然后我用这样的:

then I use it like this:

if myfun filename.txt; then ...

当然,这并不工作..这可怎么实现的?

of course this doesn't work.. how can this be accomplished?

推荐答案

有关使用真正的0和1表示假。

Use 0 for true and 1 for false.

示例:

#!/bin/bash

isdirectory() {
  if [ -d "$1" ]
  then
    # 0 = true
    return 0 
  else
    # 1 = false
    return 1
  fi
}


if isdirectory $1; then echo "is directory"; else echo "nopes"; fi

这篇关于Bash函数:返回布尔被用于如的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆