2014-12-02 12 views
0
#!/bin/bash 
# File Count 


if (($# < 2)) 
then 
echo "${0}: ERROR: Incorrect number of arguments entered." 1>&2 
echo "${0}: USAGE: ${0} <directory> <filename>" 1>&2 
exit 1 
fi 

#checks for a valid number of arguments then exits if the user doesn't provide them and  shows an error message. 

if [[ ! -d "${1}" ]] 
then 
echo "${0}: ERROR: directory ${1} does not exist." 1>&2 
echo "${0}: USAGE: ${0} <directory> <filename>" 1>&2 
exit 2 
fi 
#checks for a the directory the user specified then exits if the user doesn't provide a valid directory and shows an error message. 


if [[ -d "${1}" ]] 
then 
typeset -i directoryCount=0 
for files in $(ls) 
do 
((directoryCount++)) 
done 

# if it's a directory file add 1 to the directory count 


if [[ -x "${1}" ]] 
then 
typeset -i executableCount=0 
for files in $(ls) 
do 
((executableCount++)) 
done 
# if it's a executable file add 1 to the executable count 

if [[ -f "${1}" ]] 
then 
typeset -i ordinaryCount=0 
for files in $(ls) 
do 
((ordinaryCount++)) 
done 
# if it's a ordinary file add 1 to the ordinary count 

echo The number of directory files is "${directoryCount}" 
echo The number of executable files is "${executableCount}" 
echo The number of ordinary files is "${ordinaryCount}" 

#display file counts 

雖然我認爲我是沿着正確的線路有麻煩這個程序。任何人都可以提供洞察力?以下是它需要做的事情。謝謝。文件計數程序是不是工作的外殼

  1. 如果目錄的路徑名爲空,則退出並顯示錯誤消息。
  2. 如果給出的目錄路徑名沒有命名現有目錄,則退出時出現 錯誤消息。
  3. 如果無法讀取目錄內容,則退出並顯示錯誤消息。
  4. 初始化變量,通過目錄中的每個文件命名爲保持三項罪名
  5. 循環:
  6. ,如果它是一個目錄就當是這樣的;
  7. 如果它是可執行計數它是這樣
  8. 如果它是一個普通的文件並將其視作這樣
  9. 輸出結果。
+0

'if [[-d「$ {1}」]]'總是會爲真,因爲您已經檢查過相反的情況。不要[解析ls](http://mywiki.wooledge.org/ParsingLs)只使用'for file in *'。這裏的問題是關於如何添加'is executable'邏輯? – 2014-12-02 12:35:10

+0

爲什麼需要第二個從未使用的參數?爲什麼不使用'find'將管道輸出到'wc'? – 2014-12-02 17:46:31

+0

在這些事情中,我總是和這些事情一起奮鬥的邏輯, – user3017497 2014-12-03 01:16:31

回答

1

我想給你一個在閱讀你的腳本和描述後製作的東西的開端。我認爲這是不言而喻的。你的要求1和3沒有實現,因爲1我相信回退到當前的工作目錄更有用。

這是非常基本的腳本,它應該給你足夠的空間,增強:

#!/usr/bin/env bash 

# Either set DIR upon calling or submit a parameter or fall back to the CWD 
: ${DIR:="${1:-.}"} 

if [ ! -d "${DIR}" ]; then 
    echo "${0##*/}: ERROR: Directory '${DIR}' does not exist." 
    echo "${0##*/}: USAGE: ${0##*/} <directory>" 
    exit 1 
else 
    echo "Investigating directory: ${DIR}" 
fi 

function initVars() { 
    directoryCount=0 
    executableCount=0 
    ordinaryCount=0 
    unknownTypeCount=0 
} 

function checkOwnerExecutable() { 
    local permStr=$1 
    # return 0 if executable bit is set, otherwise 1 
    [ "x${owner//x/}" != "x${owner}" ] && return 0 || return 1 
} 

initVars 
while read entry; do 
    #echo "entry: ${entry}" 
    eval ${entry} 
    case "$type" in 
     "Directory") 
      let directoryCount+=1 
      ;; 
     "Regular File") 
      let ordinaryCount+=1 
      checkOwnerExecutable $owner && let executableCount+=1 
      ;; 
     *) 
      echo "Unknown type=$type with name=$name" 
      let unknownTypeCount+=1 
      ;; 
    esac 
done < <(stat -f 'name="%N" type="%HT" owner="%SHp" group="%SMp" other="%SLp"' ${DIR}/* ${DIR}/.*) 

echo "The number of directory files is ${directoryCount}" 
echo "The number of executable files is ${executableCount}" 
echo "The number of ordinary files is ${ordinaryCount}" 
echo "The number of unknown type files is ${unknownTypeCount}" 

這裏是/ tmp目錄輸出:

$ ./fileCount.sh /tmp 
Investigating directory: /tmp 
Unknown type=Socket with name=/tmp/textmate-501.sock 
Unknown type=Socket with name=/tmp/.prl_pcsc_gate 
The number of directory files is 11 
The number of executable files is 1 
The number of ordinary files is 4 
The number of unknown type files is 2 

它包括/計數「 「。和「..」文件作爲目錄。如果你不想要,在while循環結束時調整stat命令。