3,525 瀏覽數

Linux command – file & test

file

因為 Linux 不依據檔尾的名稱來判斷是什麼檔案,所以我們可以使用 #file 這個程式來判斷檔案的格式.而這個程式是依據 /usr/share/magic 所定義的檔頭來做分辨.

[root@benjr ~]# file /etc/passwd
/etc/passwd: ASCII text
[root@benjr ~]# file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped

下次遇到不確定的檔案可先用 #file 來確認.

test

test 一樣可以檢查檔案,方式很多種,先以下面的範例來做說明.

  • -e FILE
    FILE exists

當執行指令時都會回傳一個執行後的代碼給變數 $?,成功的執行完指令後會回傳一個 0 值(有錯誤時會回傳 錯誤代碼).
利用 test 的指令來看看 變數 $? 的變化.

root@ubuntu:~# test -e /etc/passwd
root@ubuntu:~# echo $?
0
root@ubuntu:~# test -e /etc/passwd11
root@ubuntu:~# echo $?
1

可以很明顯看到當 test 成功後回傳得值為 0 ,失敗則回傳 1 .

通常變數 $? 常會與 && || 一起使用,我們現來看一下 && || 的用法

  • cmd1 && cmd2 : 若 cmd1 執行正確且無錯誤 ($?=0),則執行 cmd2 ,反之若 cmd1 執行完畢且為錯誤 ($?≠0),則 cmd2 不執行.
  • cmd1 || cmd2 : 若 cmd1 執行正確且無錯誤 ($?=0),則 cmd2 不執行 ,反之若 cmd1 執行完畢且為錯誤 ($?≠0),則開始執行 cmd2.

下面的範例的語法是常用的架構 (這 && 與 || 的順序就是固定),用以判斷 command1 的執行是成功還是失敗,成功則執行 command2 失敗則執行 command 2

command1 && command2 || command3

使用上面常用的 && || 的判斷式的架構範例.

root@ubuntu:~# test -e /etc/passwd && echo "File exist" || echo "File not exist"
File exist
root@ubuntu:~# test -e /etc/passwd11 && echo "File exist" || echo "File not exist"
File not exist

其他可以用參數.

  • -b FILE
    FILE exists and is block special
  • -c FILE
    FILE exists and is character special
  • -d FILE
    FILE exists and is a directory
  • -f FILE
    FILE exists and is a regular file
  • -g FILE
    FILE exists and is set-group-ID
  • -G FILE
    FILE exists and is owned by the effective group ID
  • -h FILE
    FILE exists and is a symbolic link (same as -L)
  • -k FILE
    FILE exists and has its sticky bit set
  • -L FILE
    FILE exists and is a symbolic link (same as -h)
  • -O FILE
    FILE exists and is owned by the effective user ID
  • -p FILE
    FILE exists and is a named pipe
  • -r FILE
    FILE exists and read permission is granted
  • -s FILE
    FILE exists and has a size greater than zero
  • -S FILE
    FILE exists and is a socket
  • -t FD
    file descriptor FD is opened on a terminal
  • -u FILE
    FILE exists and its set-user-ID bit is set
  • -w FILE
    FILE exists and write permission is granted
  • -x FILE
    FILE exists and execute (or search) permission is granted

除了比較一個檔案,test 還可以比較兩個檔案.

  • FILE1 -ef FILE2
    FILE1 and FILE2 have the same device and inode numbers
  • FILE1 -nt FILE2
    FILE1 is newer (modification date) than FILE2
  • FILE1 -ot FILE2
    FILE1 is older than FILE2

下面的例子用來比較兩個檔案的新舊,並觀察變數 $? 的變化.

root@ubuntu:~# test /etc/passwd -nt /etc/group
root@ubuntu:~# echo $?
0
root@ubuntu:~# test /etc/passwd -ot /etc/group
root@ubuntu:~# echo $?
1

其他可以用參數.

  • STRING1 == STRING2
    the strings are equal
  • STRING1 != STRING2
    the strings are not equal
  • INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2
  • INTEGER1 -ge INTEGER2
    INTEGER1 is greater than or equal to INTEGER2
  • INTEGER1 -gt INTEGER2
    INTEGER1 is greater than INTEGER2
  • INTEGER1 -le INTEGER2
    INTEGER1 is less than or equal to INTEGER2
  • INTEGER1 -lt INTEGER2
    INTEGER1 is less than INTEGER2
  • INTEGER1 -ne INTEGER2
    INTEGER1 is not equal to INTEGER2
  • -n STRING
    the length of STRING is nonzero
  • -z STRING
    the length of STRING is zero

發表迴響