[: ==: 一元运算符预期-bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19630619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[: ==: unary operator expected-bash
提问by user3493435
I am trying to read a text fiie sport.txt which contains the following and trying to match the user input with the sports name found in the text file,
我正在尝试读取包含以下内容的文本 fiie sport.txt 并尝试将用户输入与文本文件中找到的运动名称匹配,
if it's found it will print "sports found" and if it's not found it will print "No sports found"
如果找到它会打印“找到运动”,如果没有找到它会打印“没有找到运动”
the first example shown looks almost perfect till I tried to key in a random word and it displays an error
[: ==: unary operator expected
显示的第一个示例看起来几乎完美,直到我尝试输入一个随机单词并显示错误
[: ==: 一元运算符预期
I have also tried putting the "" for the variable in the second example shown but it will just print "No sports found" even though I typed a exact sports name matches with the sports name in the text file
我还尝试在显示的第二个示例中为变量添加“”,但它只会打印“未找到运动”,即使我在文本文件中键入了与运动名称匹配的确切运动名称
sports.txt
体育.txt
cycling
swimming
batmintion
code( example 1 )
代码(示例1)
#!/bin/bash
file="sports.txt"
read -p "Enter a sports name": sportsName
existingSports=$(grep $sportsName $file);
if [ $existingSports == $sportsName ]; then
echo "Sports Found"
else
echo "No Sports Found"
fi
if I key in 'swimming' base on the above codes
如果我根据上述代码输入“游泳”
output
输出
Sports Found
now if I key in 'swim'
现在如果我输入“游泳”
output
输出
No Sports Found
and if I key in a random word 'asd'
如果我输入一个随机单词“asd”
output
输出
[: ==: unary operator expected
No Sports Found
code( example 2 )
代码(示例2)
#!/bin/bash
file="sports.txt"
read -p "Enter a sports name": sportsName
existingSports=$(grep $sportsName $file);
if [ "$existingSports" == "$sportsName" ]; then
echo "Sports Found"
else
echo "No Sports Found"
fi
if I key in 'swimming' base on the above codes
如果我根据上述代码输入“游泳”
output
输出
No Sports Found
now if I key in 'swim'
现在如果我输入“游泳”
output
输出
No Sports Found
code( example 3 )
代码(示例3)
#!/bin/bash
file="sports.txt"
read -p "Enter a sports name": sportsName
existingSports=$(grep $sportsName $file);
if [[ "$existingSports" == "$sportsName" ]]; then
echo "Sports Found"
else
echo "No Sports Found"
fi
if I key in 'swimming' base on the above codes
如果我根据上述代码输入“游泳”
output
输出
No Sports Found
now if I key in 'swim'
现在如果我输入“游泳”
output
输出
No Sports Found
As mentioned the first example is almost close. what should I do to get rid of the error msg?
如上所述,第一个示例几乎接近。我应该怎么做才能摆脱错误消息?
采纳答案by anubhava
Instead of this block:
而不是这个块:
existingSports=$(grep $sportsName $file);
if [ $existingSports == $sportsName ]; then
echo "Sports Found"
else
echo "No Sports Found"
fi
You can just utilize grep -q
with word boundariesand reduce your code to single line:
你可以只利用grep -q
与字边界,减少您的代码一行:
grep -q "\<$sportsName\>" "$file" && echo "Sports Found" || echo "No Sports Found"
As per man grep
:
根据man grep
:
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
回答by Ashish
Try doing it in my way
尝试按照我的方式做
#!/bin/bash
file="sports.txt"
read -p "Enter a sports name": sportsName
sportsName=`echo $sportsName | sed -e 's/^ *//g' -e 's/ *$//g'`
#above sed command will remove all trailing and leading spaces which user can give as input
result=`grep -c $sportsName $file`;
if [ $result -eq 0 ]
then
echo "Sorry No match found"
else
echo "$result matches found"
fi
"-c" in grep will count number of occurrences and if occurrence is not 0 it shows the number of occurrences in else loop
grep 中的“-c”将计算出现次数,如果出现次数不为 0,则显示 else 循环中的出现次数
remember using "`" tild sign on grep command
记住在 grep 命令上使用“`”波浪号
If you are looking for exact word and not be be a substring of other word then use -w -c in grep command
如果您正在寻找确切的单词而不是其他单词的子字符串,请在 grep 命令中使用 -w -c
result=`grep -w -c $sportsName $file`;
man entries for -w
-w 的 man 条目
-w, --word-regexp
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word con-
stituent character. Similarly, it must be either at the end of
the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.