2012-04-24 4 views
1

-e /sudoers.tmp -o "$(pidof visudo)"が次のコードスニペットで何を理解しようとしていますか。bashに詳しい方

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

コードレビューのタッチ: 'cp -p'を使用します。 'bash -e'でスクリプトを呼び出してエラーが発生しないようにしてください。 –

+0

良い点。ありがとう。 –

+2

@glennjackman:あるいは、スクリプト内で 'set -e'や' set -o errexit'(読みやすくするため)をしてください。 – l0b0

答えて

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は、そのファイルが存在するかどうかをチェックしています。ファイル/etc/sudoers.tmpが存在するか、コマンドpidof visudoの復帰状態が実行されているvisudoを意味し、0でない場合

1

[ -e /etc/sudoers.tmp -o "$(pidof visudo)" ]氏は述べています。

+3

より正確には、出力が空でない場合、 '[" $(pidof visudo) "]'は真です。 –

関連する問題