茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671760 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00144.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2016/8/16 2:19:05
Linux/UNIX: Find Out If File Exists With Conditional Expressions
by VIVEK GITE on FEBRUARY 16, 2006 last updated JANUARY 25, 2016

With the help of BASH shell and IF command, it is possible to find out if file exists or not on the filesystem. A conditional expressions (also know as “evaluating expressions”) can be used by [[ compound command and the test ([) builtin commands to test file attributes and perform string and arithmetic comparisons.

Syntax

The general syntax is:
[ parameter FILE ]

OR
test parameter FILE

OR
[[ parameter FILE ]]

Where parameter can be any one of the following:
-e: Returns true value if file exists.
-f: Return true value if file exists and regular file.
-r: Return true value if file exists and is readable.
-w: Return true value if file exists and is writable.
-x: Return true value if file exists and is executable.
-d: Return true value if exists and is a directory.

Please note that the [[ works only in Bash, Zsh and the Korn shell, and is more powerful; [ and test are available in POSIX shells. Let us see some examples.

Find out if file /etc/passwd file exist or not

Type the following commands:
$ [ -f /etc/passwd ] && echo "File exist" || echo "File does not exist"
$ [ -f /tmp/fileonetwo ] && echo "File exist" || echo "File does not exist"


[[ example
Type the following commands:
$ [[ -f /etc/passwd ]] && echo "File exist" || echo "File does not exist"
$ [[ -f /tmp/fileonetwo ]] && echo "File exist" || echo "File does not exist"


Find out if directory /var/logs exist or not
Type the following commands:
$ [ -d /var/logs ] && echo "Directory exist" || echo "Directory does not exist"
$ [ -d /dumper/fack ] && echo "Directory exist" || echo "Directory does not exist"


[[ example
$ [[ -d /var/logs ]] && echo "Directory exist" || echo "Directory does not exist"
$ [[ -d /dumper/fake ]] && echo "Directory exist" || echo "Directory does not exist"


Are two files are the same?

Use the -ef primitive with the [[ new test command:
[[ /etc/resolv.conf -ef /etc/resolv.conf ]] && echo "Same files" || echo "Noop"
[[ /etc/resolv.conf -ef /etc/passwd ]] && echo "Same files" || echo "Noop"

Shell script example

You can use conditional expressions in a shell script:
#!/bin/bash
FILE="$1"

if [ -f "$FILE" ];
then
   echo "File $FILE exist."
else
   echo "File $FILE does not exist" >&2
fi
Save and execute the script:
$ chmod +x script.sh
$ ./script.sh /path/to/file
$ ./script.sh /etc/resolv.conf


You can use this technique to verify that backup directory or backup source directory exits or not in shell scripts. See example script for more information.

原文出處:Linux/UNIX: Find Out If File Exists With Conditional Expressions
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|