bash、破折号和字符串比较

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

bash, dash and string comparison

bashdash-shellstring-comparison

提问by LiraNuna

I am trying to compare two strings in a simple shell script. I was using /bin/shinstead of /bin/bash, and after countless hours of debugging, it turns out sh (which is actually dash) can't handle this block of code:

我试图在一个简单的 shell 脚本中比较两个字符串。我使用的是/bin/sh而不是/bin/bash,经过无数个小时的调试,结果是 sh(实际上是破折号)无法处理这段代码:

if [ "$var" == "string" ]
then
    do something
fi

What is a portable way to compare strings using /bin/sh? I know I can always do the opposite by using !=, but I am wondering about a cleaner, portable way.

使用 比较字符串的便携方法是什么/bin/sh?我知道我总是可以通过使用 != 来做相反的事情,但我想知道一种更清洁、更便携的方式。

回答by J-16 SDiZ

dashis a very strict POSIX shell, if it work in dashit is almost certain it would work in other POSIX shell.

dash是一个非常严格的 POSIX shell,如果它在dash它里面工作几乎可以肯定它会在其他 POSIX shell 中工作。

Try:

尝试:

if [ "$var" = "string" ]
then
    some_command
fi

回答by paxdiablo

Why is there even a possibility that your script will be run by the "wrong" shell? I would think you could make that a pre-requisite of your product by using the standard sh-bang line at the top of your script:

为什么你的脚本有可能被“错误的”shell 运行?我认为您可以通过使用脚本顶部的标准 sh-bang 行将其作为产品的先决条件:

#!/bin/bash

Even if a user usesa different shell, the other shells are generally still there and, if not, simply complain and state that they are a pre-req.

即使用户使用不同的外壳,其他外壳通常仍然存在,如果没有,只需抱怨并声明它们是先决条件。

Exactly the same way that a specific kernel level, or the existence of awk, can be a pre-req.

与特定内核级别或 awk 的存在可以成为前置请求的方式完全相同。

For your specific question, I believe both shand bashallow the single '=' to be used for string comparisons - that is POSIX behavior:

对于您的具体问题,我相信两者shbash允许将单个 '=' 用于字符串比较 - 这是 POSIX 行为:

if [ "a" = "a" ] ; then
    echo yes
fi

yes

回答by skoob

Use =instead of ==. Comparisons are handled by test(1). /usr/bin/[is typically a link to /usr/bin/test. The only difference is that if you use [in a shell script, the ]is required as well.

使用=代替==。比较由 test(1) 处理。/usr/bin/[通常是指向/usr/bin/test. 唯一的区别是,如果您[在 shell 脚本中使用,则]也是必需的。

Note that bash has a built-in test/[, so it doesn't actually use /usr/bin/test.

请注意,bash 有一个内置的test/ [,因此它实际上并不使用/usr/bin/test.

回答by mikeserv

The answers already posted are certainly correct, but it may be worthwhile to note that occasionally parameter expansion can serve the same purpose with perhaps some additional flexibility.

已经发布的答案当然是正确的,但值得注意的是,有时参数扩展可以达到相同的目的,但可能具有一些额外的灵活性。

% p() { printf 'notvar = %b\n' "${notvar##"${string1}"}${string2}" ; }
% string1='some stuff about things\c'
% string2='some different stuff maybe'
% notvar="$string1" p
> 'some different stuff maybe'
% notvar="$string2" p
> 'some stuff about things'

Ok, so the above isn't super-useful as is, but also consider that you can use the similar methods for testing variables in here-documents, in-line variable assignments if necessary (to a degree...), or even just as a shorter (and faster!) means of writing your first statement.

好的,所以上面的内容并不是非常有用,但也要考虑到您可以使用类似的方法来测试此处文档中的变量,如有必要(在某种程度上......),甚至可以使用内联变量赋值就像编写第一个语句的更短(更快!)方式一样。

[ ! "${var##"string"}" ] && _MATCH || _NOMATCH

Or even...

甚至...

[ ${#var#*"${s=string}"} -lt ${#var} ] && _SUB_STRING_TEST=TRUE

Possibly even...

甚至可能...

% p() { printf '%s is %s of %s' "" "${var_chk-not}" ""
> }<<HEREDOC
> ${in="${1##*""*}"}
> ${in:-
>     ${in="${1##""}"}
>     ${in:-${var_chk=all}
>     ${var_chk=some}
> }
> HEREDOC
%

回答by ghostdog74

you can use awk

你可以使用 awk

awk 'BEGIN{
 string1="test"
 string2="tes1t"
 if(s1==s2){
    print "same string"
 }else{
    print "not same"
 }
}'