[英]Easy one-liner for bash/linux, to check if a PNG is valid?
pngcheck
is almost perfect. pngcheck
几乎是完美的。 But in its simplest form, pngcheck
outputs a line beginning with either OK:
or ERROR:
. 但是在最简单的形式中, pngcheck
输出一行以OK:
或ERROR:
开头。 This means I have to parse an output string, and I would rather just check a return value. 这意味着我必须解析输出字符串,我宁愿只检查一个返回值。
I looked at the pngcheck.c
source code, and a thorough check for PNG validity is quite the ordeal - much more than just a magic number check. 我查看了pngcheck.c
源代码,对PNG有效性进行彻底检查是非常严峻的考验 - 远远超过了魔术数字检查。 So the best I can do at the moment, is to create a simple pngcheck.sh
which calls pngcheck
and parses the output string, followed by exit 0
or exit 1
accordingly. 所以我现在能做的最好的事情就是创建一个简单的pngcheck.sh
,它调用pngcheck
并解析输出字符串,然后依次exit 0
或exit 1
。
But I wanted to check if there is an easy or more 'bashonic' solution. 但我想检查一下是否有一个简单或更“b”的解决方案。
Thanks! 谢谢!
After looking at the source code I think that you can just use $?
看完源代码后我觉得你可以用$?
that holds exit status of the previous command in many shells. 在许多shell中保存上一个命令的退出状态。 See line 4684 in pngcheck.c
where success message is printed. 请参阅pngcheck.c
中的第4684行,其中打印了成功消息。 If there was error global_error
wouldn't be set to 0 and would be passed to and returnd by main()
. 如果有错误, global_error
将不会设置为0并将被传递给main()
并返回。 Things got easy now: 事情变得容易了:
#!/usr/bin/env sh
if pngcheck "$1" > /dev/null 2>&1
then
echo things went ok
else
echo things went bad
fi
Usage: 用法:
$ ./check-png.sh /usr/share/icons/HighContrast/22x22/status/touchpad-disabled.png
things went ok
$ ./check-png.sh /tmp/FILE
things went bad
When I look in the source code of pngcheck.c (PNGcheck, version 2.3.0 of 7 July 2007), I believe it does set the return code. 当我查看pngcheck.c的源代码(PNGcheck,2007年7月7日的2.3.0版)时,我相信它确实设置了返回码。 Near the end of main( ): 接近main()的末尾:
717 if (num_errors > 0)
718 err = (num_errors > 127)? 127 : (num_errors < 2)? 2 : num_errors;
719 else if (num_warnings > 0)
720 err = 1;
...
num_errors is the number of files that failed, num_warnings is the number of files that had a warning. num_errors是失败的文件数,num_warnings是发出警告的文件数。 Then it exits with "return err;" 然后以“返回错误”退出
So the return code is 0 for all ok, 1 for warnings only, and 2 or higher is the number of files that failed (max 127). 所以返回代码为0表示所有ok,1表示仅警告,2或更高表示失败的文件数(最多127)。
And that is also consistent with the small test I did on the binary installed on Ubuntu. 这也与我在Ubuntu上安装的二进制文件上做的小测试一致。
pngcheck -q /etc/profile >/dev/null; echo $? # returns 2
pngcheck -q cpu50.png >/dev/null; echo $? # returns 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.