该grep命令给出退出状态:
$echo "foo.bar" | grep -o foo
foo
$echo $?
0
$echo "foo.bar" | grep -o pop
$echo $?
1
Run Code Online (Sandbox Code Playgroud)
但是我需要使用sed并且我意识到它没有退出状态:
$echo "foo.bar" | sed 's/bar.*$//'
foo.
$echo $?
0
$echo "foo.bar" | sed 's/pop.*$//'
foo.bar
$echo $?
0
Run Code Online (Sandbox Code Playgroud)
我知道我应该玩这个-q选项,但我没有成功。
ste*_*ver 14
您可以使用 q n以退出状态n退出- 但要使其有用,您还需要使用一些分支和流量控制:
t
有条件分支(即:跳转到标签)仅当s///自读取最后一个输入行或采用另一个条件分支后命令成功时。
最好为n选择一个不同于标准退出状态值之一的值:
退出状态为零表示成功,非零值表示失败。GNU 'sed' 返回以下退出状态错误值:
Run Code Online (Sandbox Code Playgroud)0 Successful completion. 1 Invalid command, invalid syntax, invalid regular expression or a GNU 'sed' extension command used with '--posix'. 2 One or more of the input file specified on the command line could not be opened (e.g. if a file is not found, or read permission is denied). Processing continued with other files. 4 An I/O error, or a serious processing error during runtime, GNU 'sed' aborted immediately.
所以例如
$ echo "foo.bar" | sed 's/bar.*$//; t; q42' ; echo $?
foo.
0
Run Code Online (Sandbox Code Playgroud)
然而
$ echo "foo.bar" | sed 's/baz.*$//; t; q42' ; echo $?
foo.bar
42
Run Code Online (Sandbox Code Playgroud)
如果要省略模式空间的默认打印,则替换q为Q(注意,这Q是一个 GNU 扩展)。
| 归档时间: |
|
| 查看次数: |
7640 次 |
| 最近记录: |