Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am new to bash-programming, sorry if this might be a kind of stupid question. I found a script on my server and i am trying to understand every line:

#!/bin/sh
#
# man-db cron weekly

set -e

if ! [ -d /var/cache/man ]; then
    # Recover from deletion, per FHS.
    mkdir -p /var/cache/man
    chown man:root /var/cache/man || true
    chmod 2755 /var/cache/man
fi

# regenerate man database
if [ -x /usr/bin/mandb ]; then
# --pidfile /dev/null so it always starts; mandb isn't really a daemon,
# but we want to start it like one.
start-stop-daemon --start --pidfile /dev/null \
          --startas /usr/bin/mandb --oknodo --chuid man \
          -- --quiet
fi

exit 0

Some things i don't understand like

set -e

I read about it, that it "checks" your commands. So it gives warnings or an exception if something goes wrong? Or a more detailed error-report?

if ! [ -d /var/cache/man ]; then OR
if [ -x /usr/bin/mandb ]; then

What i don't understand are the "-d" or "-x" commands at the if clause, imho there is something missing there.

chown man:root /var/cache/man || true

I don't understand the last part, what's the reason for this "OR TRUE".

Maybe somebody can help me?

tia && regards noircc

share|improve this question

2 Answers 2

up vote 2 down vote accepted

set -e # Exit immediately if a command exits with a non-zero status.

The -d option in the if test is true if the path is a directory. ! negates the test.

    if ! [ -d /var/cache/man ]  # true if /var/cache/man is not a directory
    if [ -x /usr/bin/mandb ]    # true if /usr/bin/mandb is executable

Regarding chown man:root /var/cache/man || true - the code to the right of the || is actioned if the preceding chown command returns a non-zero exit status. If the chown command failed, true is evaluated. This is to prevent the script from failing - otherwise the chown non-zero error status would exit the script because of the set -e.

share|improve this answer

In bash, the -d switch is a "check if directory exists" and -x is for checking files. Set -e tells the script to exit if any commands fail (i.e, exit with a non-zero status).

The || true prevents a non-zero response from chown (if the command fails).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.