read value from file and compare, why syntax error?

Paul Smith paul at mad-scientist.net
Mon Dec 14 00:36:01 UTC 2020


On Mon, 2020-12-14 at 01:15 +0100, Ralf Mardorf via ubuntu-users wrote:
> By the way "case in" is way faster, so when "if else" makes sense, it
> could be better to use "case in" instead.

case is often not faster in reality, since most shells have "[" (and
"test") as builtins.  However there are certainly great reasons to use
case: it's much more flexible for one thing.  Definitely case is
something every shell programmer should have in their toolbox!

However, just to be clear for the OP, if and case are not really
interchangeable.

The if condition is a _command_ that the shell runs, and the condition
is true if the command succeeds (exits with a 0 exit code) and the
condition is false if the command fails (exits with a non-0 exit code).

case does string matching.


So, if statements have the syntax:

  if <command>; then
    ...

(same with while <command>; do) and case statements have the syntax:

  case <string> in
    ...

If your if-condition is a string comparison then case can be
substituted, but if your if-condition is anything else then case won't
help.





More information about the ubuntu-users mailing list