无法做正确的匹配

问题描述 投票:1回答:1

嘿伙计们,我正在研究一个项目,其中,我需要将设备名称存储在文件中,然后我试图使用cat file | grep device找到该名称,如果该名称存在,我正在尝试打印是,否则没有。我这样做了以下代码,但我无法达到预期的结果,请指出什么是错的。

echo "Please enter the name of your device"
read device
$(cat database | grep $device) = found
if ('$found' = '$device');then
        echo "yes"
else
        echo "Nope"
fi
linux bash shell
1个回答
0
投票

您的语法错误,请尝试以下方法:

echo "Please enter the name of your device"
read device
if grep -q "$device" database; then
        echo "yes"
else
        echo "Nope"
fi

在分析shell脚本的问题时,您应该会发现ShellCheck非常有用。

© www.soinside.com 2019 - 2024. All rights reserved.