Search⌘ K
AI Features

Exercise: If Statement

Learn to convert Bash commands with logical operators into clear if statements. This exercise helps you write readable scripts that copy or remove files depending on whether they contain a specific string, reinforcing your understanding of conditional statements in Bash.

We'll cover the following...

Exercise

Below is a Bash command:

( grep -RlZ "bash" target | xargs -0 cp -t . && echo "cp - OK" || ! echo "cp - FAILS" ) && ( grep -RLZ "bash" target | xargs -0 rm && echo "rm - OK" || echo "rm - FAILS" )

It looks for the string “bash” in the files of the directory named target. If the file ...