Unix-file-operators
提供:Dev Guides
Unix/Linux-シェルファイルテストオペレーターの例
Unixファイルに関連付けられたさまざまなプロパティをテストするために使用できる演算子がいくつかあります。
変数 file には、サイズが100バイトで、 read 、 write 、 execute のアクセス許可がある既存のファイル名「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バイトで*読み取り*、*書き込み*および*実行*権限を持っていると仮定します-
上記のスクリプトは、次の結果を生成します-
ファイルテスト演算子を使用している間、次の点を考慮する必要があります-
- 演算子と式の間にはスペースが必要です。 たとえば、2+ 2は正しくありません。 2+と書く必要があります。 2。
- if … then … else … fi ステートメントは、次の章で説明されている意思決定ステートメントです。