1

I am trying to understand what -e /sudoers.tmp -o "$(pidof visudo)" does in the following code snippet.

#!/bin/bash

if [ -e /etc/sudoers.tmp -o "$(pidof visudo)" ]; then 
  echo "/etc/sudoers busy, try again later"
  exit 1
fi

cp /etc/sudoers /etc/sudoers.bak
cp /etc/sudoers /etc/sudoers.tmp

chmod 0640 /etc/sudoers.tmp
echo "whatever" >> /etc/sudoers.tmp
chmod 0440 /etc/sudoers.tmp

mv /etc/sudoers.tmp /etc/sudoers

exit 0

Appreciate an elaboration on that specific condition.

4

2 回答 2

4

This:

if [ -e /etc/sudoers.tmp -o "$(pidof visudo)" ]; then 

is using the test command. To see what the -e test does, you can run help test and look at the output:

Evaluate conditional expression.

Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR.  Expressions may be unary or binary.  Unary
expressions are often used to examine the status of a file.  There
are string operators and numeric comparison operators as well.

The behavior of test depends on the number of arguments.  Read the
bash manual page for the complete specification.

File operators:

  [...]
  -e FILE        True if file exists.

So, -e /etc/sudoers.tmp is checking to see if that file exists.

于 2012-04-24T01:48:14.587 回答
1

[ -e /etc/sudoers.tmp -o "$(pidof visudo)" ] says if the file /etc/sudoers.tmp exists or the return state of the command pidof visudo is not 0, which means visudo is running.

于 2012-04-24T01:55:46.070 回答