Perl one-liner if otherwise the logic

advertisements

I'm evaluating whether or not certain variables match expected values. The variables are set in memory by a certain program, the values of which can be access from the shell with a custom program.

I'm piping the output of the shell command to awk to get the specific field I want and then I want to run it through perl to see if it matches the expected value. For example,

ysgrp autostart | awk -F\: '{print $1}' | perl -e 'print {"True"} else {print "False"} if /on/'

However, I'm getting complaints from perl about compilation errors near "} else". How does one handle if/then/else logic in a perl one-liner?


You can't use an else condition in a postfix conditional. You can either use a ternary conditional operator like this:

perl -e 'print /on/ ? "True" : "False"'

Or use explicit blocks like this:

perl -e 'if ( /on/ ) { print "True" } else { print "False" }'