使用 bash 脚本在日志文件中搜索字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7457898/
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
Search log file for string with bash script
提问by thecakeisalie
I just started learning PHP. I'm following phpacademy'stutorials which I would recommend to anyone. Anyways, I'm using XAMPP to test out my scripts. I'm trying to write a bash script that will start XAMPP and then open firefox to the localhost page if it finds a specific string, "XAMPP for Linux started.", that has been redirected from the terminal to the file xampp.log. I'm having a problem searching the file. I keep getting a:
我刚开始学习PHP。我正在关注phpacademy 的教程,我会推荐给任何人。无论如何,我正在使用 XAMPP 来测试我的脚本。我正在尝试编写一个 bash 脚本,该脚本将启动 XAMPP,然后如果找到特定字符串“XAMPP for Linux started.”,则打开 firefox 到本地主机页面,该字符串已从终端重定向到文件 xampp.log。我在搜索文件时遇到问题。我不断收到:
grep: for: No such file or directory
I know the file exists, I think my syntax is wrong. This is what I've got so far:
我知道该文件存在,我认为我的语法是错误的。这是我到目前为止所得到的:
loaded=$false
string="XAMPP for Linux started."
echo "Starting Xampp..."
sudo /opt/lampp/lampp start 2>&1 > ~/Documents/xampp.log
sleep 15
if grep -q $string ~/Documents/xampp.log; then
$loaded=$true
echo -e "\nXampp successfully started!"
fi
if [$loaded -eq $true]; then
echo -e "Opening localhost..."
firefox "http://localhost/"
else
echo -e "\nXampp failed to start."
echo -e "\nHere's what went wrong:\n"
cat ~/Documents/xampp.log
fi
采纳答案by Roland Illig
In shell scripts you shouldn't write $variable, since that will do word expansionon the variable's value. In your case, it results in four words.
在 shell 脚本中,您不应该编写$variable,因为这会对变量的值进行字扩展。在您的情况下,它会产生四个字。
Always use quotes around the variables, like this:
始终在变量周围使用引号,如下所示:
grep -e "$string" file...
The -eis necessary when the string mightstart with a dash, and the quotes around the string keep it as oneword.
-e当字符串可能以破折号开头并且字符串周围的引号将其保留为一个单词时,这是必需的。
By the way: when you write shell programs, the first line should be set -eu. This enables *e*rror checking and checks for *u*ndefined variables, which will be useful in your case. For more details, read the Bash manual.
顺便说一句:当你编写 shell 程序时,第一行应该是set -eu. 这将启用 * e*rror 检查并检查 * u*n 定义的变量,这对您的情况很有用。有关更多详细信息,请阅读 Bash 手册。
回答by ztank1013
You are searching for a string you should put wihtin quotes.
Try "$string"instead of $string
您正在搜索应该用引号括起来的字符串。
尝试"$string"代替$string
回答by Karoly Horvath
There are a couple of problems:
有几个问题:
- quote variables if you want to pass them as a simple argument
"$string" - there is no
$trueand$false - bash does variable expansion, it substitutes variable names with their values before executing the command.
$loaded=$trueshould beloaded=true. - you need spaces and usually quotes in the if:
if [$loaded -eq $true]if [ "$loaded" -eq true ]. in this case the variable is set so it won't cause problems but in general don't rely on that.
- 引用变量,如果你想将它们作为一个简单的参数传递
"$string" - 没有
$true和$false - bash 进行变量扩展,它在执行命令之前用它们的值替换变量名。
$loaded=$true应该loaded=true. - 您需要空格,并且通常在 if: 中使用引号
if [$loaded -eq $true]if [ "$loaded" -eq true ]。在这种情况下,变量被设置为不会引起问题,但通常不要依赖它。

