我想编写一个 bash 函数来检查文件是否具有某些属性并返回 true 或 false。然后我可以在“如果”的脚本中使用它。但是我应该返回什么?
function myfun(){ ... return 0; else return 1; fi;}
然后我这样使用它:
if myfun filename.txt; then ...
这当然不行。如何实现?
最佳答案
使用 0 表示真,1 表示假。
示例:
#!/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
编辑
从@amichair的评论来看,这些也是可以的
isdirectory() {
if [ -d "$1" ]
then
true
else
false
fi
}
isdirectory() {
[ -d "$1" ]
}
关于bash - 从 Bash 函数返回 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5431909/