-2

このテーマについて多くの回答を見てきましたが、 を使用してこれを行いたくありません find。私はこれを書いたが、何かがうまくいかない:

function CountEx()
{
    count=0
    for file in `ls $1`
    do
        echo "file is $file"
        if [ -x $file ]
        then
            count=`expr $count + 1`
        fi
    done
    echo "The number of executable files in this dir is: $count"
}
while getopts x:d:c:h opt
do
    case $opt in
        x)CountEx $OPTARG;;
        d)CountDir $OPTARG;;
        c)Comp $OPTARG;;
        h)help;;
        *)echo "Please Use The -h Option to see help"
        break;;
    esac
done

このスクリプトを次のように使用しています。

yaser.sh -x './..../...../.....'

シェルはそれを実行し、出力します: The number of executable files in this dir is: 0 このディレクトリに多くの実行可能ファイルがある場合。

4

2 に答える 2

0

To count the number of executable (like the title says)

 count=0 
 for file in yourdir/*; do 
     if [ -x $file ]; then 
         count=$((count+1)); 
     fi; 
 done; 
 echo "total ${count}"

To count folders, just change the -x test with -d

于 2013-02-12T12:42:06.300 に答える
0

ディレクトリを数えることが目的であれば、非常に多くのオプションがあります。

findあなたが望んでいないと言った方法:

CountDir() {
  if [[ ! -d "$1" ]]; then
    echo "ERROR: $1 is not a directory." >&2
    return 1
  fi
  printf "Total: %d\n" $(find "$1" -depth 1 -type d | wc -l)
}

あなたのfor例に似た方法:

CountDir() {
  if [[ ! -d "$1" ]]; then
    echo "ERROR: $1 is not a directory." >&2
    return 1
  fi
  count=0
  for dir in "$1"/*; do
    if [[ -d "$dir" ]]; then
      ((count++))
    fi
  done
  echo "Total: $count"
}

setループを完全にスキップする方法。

CountDir() {
  if [[ ! -d "$1" ]]; then
    echo "ERROR: $1 is not a directory." >&2
    return 1
  fi
  set -- "$1"/*/
  echo "Total: $#"
}
于 2013-02-12T12:35:54.920 に答える