我想用一个更干净、不那么丑陋的单行代码来完成测试:
#!/bin/bash
test -d "$1" || (echo "Argument 1: '$1' is not a directory" 1>&2 ; exit 1) || exit 1
# ... script continues if $1 is directory...
基本上我追求的是不重复 exit
的东西,并且最好不产生子 shell(因此看起来也不那么难看),但仍然适合一行。
最佳答案
没有子 shell 也没有复制 exit
:
test -d "$1" || { echo "Argument 1: '$1' is not a directory" 1>&2 ; exit 1; }
您可能还想引用 Grouping Commands :
{}
{ list; }
Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.
关于bash - 用于退出带有错误消息的 bash 脚本的更好的一行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19515246/