我正在尝试从一个程序有多个版本的目录中获取最大版本号
I am trying to get the max version number from a directory where i have several versions of one program
例如,如果ls的输出是
for example if output of ls is
something01_1.sh something02_0.1.2.sh something02_0.1.sh something02_1.1.sh something02_1.2.sh something02_2.0.sh something02_2.1.sh something02_2.3.sh something02_3.1.2.sh something.sh我正在获得具有以下内容的最大版本号-
I am getting the max version number with the following -
ls somedir | grep some_prefix | cut -d '_' -f2 | sort -t '.' -k1 -r | head -n 1现在,如果同时我想用系统中已有的版本号检查它,那么最好的方法是什么...
Now if at the same time i want to check it with the version number which i already have in the system, whats the best way to do it...
在bash中,我可以正常工作(如果当前版本为2.5)
in bash i got this working (if 2.5 is the current version)
(ls somedir | grep some_prefix | cut -d '_' -f2; echo 2.5) | sort -t '.' -k1 -r | head -n 1还有其他正确的方法吗?
is there any other correct way to do it?
在上面的示例中,some_prefix是something02.
In the above example some_prefix is something02.
这里的实际问题是
(ls smthing; echo more) | sort这是将两个命令/程序的输出合并到第三个命令的最佳方法.
is it the best way to merge output of two commands/program for piping into third.
推荐答案我找到了解决方案.看来最好的方法是使用流程替换.
I have found the solution. The best way it seems is using process substitution.
cat <(ls smthing) <(echo more) | sort以我的版本示例
cat <(ls somedir | grep some_prefix | cut -d '_' -f2) <(echo 2.5) | sort -t '.' -k1 -r | head -n 1为了将来的读者的利益,我建议-请放弃一线诱饵,并使用glob作为 chepner 建议.
for the benefit of future readers, I recommend - please drop the lure of one-liner and use glob as chepner suggested.
在超级用户上提出了几乎类似的问题.
Almost similar question is asked on superuser.
有关流程替换的更多信息.
更多推荐
我如何将一些文本附加到没有临时文件的管道中
发布评论