bash 在 shell 脚本中使用 grep 会导致找不到文件错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19447352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:20:17  来源:igfitidea点击:

Using grep inside shell script gives file not found error

bashshellubuntugrep

提问by Bonz0

I cannot believe I've spent 1.5 hours on something as trivial as this. I'm writing a verysimple shell script which greps a file, stores the output in a variable, and echos the variable to STDOUT.

我简直不敢相信我花了 1.5 个小时在像这样微不足道的事情上。我正在编写一个非常简单的 shell 脚本,它对文件进行 grep,将输出存储在一个变量中,并将该变量回显到 STDOUT。

I have checked the grep command with the regex on the command line, and it works fine. But for some reason, the grep command doesn't work inside the shell script.

我已经在命令行上用正则表达式检查了 grep 命令,它工作正常。但是由于某种原因,grep 命令在 shell 脚本中不起作用。

Here is the shell script I wrote up:

这是我写的shell脚本:

#!/bin/bash

tt=grep 'test' 
echo $tt

I ran this with the following command: ./myScript.sh testingFile. It just prints an empty line.

我跑这用下面的命令:./myScript.sh testingFile。它只是打印一个空行。

  • I have already used chmod and made the script executable.
  • I have checked that the PATH variable has /binin it.
  • Verified that echo $SHELLgives /bin/bash
  • In my desperation, I have tried all combinations of:
    • tt=grep 'test' "$1"
    • echo ${tt}
    • Not using the command line argument at all, and hardcoding the name of the file tt=grep 'test' testingFile
  • I found this: grep fails inside bash script but works on command line, and even used dos2unixto remove any possible carriage returns.
  • Also, when I try to use any of the grep options, like: tt=grep -oE 'test' testingFile, I get an error saying: ./out.sh: line 3: -oE: command not found.
  • This is crazy.
  • 我已经使用了 chmod 并使脚本可执行。
  • 我已经检查过 PATH 变量是否/bin在其中。
  • 验证echo $SHELL给出/bin/bash
  • 在绝望中,我尝试了以下所有组合:
    • tt=grep 'test' "$1"
    • echo ${tt}
    • 根本不使用命令行参数,并硬编码文件名 tt=grep 'test' testingFile
  • 我发现了这一点:grep 在 bash 脚本中失败,但在命令行上工作,甚至用于dos2unix删除任何可能的回车。
  • 此外,当我尝试使用任何 grep 选项时,例如:tt=grep -oE 'test' testingFile,我收到一条错误消息:./out.sh: line 3: -oE: command not found
  • 这太疯狂了。

回答by Chris Seymour

You need to use command substitution:

您需要使用命令替换:

#!/usr/bin/env bash

test=$(grep 'foo' "")
echo "$test"

Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed like this:

命令替换允许命令的输出替换命令本身。当命令被这样包围时,会发生命令替换:

$(command)

or like this using backticks:

或者像这样使用反引号:

`command`

Bash performs the expansion by executing COMMAND and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

Bash 通过执行 COMMAND 并用命令的标准输出替换命令替换来执行扩展,并删除所有尾随换行符。嵌入的换行符不会被删除,但在分词过程中可能会被删除。

The $()version is usually preferred because it allows nesting:

$()版本通常是首选,因为它允许嵌套:

$(command $(command))

For more information read the command substitutionsection in man bash.

有关更多信息,请阅读 中的command substitution部分man bash