Unix-file-operators

提供:Dev Guides
移動先:案内検索

Unix/Linux-シェルファイルテストオペレーターの例

Unixファイルに関連付けられたさまざまなプロパティをテストするために使用できる演算子がいくつかあります。

変数 file には、サイズが100バイトで、 readwriteexecute のアクセス許可がある既存のファイル名「test」が含まれていると仮定します-

Operator Description Example
-b file Checks if file is a block special file; if yes, then the condition becomes true. [ -b $file ] is false.
-c file Checks if file is a character special file; if yes, then the condition becomes true. [ -c $file ] is false.
-d file Checks if file is a directory; if yes, then the condition becomes true. [ -d $file ] is not true.
-f file Checks if file is an ordinary file as opposed to a directory or special file; if yes, then the condition becomes true. [ -f $file ] is true.
-g file Checks if file has its set group ID (SGID) bit set; if yes, then the condition becomes true. [ -g $file ] is false.
-k file Checks if file has its sticky bit set; if yes, then the condition becomes true. [ -k $file ] is false.
-p file Checks if file is a named pipe; if yes, then the condition becomes true. [ -p $file ] is false.
-t file Checks if file descriptor is open and associated with a terminal; if yes, then the condition becomes true. [ -t $file ] is false.
-u file Checks if file has its Set User ID (SUID) bit set; if yes, then the condition becomes true. [ -u $file ] is false.
-r file Checks if file is readable; if yes, then the condition becomes true. [ -r $file ] is true.
-w file Checks if file is writable; if yes, then the condition becomes true. [ -w $file ] is true.
-x file Checks if file is executable; if yes, then the condition becomes true. [ -x $file ] is true.
-s file Checks if file has size greater than 0; if yes, then condition becomes true. [ -s $file ] is true.
-e file Checks if file exists; is true even if file is a directory but exists. [ -e $file ] is true.

次の例では、すべての*ファイルテスト*演算子を使用します-

可変ファイルが既存のファイル名 "/var/www/finddevguides/unix/test.sh" を保持し、そのサイズが100バイトで*読み取り*、*書き込み*および*実行*権限を持っていると仮定します-

#!/bin/sh

file="/var/www/finddevguides/unix/test.sh"

if [ -r $file ]
then
   echo "File has read access"
else
   echo "File does not have read access"
fi

if [ -w $file ]
then
   echo "File has write permission"
else
   echo "File does not have write permission"
fi

if [ -x $file ]
then
   echo "File has execute permission"
else
   echo "File does not have execute permission"
fi

if [ -f $file ]
then
   echo "File is an ordinary file"
else
   echo "This is sepcial file"
fi

if [ -d $file ]
then
   echo "File is a directory"
else
   echo "This is not a directory"
fi

if [ -s $file ]
then
   echo "File size is not zero"
else
   echo "File size is zero"
fi

if [ -e $file ]
then
   echo "File exists"
else
   echo "File does not exist"
fi

上記のスクリプトは、次の結果を生成します-

File does not have write permission
File does not have execute permission
This is sepcial file
This is not a directory
File size is not zero
File does not exist

ファイルテスト演算子を使用している間、次の点を考慮する必要があります-

  • 演算子と式の間にはスペースが必要です。 たとえば、2+ 2は正しくありません。 2+と書く必要があります。 2。
  • if …​ then …​ else …​ fi ステートメントは、次の章で説明されている意思決定ステートメントです。