2011-01-12 45 views
73

我試圖在Ubuntu中編寫一個非常簡單的腳本,它允許我將它傳遞給文件名或目錄,並且能夠在某個文件,以及其他目錄中的其他內容。我遇到的問題是當目錄名稱或文件太多時,名稱中包含空格或其他易變字符。檢查傳遞的參數是否是BASH中的文件或目錄

下面是我的基本代碼下面,和幾個測試。

#!/bin/bash 

PASSED=$1 

if [ -d "${PASSED}" ] ; then 
    echo "$PASSED is a directory"; 
else 
    if [ -f "${PASSED}" ]; then 
     echo "${PASSED} is a file"; 
    else 
     echo "${PASSED} is not valid"; 
     exit 1 
    fi 
fi 

而這裏的輸出:

[email protected]~ $ ./scripts/testmove.sh /home/andy/ 
/home/andy/ is a directory 

[email protected]~ $ ./scripts/testmove.sh /home/andy/blah.txt 
/home/andy/blah.txt is a file 

[email protected]~ $ ./scripts/testmove.sh /home/andy/blah\ with\ a\ space.txt 
/home/andy/blah with a space.txt is not valid 

[email protected]~ $ ./scripts/testmove.sh /home/andy\ with\ a\ space/ 
/home/andy with a space/ is not valid 

所有這些路徑都有效,而且存在。

+4

`if`-`else`結構還支持`elif`。只是FYI。 – 2011-01-12 03:22:50

回答

102

這應該工作。我不確定它爲什麼失敗。你正確地引用你的變量。如果您使用雙腳本腳本[[]]會發生什麼情況?

if [[ -d $PASSED ]]; then 
    echo "$PASSED is a directory" 
elif [[ -f $PASSED ]]; then 
    echo "$PASSED is a file" 
else 
    echo "$PASSED is not valid" 
    exit 1 
fi 

雙方括號是[ ]的bash擴展名。它不需要引用變量,即使它們包含空格。

也值得一試:-e測試路徑是否存在,而不測試它是什麼類型的文件。

7

至少亂寫濃密的樹代碼:

#!/bin/bash 

PASSED=$1 

if [ -d "${PASSED}" ] 
then echo "${PASSED} is a directory"; 
elif [ -f "${PASSED}" ] 
then echo "${PASSED} is a file"; 
else echo "${PASSED} is not valid"; 
    exit 1 
fi 

當我將它放入一個文件「xx.sh」,並創建一個文件「XX SH」,並運行它,我得到:

$ cp /dev/null "xx sh" 
$ for file in . xx*; do sh "$file"; done 
. is a directory 
xx sh is a file 
xx.sh is a file 
$ 

既然你有問題,也許你應該添加一行調試腳本:

ls -l "${PASSED}" 

這將顯示你ls認爲你通過腳本的名字。

-1

更新: 我被低估了,所以我決定重新寫我的答案,感謝您的反饋。

使用「文件」命令可能是這個有用:

#!/bin/bash 
check_file(){ 

if [ -z "${1}" ] ;then 
echo "Please input something" 
return; 
fi 

f="${1}" 
result="$(file $f)" 
if [[ $result == *"cannot open"* ]] ;then 
     echo "NO FILE FOUND ($result) "; 
elif [[ $result == *"directory"* ]] ;then 
     echo "DIRECTORY FOUND ($result) "; 
else 
     echo "FILE FOUND ($result) "; 
fi 

} 

check_file ${1} 

輸出的例子:

$ ./f.bash login 
DIRECTORY FOUND (login: directory) 
$ ./f.bash ldasdas 
NO FILE FOUND (ldasdas: cannot open `ldasdas' (No such file or directory)) 
$ ./f.bash evil.php 
FILE FOUND (evil.php: PHP script, ASCII text) 

FYI:上述工作的答案,但你可以使用-s怪異的情況下,幫助通過檢查有效的文件第一:

#!/bin/bash 

check_file(){ 
    local file="${1}" 
    [[ -s "${file}" ]] || { echo "is not valid"; return; } 
    [[ -d "${file}" ]] && { echo "is a directory"; return; } 
    [[ -f "${file}" ]] && { echo "is a file"; return; } 
} 

check_file ${1} 
-2
#!/bin/bash                        
echo "Please Enter a file name :"                   
read filename                        
if test -f $filename                      
then                          
     echo "this is a file"                    
else                          
     echo "this is not a file"                   
fi 
2

使用-f-d交換機上/bin/test:在猛砸

F_NAME="${1}" 

if test -f "${F_NAME}" 
then         
    echo "${F_NAME} is a file" 
elif test -d "${F_NAME}" 
then 
    echo "${F_NAME} is a directory" 
else         
    echo "${F_NAME} is not valid" 
fi 
相關問題