使用 bash 脚本在日志文件中搜索字符串

发布于 2024-12-05 15:21:58 字数 876 浏览 1 评论 0原文

我刚刚开始学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-12-12 15:21:58

在 shell 脚本中,您不应该编写 $variable,因为这会对变量的值进行单词扩展。在你的例子中,它会产生四个单词。

始终在变量周围使用引号,如下所示:

grep -e "$string" file...

当字符串可能以破折号开头时,-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:

grep -e "$string" file...

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.

惯饮孤独 2024-12-12 15:21:58

您正在搜索一个字符串,您应该将其放在引号中。
尝试使用 "$string" 而不是 $string

You are searching for a string you should put wihtin quotes.
Try "$string" instead of $string

早乙女 2024-12-12 15:21:58

有几个问题:

  • 如果您想将变量作为简单参数传递,请引用变量 "$string"
  • 没有 $true$false code>
  • bash 进行变量扩展,它在执行命令之前用变量的值替换变量名称。 $loaded=$true 应为 loaded=true。
  • 您需要在 if 中添加空格和引号:if [$loaded -eq $true] if [ "$loaded" -eq true ]。在这种情况下,变量被设置,因此它不会引起问题,但通常不要依赖它。

There are a couple of problems:

  • quote variables if you want to pass them as a simple argument "$string"
  • there is no $true and $false
  • bash does variable expansion, it substitutes variable names with their values before executing the command. $loaded=$true should be loaded=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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文