您好,我是 shell
脚本编写的初学者。
我有一个名为 logA.log
的日志文件和一个 B.sh
文件。
在日志文件中有一些行,我想通过执行B.sh
来查找该日志中特定单词的编号(最后10行)
在B中我写道
#!/bin/bash
variableString = tail -10f /home/appuser/logA.log
grep ERROR $variableString | wc -l
但输出是:
variableString: command not found
我知道“grep”行正在工作,但我无法访问 b.sh 中的 logA
。
如何将名为 variableString
的变量定义为 logA
的最后 10 行
最佳答案
您的命令没问题,但您必须了解存储命令输出的方式:var=$(command)
。另外,您可能会得到几行,因此请引用 return 命令以保持格式。因此,您应该使用:
variableString="$(tail -10f /home/appuser/logA.log)"
grep ERROR "$variableString" | wc -l
当您收到错误时
variableString: command not found
这是因为当您定义语法时,bash
会解释必须使用 = tail -10f/home/appuser/logA 执行
参数。请参阅Basic bash script variable declaration - command not found有关此的更多信息。variableString
命令.log
关于shell - Unix 变量声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21283184/