# In this script, we will demonstrate how commands can be chained. # This is sometimes useful in ‹if› conditions, but also in other # scenarios. We already know pipes: those are a form of chaining # too. But we are now interested in simpler things. You know that if # you enter two commands on separate lines (in a script, or in an # interactive session), those commands will run in a sequence. A # semicolon does the same thing: echo foo ; echo bar # That is not very interesting, but sometimes useful, if you want to # run a sequence of commands at once and each of them takes a while # and you don't want to watch for each to finish so you can type in # the next one. # The next two ways are more interesting: the ‹&&› and ‹||› operators: true && echo "this command will run" false && echo "this command will not run" # Just like in C, && is short-circuiting: if the first command # fails, the result must always be false and running the second # command is not needed. Same (but opposite) with ‹||›: true || echo "this command will not run" false || echo "this command will run" # We can form a longer chain, too: true && echo "this will run" || echo "but this won't" false && echo "this won't run" || echo "but this will" # Of course, you can use those operators in conditionals: if true && false; then echo "not happening"; fi if test -f file.txt && test -f file.bak; then echo "both file.txt and file.bak exist and are regular files" fi # This is all I wanted to say about chaining for now. We might look # at the ‹&› operator later in the semester, if there's enough time. # For now, let's move on to loops: # # $ micro loops.sh