使用 bash 脚本在日志文件中搜索字符串
我刚刚开始学习PHP。我正在关注 phpacademy 的教程,我会推荐给任何人。无论如何,我正在使用 XAMPP 来测试我的脚本。我正在尝试编写一个 bash 脚本,该脚本将启动 XAMPP,然后如果找到特定字符串“XAMPP for Linux started”,则将 Firefox 打开到本地主机页面,该字符串已从终端重定向到文件 xampp.log。我在搜索文件时遇到问题。我不断收到:
grep: for: No such file or directory
我知道该文件存在,我认为我的语法是错误的。这是我到目前为止所得到的:
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
I just started learning PHP. I'm following phpacademy's tutorials 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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 shell 脚本中,您不应该编写
$variable
,因为这会对变量的值进行单词扩展。在你的例子中,它会产生四个单词。始终在变量周围使用引号,如下所示:
当字符串可能以破折号开头时,
-e
是必需的,并且字符串周围的引号将其保持为一个词。顺便说一句:当你编写shell程序时,第一行应该是
set -eu
。这将启用*e*错误检查和*u*n定义变量检查,这对您的情况很有用。有关更多详细信息,请阅读 Bash 手册。In shell scripts you shouldn't write
$variable
, since that will do word expansion on the variable's value. In your case, it results in four words.Always use quotes around the variables, like this:
The
-e
is necessary when the string might start with a dash, and the quotes around the string keep it as one word.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.您正在搜索一个字符串,您应该将其放在引号中。
尝试使用
"$string"
而不是$string
You are searching for a string you should put wihtin quotes.
Try
"$string"
instead of$string
有几个问题:
"$string"
$true
和$false
code>$loaded=$true
应为loaded=true。
if [$loaded -eq $true]
if [ "$loaded" -eq true ]
。在这种情况下,变量被设置,因此它不会引起问题,但通常不要依赖它。There are a couple of problems:
"$string"
$true
and$false
$loaded=$true
should beloaded=true.
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.