yin
yin

Reputation: 21

why this shell script output so strange

#!/bin/bash
echo "===========3333333====="

if [ $0 == "test" ] || $0 == "all" ];then
    echo "---"
fi

endless loop output:

===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====
===========3333333=====

although I know the wrong is Missing square brackets if [ $0 == "test" ] || [ $0 == "all" ];then but why output like that???

Upvotes: 2

Views: 47

Answers (2)

ersinakyuz
ersinakyuz

Reputation: 31

$0 == "all" ];then

this section is starting the recursion. because $0 starting the script again.

for example below code is producing same result

#!/bin/bash
echo "===========3333333====="
$0 bash is not checking below!!!!!!!!!!
        echo "ok"
fi

Upvotes: 1

William Pursell
William Pursell

Reputation: 212248

Consider the line if [ $0 == "test" ] || $0 == "all" ]

This is of the form if cmd1 || cmd2, where cmd1 is [ $0 == "test ] and cmd2 is $0 == "all" ]

That second command is invoking your script with arguments ==, all, and ]. So you've got a recursion.

Remember, [ is not part of the shell grammar. It is just a command with the strange feature of requiring that its last argument be ].

Upvotes: 4

Related Questions