Unix 和 Linux Cut 命令等同于 For Loop DOS 命令

Unix and Linux Cut command to equivalent with the For Loop DOS command

场景

我有这个 OpenSSL 命令,输出到管道以进行切割。

openssl x509 -serial -noout -in cacert.pem | cut -d= -f2 > serial

我只想保留序列号,但要使用 cmd 循环

openssl x509 -serial -noout -in cacert.pem
serial=E314E555E2D52FC8

我希望的结果示例

openssl x509 -serial -noout -in cacert.pem | FOR /F "tokens=2 delims==" %G IN ("pipe=result") DO @echo %G>serial

DOS 没有像您正在尝试的那样的 FOR 循环,但我怀疑您指的不是 DOS,因为 OpenSSL 尚未移植到该操作系统。如果你的意思是Windows,那么你需要的命令是:

(for /f "tokens=2 delims==" %a in ('openssl x509 -serial -noout -in cacert.pem') do @echo %a) > serial

如果将以上内容放入 .bat 文件中,则需要将 %a 更改为 %%a