[{"data":1,"prerenderedAt":107},["ShallowReactive",2],{"/2025/10/17-linux-script-conditional-conditions":3},{"id":4,"title":5,"body":6,"date":98,"description":47,"extension":99,"meta":100,"navigation":101,"path":102,"robots":103,"seo":104,"stem":105,"__hash__":106},"posts/2025/10/17 linux-script-conditional-conditions.md","17 Linux Script Conditional Conditions",{"type":7,"value":8,"toc":95},"minimark",[9,18,25,28,31,34,48,51,54,62,65,73,76,84,87],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"linux-script-conditional-conditions","Linux Script Conditional Conditions",[19,20,21],"notes",{},[22,23,24],"p",{},"This is a repost from my old blog. First posted in 3/21/2019.",[26,27],"br",{},[22,29,30],{},"Like most people, I guess, I came from Windows background. Just recently, I have projects exploring multiple flavors of Linux and spending a lot of time to understand how conditions work in bash and/or shell script in Linux. And then my code didn't work and that took me on a journey.",[22,32,33],{},"Long time ago, I tried to do something as simple as the following:",[35,36,37],"code-block",{},[38,39,44],"pre",{"className":40,"code":42,"language":43},[41],"language-text","if (\u003Cexpr1> or \u003Cexpr2>) and (\u003Cexpr3> or \u003Cexpr4>) then \u003Cdo this> fi\n","text",[45,46,42],"code",{"__ignoreMap":47},"",[22,49,50],{},"But things get complicated as the expressions involve which and grep commands.",[22,52,53],{},"For example, I'm checking if any python is installed, so my first attempt was",[35,55,56],{},[38,57,60],{"className":58,"code":59,"language":43},[41],"if [ \"`which python`\" = \"\" -a \"`which python3`\" = \"\" ]; then echo \"no python\"; fi\n",[45,61,59],{"__ignoreMap":47},[22,63,64],{},"then I realize that in RedHat, if there is no python installed, it will return a string containing \"no python\" instead of empty string, so my code becomes",[35,66,67],{},[38,68,71],{"className":69,"code":70,"language":43},[41],"if [ \\( \"`which python`\" = \"\" -o \"`which python | grep 'no python'`\" = \"\" \\) -a \\( \"`which python3`\" = \"\" -o \"`which python3 | grep 'no python3'`\" = \"\" \\) ]; then \n\n     echo \"no python\"; \n\nfi\n",[45,72,70],{"__ignoreMap":47},[22,74,75],{},"Well, it didn't work. Scrutinizing it a bit more. I found out that when there is no python, grep doesn't return empty string, but instead a failure code, so that turns my code into:",[35,77,78],{},[38,79,82],{"className":80,"code":81,"language":43},[41],"if [ \\( \"`which python`\" = \"\" -o \"`which python | grep 'no python'`\" \\) -a \\( \"`which python3`\" = \"\" -o \"`which python3 | grep 'no python3'`\" \\) ]; then \n\n     echo \"no python\"; \n\nfi\n",[45,83,81],{"__ignoreMap":47},[22,85,86],{},"Which seems to work so far, but for consistency, I prefer to use the flag -z to check for empty string and -n to check for non empty string, so the code becomes",[35,88,89],{},[38,90,93],{"className":91,"code":92,"language":43},[41],"if [ \\( -z \"`which python`\" -o -n \"`which python | grep 'no python'`\" \\) -a \\( -z \"`which python3`\"  -o -n \"`which python3 | grep 'no python3'`\" \\) ]; then \n\n     echo \"no python\"; \n\nfi\n",[45,94,92],{"__ignoreMap":47},{"title":47,"searchDepth":96,"depth":96,"links":97},2,[],"2025-10-17T00:00:00.000Z","md",{},true,"/2025/10/17-linux-script-conditional-conditions",null,{"title":5,"description":47},"2025/10/17 linux-script-conditional-conditions","NRhaO2aoFHmcKQuLcscmiFU-VEvqyNDDv-730xJjNzw",1785167452828]