I have the following script ~/bin/cat
that uses pygmentize
to display syntax highlightable files when ever possible if not just regular old cat
.
#!/bin/bash
for var; do
pygmentize "$var" 2> /dev/null
if [ $? -ne 0 ]; then
/bin/cat "$var"
fi
done
This works fine on my work machine but not on my home machine. At home if pygmentize
doesn't recognize a file it displays the same error message but the exit status is 0 where as at work it returns 1, which breaks the script. The only difference being at work I run Fedora and at home Ubuntu.
$ pygmentize testfile
Error: no lexer for filename 'testfile' found
$ echo $?
0
$ file testfile
file: ASCII text
This is strange as both are the same version
$ pygmentize -V
Pygments version 1.4, (c) 2006-2008 by Georg Brandl.
I could grep
for Error
in stderr
but how do I do this without throwing away stdout
, How should I handle this?