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

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00038.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]Find Out If File Exists With Conditional Expressions

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Find Out If File Exists With Conditional Expressions
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
前一個主題 | 下一個主題 | | | |

討論串




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