tr 命令在 cygwin bash.exe shell 中导致问题,但在 mintty.exe 中有效
题
我使用 Windows 和 cygwin。以下命令应输出字符串“./bootstrp3.cab”中“/”出现的次数。当我使用 mintty.exe shell 并输出“1”时它起作用:
echo ./bootstrp3.cab | tr -cd / | wc -c
不幸的是,它使用 bash.exe shell 输出以下消息
Usage:
tr [-cs] string1 string2
tr -s[-c] string1
tr -d[-c] string1
tr -ds[-c] string1 string2
看起来这两个 shell 的行为不同,但它们似乎都是同一种 shell,因为当我 echo $0
, bash.exe 输出 bash
和 mintty.exe 输出 -bash
.
我还尝试使用 bourne shell (sh.exe) 命令,但它输出与 bash.exe shell 相同的消息。
有谁知道如何让该命令与 sh.exe 或 bash.exe shell 一起使用?有人知道为什么两个 bash shell 的行为不同吗?
解决方案
你可以使用做同样的事情 sed
和 wc
:
echo -n ./bootstrp3.cab | sed 's![^/]!!g' | wc -c
不隶属于 StackOverflow