Bash程序,用于使用不显示任何数字的函数计算目录中的某些类型的文件

时间:2016-02-23 03:58:09

标签: bash shell

我的程序需要搜索文件目录中的所有非隐藏文件,并在用户输入文件名作为参数时查找其权限,如下所示:

./search.sh file*

然后显示具有该名称和特定权限的文件数。问题是,当我输入文件名时,程序根本没有显示数字。 任何帮助将不胜感激!

#!/bin/bash

oRE()
{
  find . -type f -readable -executable -name "$1" | wc -l
}

nonEx()
{
  num=0
  num=$(find . -type f -name "$1" | wc -l)
  if [[ $num -eq 0 ]];
   then
      echo 1
  else
      echo $num
  fi
}

dir()
{
  find "$1" -maxdepth 0 -type d | wc -l
}

oR()
{
  find . -type f -readable -name "$1" | wc -l
}

tByt()
{
  wc -c < "$1"
}

if [[ "$#" -eq 1 ]];
 then
   echo normal, readable, and executable     : $(oRE "$1")
   echo does not exist                       : $(nonEx "$1")
   echo directory                            : $(dir "$1")
   echo normal and readable                  : $(oR "$1")
   echo total bytes in normal files          : $(tByt "$1")
fi

这就是我得到的:

 normal, readable, and executable     : 1
 does not exist                       : 2
find: `sample*': No such file or directory
 directory                            : 0
 normal and readable                  : 2
./search.sh: line 32: sample*: No such file or directory
 total bytes in normal files          :

1 个答案:

答案 0 :(得分:0)

您对脚本的命令行将扩展为当前目录中的任何匹配文件作为单独的参数。有多个参数,由于if语句,脚本不会执行任何操作。尝试引用参数'file*'