OS X上脚本和命令行之间的空文件的grep -v -f不同

问题描述 投票:0回答:1

在bash中,在Mac上的终端中(但不在Linux中),grep -v -f的行为会有所不同,具体取决于它是在命令行还是在脚本中执行。从命令行:

$ touch empty-file  #create an empty file
$ printf 'foo' | grep -v -f empty-file
foo

这是预期的。但是当该行在脚本中时,它什么都不输出。这是脚本:

$ cat grep-v-in-script.sh 
#!/usr/bin/env bash
printf 'foo\n' | grep -v -f empty-file
printf 'end of script\n'

当我执行该脚本时:

$ ./grep-v-in-script.sh 
end of script

如果我在Linux中运行相同的脚本,它按预期工作:

herdrick@some-linux-server:~$ ./grep-v-in-script.sh 
foo
end of script

如果我将'grep -v -f'更改为'grep -f',我的Mac上的FWIW会再次输出,但这次是预期的。

这是我的bash版本:

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.
bash macos bsd
1个回答
1
投票

问题只是GNU grep和BSD grep之间不兼容。查看@ that-other-guy对帖子的评论。

我的困惑是由于我的别名设置为使用GNU grep。否则,在命令行和脚本中执行此操作没有区别。

© www.soinside.com 2019 - 2024. All rights reserved.