SHELL------判斷文件類型

SHELL腳本練習:判斷文件類型;

設計思路:

[ -z "$a" ]   是否爲空
[ -e "file" ] 是否存在
[ -f "file" ] 普通文件
[ -b "file" ] 塊設備
[ -S "file" ] 套接字
[ -c "file" ] 字符設備
[ -L "file" ] 軟鏈接

exit 0 代表正常運行程序並退出程序,

exit 1 代表非正常運行導致退出程序;

#!/bin/bash
[ -z "$1" ] && {
    echo "please enter the file name!"
    exit 1
}
[ -e "$1" ] || {
        echo "This file does not exist!"
        exit 1
} 

[ -f "$1" ] && {
        echo "The file is a normal file."
} 
[ -b "$1" ] && {
        echo "The file is a block device."
}  
[ -S "$1" ] && {
        echo "The file is a socket."
} 
[ -c "$1" ] && {
        echo "The file is a character device.0"
}  
[ -L "$1" ] && {
        echo "The file is a soft link"

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章