比较 Busybox ash 中的子串
compare substrings in Busybox ash
这是我第一次使用 Whosebug。我目前遇到一个问题,在此分享详细信息。
我目前正在构建 POS 自动化脚本。 POS 终端有 Shell Busybox ash 。这就是为什么我无法使用基本命令,因为它们的行为不同。
下面是查询:
假设
[[ $I == $N$A ]] - 此比较用于精确匹配,其中 $I 是较大的字符串,$N$A 包含 $I 的子字符串。
我使用了 [ -z ${I##$N$A* ] 和 [ “$I” == “$N$A”* ] 语法来比较子字符串,但它失败了并且没有表现得像它应该的那样。
请大侠指导。
请让我知道是否有 busybox ash 的在线控制台,我可以在其中测试一些脚本。
示例已添加 -27-08-16
假设 -
导出值 $I = "Credit.saleApproved"
的脚本
我正在传递 $N= "Credit" and $A= ".sale"
的值
所以基本上 echo $N$A 是 echo $I 的 substring
我写这个伪逻辑是为了让你更好地理解
If [[ $I == $N$A ]]
then
echo "sale is complete"
else
echo "sale is declined"
fi
我只需要 -->
1 . input : $I = Credit.saleApproved
$N$A = Credit.sale
Output :sale is complete
2.input : $I = Credit.sApproved
$N$A = Credit.sale
Output :sale is Declined
Bourne Again SHell supports some comparisons that are not supported by other shells, such as Busybox ash. Some common pitfalls are enlisted here
只有 bash 支持与 [[ ... ]]
的具体比较,以及在比较中使用通配符 (*
)。
如果你想用灰来搭配,你可以试试这些:
[ "$I" == "$N$A" ] # Match exactly
[ "$I" != "${I#$N$A}" ] # Starts with
[ "$I" != "${I%$N$A}" ] # Ends with
要检查一个字符串是否包含其他字符串,我想不出使用 shell 表达式的简单方法,ash 不支持像 ${I/$N$A}
这样的字符串替换。有多种工具可供选择,例如 grep
和 sed
.
使用 grep
你可以:
if echo $I|grep "$N$A" - > /dev/null; then ...
使用 sed
你可以:
[ -z $(echo "$I"|sed "/$N$A/d") ] # Contains
但是有很多方法可以实现。
与该问题有点无关,但我 运行 在尝试比较日期字符串时跨过此 post 并认为它对遇到类似问题的任何人都有用。要在 ash 中的字符串上使用大于或小于,您需要在比较字符前加一个正斜杠:
str2="2020-1-1"
str3="2020-1-2"
# greater or less than
if [ "$str2" \> "$str3" ]; then
echo "$str2 is greater then $str3"
elif [ "$str2" \< "$str3" ]; then
echo "$str2 is less then $str3"
fi
这是我第一次使用 Whosebug。我目前遇到一个问题,在此分享详细信息。
我目前正在构建 POS 自动化脚本。 POS 终端有 Shell Busybox ash 。这就是为什么我无法使用基本命令,因为它们的行为不同。 下面是查询:
假设 [[ $I == $N$A ]] - 此比较用于精确匹配,其中 $I 是较大的字符串,$N$A 包含 $I 的子字符串。 我使用了 [ -z ${I##$N$A* ] 和 [ “$I” == “$N$A”* ] 语法来比较子字符串,但它失败了并且没有表现得像它应该的那样。
请大侠指导。 请让我知道是否有 busybox ash 的在线控制台,我可以在其中测试一些脚本。
示例已添加 -27-08-16
假设 -
导出值 $I = "Credit.saleApproved"
的脚本
我正在传递 $N= "Credit" and $A= ".sale"
的值
所以基本上 echo $N$A 是 echo $I 的 substring
我写这个伪逻辑是为了让你更好地理解
If [[ $I == $N$A ]]
then
echo "sale is complete"
else
echo "sale is declined"
fi
我只需要 -->
1 . input : $I = Credit.saleApproved
$N$A = Credit.sale
Output :sale is complete
2.input : $I = Credit.sApproved
$N$A = Credit.sale
Output :sale is Declined
Bourne Again SHell supports some comparisons that are not supported by other shells, such as Busybox ash. Some common pitfalls are enlisted here
只有 bash 支持与 [[ ... ]]
的具体比较,以及在比较中使用通配符 (*
)。
如果你想用灰来搭配,你可以试试这些:
[ "$I" == "$N$A" ] # Match exactly
[ "$I" != "${I#$N$A}" ] # Starts with
[ "$I" != "${I%$N$A}" ] # Ends with
要检查一个字符串是否包含其他字符串,我想不出使用 shell 表达式的简单方法,ash 不支持像 ${I/$N$A}
这样的字符串替换。有多种工具可供选择,例如 grep
和 sed
.
使用 grep
你可以:
if echo $I|grep "$N$A" - > /dev/null; then ...
使用 sed
你可以:
[ -z $(echo "$I"|sed "/$N$A/d") ] # Contains
但是有很多方法可以实现。
与该问题有点无关,但我 运行 在尝试比较日期字符串时跨过此 post 并认为它对遇到类似问题的任何人都有用。要在 ash 中的字符串上使用大于或小于,您需要在比较字符前加一个正斜杠:
str2="2020-1-1"
str3="2020-1-2"
# greater or less than
if [ "$str2" \> "$str3" ]; then
echo "$str2 is greater then $str3"
elif [ "$str2" \< "$str3" ]; then
echo "$str2 is less then $str3"
fi