kiaraRobles kiaraRobles - 2 months ago 6
Bash Question

Combine these two IF statements into one?

Any way to combine these two IF statements into one...

if [ -n "$(system_profiler SPPrintersDataType | grep Shared | grep Yes)" ]; then
echo 1
fi
if [ -n "$(system_profiler SPPrintersDataType | grep 'System Printer Sharing: Yes')" ]; then
echo 1
fi

Answer

Add && short circuit evaluation in between:

if [ -n ... ] && [ -n ... ]; then ## Something; fi 

In your case:

if [ -n "$(system_profiler SPPrintersDataType | grep Shared | grep Yes)" ] && [ -n "$(system_profiler SPPrintersDataType | grep 'System Printer Sharing: Yes')" ]; then 
    echo 1
fi

Just to note, if you use the bash keyword [[, then the following is valid too:

if [[ -n ... && -n ... ]]; then ## Something; fi