linux shell

编程入门 行业动态 更新时间:2024-10-27 15:16:14
linux shell - 无法将字符串与:adb shell getprop ro.product.brand进行比较(linux shell - can't compare strings with: adb shell getprop ro.product.brand)

这真的很奇怪......我无法在我的linux shell中得到这个测试结果,并且我找不到原因。

#!/bin/bash a=$(adb shell getprop ro.product.brand) adb shell getprop ro.product.brand if [ "$a" == "Huawei" ]; then echo "Success" else echo "Failed" fi

该脚本只输出:

Huawei Failed

鉴于这个脚本:

b=$(whoami) whoami if [ "$b" == "amo" ]; then echo "Success" else echo "Failed" fi

...输出:

amo Success

任何人都可以帮我理解吗?

我已经尝试通过管道剪切或sed剪切$ a中的空格或换行符,但是我得到相同的结果...

This is really odd... I can't get this test to result in true in my linux shell and I can't figure out why.

#!/bin/bash a=$(adb shell getprop ro.product.brand) adb shell getprop ro.product.brand if [ "$a" == "Huawei" ]; then echo "Success" else echo "Failed" fi

The script just outputs:

Huawei Failed

Whereas this script:

b=$(whoami) whoami if [ "$b" == "amo" ]; then echo "Success" else echo "Failed" fi

...outputs:

amo Success

Can anyone help me understand that?

I already tried cutting away spaces or line breaks in $a by piping to cut or sed but I get the same result...

最满意答案

我建议这是删除前导/尾随空白的一种方法:

# Trims $1 # If $2 supplied, assigns result to variable named $2 # If $2 not present, echoes the value to stdout trim() { if [[ $1 =~ ^[[:blank:]]*(.*[^[:blank:]])[[:blank:]]*$ ]] then local result="${BASH_REMATCH[1]}" else local result="$1" fi if (( $# > 1 )) then printf -v "$2" %s "$result" else printf %s "$result" fi }

该功能不使用外部程序,因此开销较低。

也许是正则表达式的快速解释...

^[[:blank:]]*(.*[^[:blank:]])[[:blank:]]*$ 它匹配所有主要和尾随空白(在那里并不意外) 在中间,它匹配以非空白结尾的任何字符串,并将其保存为用BASH_REMATCH访问的子表达式 如果没有指定结束中间部分的“非空白”字符,那么贪婪的.*会将所有东西都吃掉,直到字符串结束,包括尾随空白。 另一方面, .*是肯定以非空白开始的,因为贪婪的初始[[:blank:]]*只会在遇到非空白时停止。

根据您的需要,您也可以使用[[:space:]]而不是[[:blank:]] (区别解释如下: https : //en.wikipedia.org/wiki/Regular_expression#Character_classes )。 基本上, [[:blank:]]匹配制表符和空格,而[[:space:]]也匹配换行符,回车符等等。

I suggest this as a way to remove leading/trailing whitespace :

# Trims $1 # If $2 supplied, assigns result to variable named $2 # If $2 not present, echoes the value to stdout trim() { if [[ $1 =~ ^[[:blank:]]*(.*[^[:blank:]])[[:blank:]]*$ ]] then local result="${BASH_REMATCH[1]}" else local result="$1" fi if (( $# > 1 )) then printf -v "$2" %s "$result" else printf %s "$result" fi }

This function uses no external program, so has low overhead.

Maybe a quick explanation of the regular expression...

^[[:blank:]]*(.*[^[:blank:]])[[:blank:]]*$ It matches all leading and trailing whitespace (no surprise there) In the middle, it matches any string of characters that ends with a non-blank and saves that as a sub-expression for access with BASH_REMATCH If there were no "non-blank" character specified to end the middle portion, the greedy .* would eat everything up until the end of the string, including trailing blanks. The .* is, on the other hand, certain to begin with a non-blank, because the greedy initial [[:blank:]]* will only stop when encountering a non-blank.

Depending on your need, you may also use [[:space:]] instead of [[:blank:]] (difference explained here : https://en.wikipedia.org/wiki/Regular_expression#Character_classes). Basically, [[:blank:]] matches tabs and spaces, and [[:space:]] also matches newlines, carriage returns, and a few more.

更多推荐

本文发布于:2023-08-03 04:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:linux   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!