linux - Bash 脚本 - "err: command not found"?

标签 linux bash shell

我正在编写一个 bash 脚本,我正在使用 for 循环来检查我的参数。

for var in "$@"
do
    test_arg "$var"
done

这是我的 test_arg 函数

function test_arg {
    [ -n "$1" ] || err "Empty argument"
    [ -f "$1" ] || err "Argument '$1' is not a file"
    [ -r "$1" ] || err "Data file '$1' is not readable"
    [ -s "$1" ] || err "Data file '$1' is empty"
    egrep -v '^-?([0-9]+|[0-9]*\.[0-9]+)$' "$1" && { echo "Bad data format in '$1'"; exit 1; }
}

然而,当这些条件中的任何一个不满足时,脚本只会写出“script.sh: line XX: err: command not found”。我不太确定测试,我是 bash 初学者。

非常感谢您的回答

最佳答案

您的代码依赖于名为 err 的函数。考虑这样定义它:

err() { echo "$*" >&2; exit 1; }

关于linux - Bash 脚本 - "err: command not found"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102281/

相关文章:

linux - 跳转后寄存器和变量不保存状态

linux - Linux 中的输出格式

linux - 就地排序文件

shell - # 和 ! 之间有空格在 shebang (#!/usr/bin/ksh)

bash - 如何使用命令通过 FTP 覆盖文件?

android - AOSP build image编译报错android 7

c++ - 记录数据时磁盘写入延迟

bash - 在 bash 中将时间戳转换为日期

php - Linux 更新 roundcube 停止工作并指向 squirrelmail

linux - 在 Linux shell 脚本中添加一个新目录