为什么我要在Windows终端获得Gibberish结果?
我正在尝试比较Windows终端中的版本。它在git bash中效果很好,但是在Windows终端中显示出粗略和错误的结果?
verlte() {
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
verlt() {
[ "$1" = "$2" ] && return 1 || verlte $1 $2
}
verlte 2.5.7 2.5.6 && echo "yes" || echo "no" # no
verlt 2.4.10 2.4.9 && echo "yes" || echo "no" # no
verlt 2.4.8 2.4.10 && echo "yes" || echo "no" # yes
verlte 2.5.6 2.5.6 && echo "yes" || echo "no" # yes
verlt 2.5.6 2.5.6 && echo "yes" || echo "no" # no
导致Windows终端
git bash
I am trying to compare version in windows terminal. It works great in Git Bash but in Windows terminal it shows gibberish and wrong result?
verlte() {
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
verlt() {
[ "$1" = "$2" ] && return 1 || verlte $1 $2
}
verlte 2.5.7 2.5.6 && echo "yes" || echo "no" # no
verlt 2.4.10 2.4.9 && echo "yes" || echo "no" # no
verlt 2.4.8 2.4.10 && echo "yes" || echo "no" # yes
verlte 2.5.6 2.5.6 && echo "yes" || echo "no" # yes
verlt 2.5.6 2.5.6 && echo "yes" || echo "no" # no
Result in Windows terminal
Result in Git Bash
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在比较版本。在PowerShell和.net框架中,已经有
[版本]
做到这一点:请参阅 powerShell比较操作员有关更多信息,
请注意,Windows终端是A 端子 和可以't任何东西。与其他终端(例如Conhost(旧窗口上的默认终端),术语,X term,iterm,konsole ...运行命令的东西)相同上班。因此,说“在Windows终端运行”几乎没有意义,因为错误来自外壳。您的命令是bash命令,因此显然如果您在Windows终端中运行bash,则它将正确运行。目前尚不清楚您正在使用哪个外壳,但
[“ $ 1” =“ echo -e” $ 1 \ n $ 2“ | sort -v | head -n1`”]
完全未能解析,因为在PowerShell中代码>在PowerShell中是操作员,而不是正常字符,例如
$
不是可变和参数替换的字符,也没有特殊的含义。 ,因此,如果您的路径中有一个
[。EXE
不要尝试运行为另一个完全不同的外壳写的命令
It seems you're comparing versions. In PowerShell and .NET framework there are already
[version]
to do that:See PowerShell Comparison Operators for more information
Note that Windows Terminal is a terminal and can't run anything. Same to other terminals like conhost (the default terminal on older Windows), term, xterm, iterm, konsole... The thing that runs commands is called the shell and a shell must be connected to some terminal to work. So saying "running in Windows Terminal" makes almost no sense because the error comes from the shell. Your command is a bash command so obviously if you run bash in Windows Terminal then it'll run properly. It's unclear which shell you're using but
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
completely fails to parse because in PowerShell`
is an escape character (which means your command doesn't terminate), and=
,[
/]
in PowerShell are an operator instead of a normal character like in bash$
isn't the character for variable and argument substitution, and`
also has no special meaning, so if there's a[.exe
in your path then cmd will pass the whole literal command to the[
command which will fail to compare anythingEach shell has its own syntax, don't try to run commands written for a shell on another completely different one
从对包含版本编号的列表正确,引用如何按文件名与Windows Explorer使用的方式进行排序?,这是我尝试转换为PowerShell语法的尝试:
Borrowing from Sort a list that contains version numbers properly, which references How to sort by file name the same way Windows Explorer does?, here's my attempt at converting to PowerShell syntax: