A script contains the line `mkdir /srv/data && cp report.txt /srv/data/`. Under what condition does the cp run?

LPIC-1 Exam 102-500, objective 105. Shells and shell scripting medium

Draft — not checked yet. This question was written from the published exam objectives and cites the clause it comes from, but nobody has yet read it against that clause and signed for it.

It is kept out of search results and out of mock exams until they have. Read the source below before you take the answer as settled.

The options

Not correct Always, because && only separates commands on one line.

Wrong. The plain separator that always runs the next command is `;`. `&&` adds a condition.

Not correct Only if mkdir writes nothing to standard output.

Wrong. Output on stdout or stderr is irrelevant to && and ||. Only the numeric exit status is examined.

Correct Only if mkdir exits with status 0.

Correct. `A && B` runs B only when A succeeded. Here that avoids copying into a directory that could not be created.

Not correct Only if mkdir exits with a non-zero status.

Wrong, that is `||`. `A || B` runs B only when A failed, which is the idiom behind `command || exit 1`.

Why

&& and || are short-circuit list operators driven purely by exit status: && runs the right-hand side on success (status 0), || runs it on failure (non-zero). They chain left to right with equal precedence, so `A || B && C` is read as `(A || B) && C`, not as an if/else.

Where this comes from

Cited
LPI exam objective 105.2
What it says
Use conditional execution based on the exit status of a command.

Practise this

Reading one question is not practice. The trainer will draw a set from objective 105 and space the ones you get wrong.

Practise LPIC-1 Exam 102-500