&2 exit 1 fi #checks for a valid n-6ren">
gpt4 book ai didi

linux - 文件计数程序是 shell 不太工作

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:20 24 4
gpt4 key购买 nike

#!/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. 输出结果。

最佳答案

在阅读了您的脚本和描述后,我想先让您了解一下我准备的东西。我认为这是不言自明的。您的要求 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 命令。

关于linux - 文件计数程序是 shell 不太工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27248824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com