bash shell脚本中2个字符串变量的比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18346824/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:19:02  来源:igfitidea点击:

Comparison of 2 string variables in shell script

bashshell

提问by eddyrokr

Consider there is a variable line and variable word:

考虑有一个变量 line 和变量 word:

line = 1234 abc xyz 5678
word = 1234

The value of these variables are read from 2 different files.

这些变量的值是从 2 个不同的文件中读取的。

I want to print the line if it contains the word. How do I do this using shell script? I tried all the suggested solutions given in previous questions. For example, the following code always passed even if the word was not in the line.

如果它包含单词,我想打印该行。如何使用 shell 脚本执行此操作?我尝试了之前问题中给出的所有建议解决方案。例如,即使单词不在行中,以下代码也始终通过。

if [ "$line"==*"$word"*]; then
    echo $line
fi

回答by Tim Cooper

No need for an ifstatement; just use grep:

无需if声明;只需使用grep

echo $line | grep "\b$word\b"

回答by Bill

You can use if [[ "$line" == *"$word"* ]]

您可以使用 if [[ "$line" == *"$word"* ]]

Also you need to use the following to assign variables

您还需要使用以下内容来分配变量

line="1234 abc xyz 5678"
word="1234"

Working example -- http://ideone.com/drLidd

工作示例——http://ideone.com/drLidd

回答by David W.

Watch the white spaces!

注意空白区域!

When you set a variable to a value, don't put white spaces around the equal sign. Also use quotes when your value has spaced in it:

将变量设置为值时,不要在等号周围放置空格。当您的值在其中间隔时也使用引号:

line="1234 abc xyz 5678"   # Must have quotation marks
word=1234                  # Quotation marks are optional

When you use comparisons, you mustleave white space around the brackets and the comparison sign:

使用比较时,必须在括号和比较符号周围留出空格:

if [[ $line == *$word* ]]; then
    echo $line
fi

Note that double square brackets. If you are doing pattern matching, you mustuse the double square brackets and not the single square brackets. The double square brackets mean you're doing a pattern match operation when you use ==or =. If you use single square brackets:

注意双方括号。如果您进行模式匹配,则必须使用双方括号而不是单方括号。双方括号表示您在使用==or时正在执行模式匹配操作=。如果您使用单个方括号:

if [ "$line" = *"$word"* ]

You're doing equality. Note that double square brackets don't need quotation marks while single brackets it is required in most situations.

你在做平等。请注意,双方括号不需要引号,而在大多数情况下需要单括号。

回答by tolanj

echo $line | grep "$word"

would be the typical way to do this in a script, of course it does cost a new process

将是在脚本中执行此操作的典型方法,当然它确实需要一个新过程

回答by Bentoy13

You can use the bash match operator =~:

您可以使用 bash 匹配运算符=~

 [[ "$line" =~ "$word" ]] && echo "$line"

Don't forget quotes, as stated in previous answers (especially the one of @Bill).

不要忘记引用,如先前答案中所述(尤其是@Bill 之一)。

回答by William Pursell

The reason that if [ "$line"==*"$word"* ]does not work as you expect is perhaps a bit obscure. Assuming that no files exist that cause the glob to expand, then you are merely testing that the string 1234 abc xyz 5678==*1234*is non empty. Clearly, that is not an empty string, so the condition is always true. You need to put whitespace around your ==operator, but that will not work because you are now testing if the string 1234 abc xyz 5678is the same as the string to which the glob *1234*expands, so it will be true only if a file named 1234 abc xyz 5678exists in the current working directory of the process executing the shell script. There are shell extensions that allow this sort of comparison, but grepworks well, or you can use a case statement:

if [ "$line"==*"$word"* ]没有按预期工作的原因可能有点模糊。假设不存在导致 glob 扩展的文件,那么您只是在测试字符串1234 abc xyz 5678==*1234*是否为非空。显然,这不是空字符串,因此条件始终为真。您需要在==操作符周围放置空格,但这将不起作用,因为您现在正在测试字符串1234 abc xyz 5678是否与 glob*1234*扩展到的字符串相同,因此仅当1234 abc xyz 5678当前工作目录中存在名为的文件时才会为真执行 shell 脚本的进程。有一些 shell 扩展允许进行这种比较,但grep效果很好,或者您可以使用 case 语句:

case "$line" in
*$word*) echo $line;;
esac

回答by NS23

Code Snippet:

代码片段:

$a='text'
$b='text'

if [ $a -eq $b ]
 then
   msg='equal'
fi 

回答by Baiyan Huang

An alternative solution would be using loop:

另一种解决方案是使用循环:

for w in $line
do
    if [ "$w" == "$word" ]; then
        echo $line
        break
    fi
done