문제

I'm studying System V init scripts found in /etc/init.d/ in current Debian 7.4.0 wheezy release(But its also present in other, previous, releases). Almost all of them (from existing services) found in that folder end with, basically an empty line containing nothing but a colon (:) sign. Even the 'skeleton' template file that can be found there for the purpose of writing your own init scripts has this. Here is a copy/paste from the end of the code:

esac

:

(that was the end of an case statement and then there is the end of the file)

What is also interesting is that there is no exit 0 or exit $? call, except only in some conditions in the case statement, that would be called otherwise so it seems as if that colon sign is a sort of a replacement for it? Full skeleton file code is here: https://gist.github.com/ivankovacevic/9917139

What could that colon be and why?

도움이 되었습니까?

해결책

The colon is a syntactic element that, essentially, does nothing, but returns true. It can be used whereever a command can be used.

It is sometimes needed where sh(1) requires a statement. E.g., this gives an error:

if [ "$a" = "" ] ; then
  # comment out this part for now
  # echo yes
else
  echo no
fi

bash: syntax error near unexpected token `else'

Replacing the comment with a : makes it work:

if [ "$a" = "" ] ; then
  # comment out this part for now
  : echo yes
else
  echo no
fi

It is rarely needed to explicitly use "exit 0" in the shell; in the absence of an exit statement, the shell script exits with the status of the last command, a shell script that just executes

/bin/false

will give an exit status 1:

$ echo $?
1

The colon is largely black magic, and I learned what little I know about it from experimentation.

다른 팁

The : returns true status in BASH and is just a replacement for the word true. I don't see any benefit to using : as apposed to exit 0 at the end of a system init script other than to save characters or make it less readable.

As chepner point's out, : would ensure a true status without exiting the shell if the script is sourced.

It is also commonly used to replace logical negation using ! in statements.

if [[ $var ]]; then
    :
else
    echo no
fi

Is the same as:

if ! [[ $var ]]; then
    echo no
fi
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top