更熟悉bash

时间:2012-04-24 01:44:37

标签: bash

我想了解-e /sudoers.tmp -o "$(pidof visudo)"在以下代码段中的作用。

#!/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

欣赏有关该特定条件的详细说明。

2 个答案:

答案 0 :(得分:4)

此:

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

正在使用test命令。要查看-e测试的作用,您可以运行help test并查看输出:

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.

因此,-e /etc/sudoers.tmp正在检查该文件是否存在。

答案 1 :(得分:1)

[ -e /etc/sudoers.tmp -o "$(pidof visudo)" ]表示文件/etc/sudoers.tmp存在或命令pidof visudo的返回状态不为0,表示visudo正在运行。

相关问题