perl - How to determine if shell command didn't run or produced no output -


i executing shell commands via perl script , capturing output, this,

$commandoutput = `cat /path/to/file | grep "some text"`; 

i check if command ran or not this,

if(!$commandoutput) {     # command not run! } else {     # further processing } 

this works , output correctly. problem is, in cases, command not produce output. instance, text trying grep not present in target file, no output provided result. in case, script detects "command not run", while not true.

what correct way differentiate between these 2 cases in perl?

you can use know whether command failed or command return nothing

$val = `cat text.txt | grep -o '[0-9]*'`;     print "command failed" if (!$?);     print "empty string" if(! length($val) );     print "val = $val"; 

assume text.txt contain "123ab" want number only.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -