小编the*_*lie的帖子

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

我刚开始学习PHP。我正在关注phpacademy 的教程,我会推荐给任何人。无论如何,我正在使用 XAMPP 来测试我的脚本。我正在尝试编写一个 bash 脚本,该脚本将启动 XAMPP,然后在找到特定字符串“XAMPP for Linux started.”时打开 firefox 到本地主机页面,该字符串已从终端重定向到文件 xampp.log。我在搜索文件时遇到问题。我不断收到:

grep: for: No such file or directory
Run Code Online (Sandbox Code Playgroud)

我知道该文件存在,我认为我的语法是错误的。这是我到目前为止所得到的:

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
Run Code Online (Sandbox Code Playgroud)

string bash grep

4
推荐指数
1
解决办法
7872
查看次数

标签 统计

bash ×1

grep ×1

string ×1