This post focuses on one question:
Q: was previous cmd successful@@
A: (based on [[ teach yourself shell programming ]])
if [ $? -eq 0 ] ….
Background: i wasn’t sure if my own solutions were reliable. Now this author also believed in [ $? -eq 0 ]
cmd1 && … # tests 0. [[ learning the bash shell ]]
cmd1 || .. # tests 0.
“0 means OK” is standard convention. A script or command can break the convention, but you are allowed to assume no one does.