0

As far as I understand, an interpreter runs code line by line; but I find probable that my reasoning is incorrect. If in this example case the first if line returns true, will the program know to skip to fi after echo "APPROVED" (and will it know which line fi is in?) or will it still take some time to check the next lines, and not execute them when they are inside an elif or else?

if [ $AVERAGE -ge "6" ] 
then
        echo "APPROVED"
        # Does shell know how to directly get from here /\...
elif  [ $AVERAGE -lt "2" ]
then
        echo "FAILED"
else
        echo "RETAKE TEST"
# ... To here? \/
fi

Thank you!

Hermitao
  • 3
  • 2
  • Does it matter much how the exact implementation works? Anyway, all shells I've seen parse the whole `if` construct before executing anything in it. But optimization? – ilkkachu Mar 18 '21 at 18:17
  • Of necessity there's syntax checking. I wouldn't imagine it would go much beyond that – roaima Mar 18 '21 at 19:09
  • 2
    All shells *must* parse the whole of a compound command before running it, because there could be trailing operators that affect the entire execution (a pipe or redirection, it's backgrounded). – Michael Homer Mar 18 '21 at 19:41
  • @MichaelHomer, d'oh, good point. Thanks for pointing that out. – ilkkachu Mar 18 '21 at 21:03
  • if first `if` condition is true,the first `then` part will be executed, and shell will proceed execution **after** `fi`. the second `elif` will not be tested, and no other part will be looked for (except to validate syntax as @roaima pointed out) – Archemar Mar 18 '21 at 21:25
  • Thanks everyone for the input. @MichaelHomer, so would it be more correct to say the shell parses by command instead of by line? You can also post your comment as an answer if you want as it answers my original question! – Hermitao Mar 18 '21 at 21:27

1 Answers1

1

As Michael Horner pointed out, a shell must parse the entire command first because if it's in a pipeline or is backgrounded, that will affect the execution of the command. This is true of any command that the shell executes, and it's pretty obvious if you write a multi-line shell command at an interactive prompt that it does so, since it will wait for you to complete the command before executing it.

The shell will correctly execute each condition and stop once it reaches a case where the condition is true. It will not execute further elif conditions. The behavior of Unix shells is well specified by POSIX.

Whether the shell has optimizations to make things run faster or more efficiently is a quality of implementation issue. A small shell, such as busybox's, would probably prefer to be as small as possible at the cost of a small amount of speed, whereas other shells might prefer to optimize things more.

In general, shell scripting isn't a very efficient way to write code, since you end up forking and execing a large number of processes, so in my experience optimizations like you'd find in a typical optimizing compiler tend to be absent, since they don't improve things that much. It's generally more efficient to provide more built-in commands, which avoid the need for spawning external processes at all and give you more bang for your buck.

bk2204
  • 3,571
  • 5
  • 9