文件计数程序是shell不太正常工作

时间:2014-12-02 11:55:55

标签: linux bash shell

#!/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. 如果是普通文件,则将其视为
  10. 输出结果。

1 个答案:

答案 0 :(得分: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

它包含/计算&#34;。&#34;和&#34; ..&#34;文件也是目录。如果您不想这样,请在while循环结束时调整stat命令。