busybox和UbuntuRaspbianetc之间在 "dash"(-)上的 "tr "命令的行为差异。

问题描述 投票:0回答:1

我在一个脚本中使用了一个函数来验证输入字符串是否包含任何不可接受的字符。 在这个例子中,允许的字符是字母、数字、下划线、破折号、句号和空格。

#!/bin/sh
pattern="\_\-\. [a-zA-Z0-9]"
while [ 1 ]; do
echo "enter your test string"
read string
echo "result:"
echo "$string" | tr -cd "$pattern" | sed 's/\[//' | sed 's/\]//' 
echo
echo
done

在Raspbian(树莓皮)上进行测试。

pi@raspberrypi:~ $ ./trtest.sh 
enter your test string
dash-dash
result:
dash-dash
enter your test string
under_score
result:
under_score

在洋葱板(OpenWRTbusybox)上测试:

root@Omega-FD22:~# ./trtest.sh 
enter your test string
dash-dash
result:
dashdash  <<<----- I'm not expecting this
enter your test string
under_score
result:
under_score

所以 #1 我不知道为什么在这两种情况下 "tr "会有不同的行为 特别是在 "破折号 "字符上。

#2如果有其他方法,我愿意接受。

谢谢你的任何见解。

DL

tr busybox
1个回答
0
投票

顺便说一下,我的一个勤奋的同事发现了这个问题,所以我把他的解决方案告诉大家。 如果你移动 "\-" 到模式匹配字符串的结尾,那么它就可以在两种环境中工作。 我无法解释其中的技术哲学基础,但我很高兴它能工作。

之前:在我的能力范围内,我很高兴它能工作。

pattern="\_\-\. [a-zA-Z0-9]"

After:

pattern="\_\. [a-zA-Z0-9]\-"
© www.soinside.com 2019 - 2024. All rights reserved.