嘿伙计们,我正在研究一个项目,其中,我需要将设备名称存储在文件中,然后我试图使用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
您的语法错误,请尝试以下方法:
echo "Please enter the name of your device"
read device
if grep -q "$device" database; then
echo "yes"
else
echo "Nope"
fi
在分析shell脚本的问题时,您应该会发现ShellCheck非常有用。