说我有一个输入文件和一个输出文件(test.in和test.out)和一个程序“./myprogram”,标准输入重定向来自test.in并使用结果这是从标准输出捕获与test.out进行比较比较输入和输出 - bash
究竟如何我可以比较
即时通讯思想,
if [ $(myprogram < test.in) == $(cat test.out) ]
有什么建议?
说我有一个输入文件和一个输出文件(test.in和test.out)和一个程序“./myprogram”,标准输入重定向来自test.in并使用结果这是从标准输出捕获与test.out进行比较比较输入和输出 - bash
究竟如何我可以比较
即时通讯思想,
if [ $(myprogram < test.in) == $(cat test.out) ]
有什么建议?
使用cmp
&dagger;并指定-
作为要比较的文件之一,该文件告诉其使用stdin。
if myprogram < test.in | cmp -s - test.out; then
如果你想两个命令的输出比较,而无需创建临时文件,使用<(cmd)
功能。 (在man bash
搜索 “过程替代”。)
if cmp -s <(myprogram < test1.in) <(myprogram < test2.in); then
&匕首;或diff
如果你想知道有什么区别。
我会使用差异。类似这样的:
if cat test.in | myprogram | diff - test.out
也许还会使用'-q'(quiet)选项来进行比较。 –
是的,虽然“安静”仍然打印出一些信息。 –
'-'被假定为文件? – Thatdude1
我是否必须制作一个tmp文件来将.out文件与.in输入的程序的实际结果相匹配? – Thatdude1
@Beginnernato大多数shell程序将'-'视为stdin的简写形式。它也可以通过伪文件'/ dev/stdin'来访问。 –