我想编写一个 bash 函数来检查文件是否具有某些属性并返回 true 或 false。然后我可以在我的脚本中的“if”中使用它。但我应该返回什么?
function myfun(){ ... return 0; else return 1; fi;}
然后我像这样使用它:
if myfun filename.txt; then ...
当然这是行不通的。如何才能做到这一点?
使用 0 表示 true,1 表示 false。
Sample:
#!/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
Edit
从@amichair的评论来看,这些也是可能的
isdirectory() {
if [ -d "$1" ]
then
true
else
false
fi
}
isdirectory() {
[ -d "$1" ]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)