为什么我要在Windows终端获得Gibberish结果?

发布于 2025-01-22 17:48:18 字数 1006 浏览 0 评论 0原文

我正在尝试比较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

Windows terminal result

Result in Git Bash

Bash result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2025-01-29 17:48:18

看来您正在比较版本。在PowerShell和.net框架中,已经有 [版本] 做到这一点:

PS C:\Users> [version]"2.5.7" -le [version]"2.5.6"
False
PS C:\Users> [version]"2.4.10" -lt [version]"2.4.9"
False
PS C:\Users> [version]::new(2, 4, 8) -lt [version]::new(2, 4, 10)
True
PS C:\Users> [version]"2.5.6" -le [version]"2.5.6"
True
PS C:\Users> [version]::new(2, 5, 6) -lt [version]::new(2, 5, 6)
False

请参阅 powerShell比较操作员有关更多信息,


请注意,Windows终端是A 端子可以't任何东西。与其他终端(例如Conhost(旧窗口上的默认终端),术语,X term,iterm,konsole ...运行命令的东西)相同上班。因此,说“在Windows终端运行”几乎没有意义,因为错误来自外壳。您的命令是bash命令,因此显然如果您在Windows终端中运行bash,则它将正确运行。目前尚不清楚您正在使用哪个外壳,但

  • 在PowerShell [“ $ 1” =“ echo -e” $ 1 \ n $ 2“ | sort -v | head -n1`”]完全未能解析,因为在PowerShell 中代码>在PowerShell中是操作员,而不是正常字符,例如
  • CMD $不是可变和参数替换的字符,也没有特殊的含义。 ,因此,如果您的路径中有一个[。EXE

不要尝试运行为另一个完全不同的外壳写的命令

It seems you're comparing versions. In PowerShell and .NET framework there are already [version] to do that:

PS C:\Users> [version]"2.5.7" -le [version]"2.5.6"
False
PS C:\Users> [version]"2.4.10" -lt [version]"2.4.9"
False
PS C:\Users> [version]::new(2, 4, 8) -lt [version]::new(2, 4, 10)
True
PS C:\Users> [version]"2.5.6" -le [version]"2.5.6"
True
PS C:\Users> [version]::new(2, 5, 6) -lt [version]::new(2, 5, 6)
False

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

  • In PowerShell [ "$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
  • In cmd $ 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 anything

Each shell has its own syntax, don't try to run commands written for a shell on another completely different one

御弟哥哥 2025-01-29 17:48:18

对包含版本编号的列表正确,引用如何按文件名与Windows Explorer使用的方式进行排序?,这是我尝试转换为PowerShell语法的尝试:

# compare two arbitrary version strings using natural sorting
# allows strings to contain non-numeric components
function verlte {
    param(
        [string]$v1,
        [string]$v2
    )

    function ToNatural {
        param( [string]$v )
        [regex]::Replace($v, '\d+', { $args[0].Value.PadLeft(20) })
    }
    
    (ToNatural $v1) -le (ToNatural $v2)
}

function verlt {
    param(
        [string]$v1,
        [string]$v2
    )

    if ($v1 -eq $v2) { return $false }
    verlte $v1 $v2
}

verlte 2.5.7 2.5.6 # False
verlt 2.4.10 2.4.9 # False
verlt 2.4.8 2.4.10 # True
verlte 2.5.6 2.5.6 # True
verlt 2.5.6 2.5.6 # False

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:

# compare two arbitrary version strings using natural sorting
# allows strings to contain non-numeric components
function verlte {
    param(
        [string]$v1,
        [string]$v2
    )

    function ToNatural {
        param( [string]$v )
        [regex]::Replace($v, '\d+', { $args[0].Value.PadLeft(20) })
    }
    
    (ToNatural $v1) -le (ToNatural $v2)
}

function verlt {
    param(
        [string]$v1,
        [string]$v2
    )

    if ($v1 -eq $v2) { return $false }
    verlte $v1 $v2
}

verlte 2.5.7 2.5.6 # False
verlt 2.4.10 2.4.9 # False
verlt 2.4.8 2.4.10 # True
verlte 2.5.6 2.5.6 # True
verlt 2.5.6 2.5.6 # False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文