0

こんにちは、このコードを使用して、指定されたパスから .java ファイルを見つけ、パスから出力ファイルへの .java ファイルのリストを生成し、各 Java ファイル内のタブとスペースの総数を数えました。関数内の 2 つの while ループとその関数を case ステートメントで呼び出す場合、これを行う方法は?

コード:

#!/bin/sh
    #
    output=/home/user/Desktop/file-list.txt
    path=/home/user/Desktop
    find $path -type f -name \*.java > $output
    echo "Generating files list..."
    echo "Done"


    while IFS= read file
    do 
        if [ -f "$file" ]; then
            spaces=$(tr -cd '\s' < "$file"  | wc -c);
            echo "$spaces spaces in file $file" >> "/home/user/Desktop/space-count.txt"
        fi
    done < "$output"
    echo "Space Count Done!"



    while IFS= read file
    do 
        if [ -f "$file" ]; then
            tabs=$(tr -cd '\t' < "$file"  | wc -c);
            echo "$tabs tabs in file $file" >> "/home/user/Desktop/tab-count.txt"
        fi
    done < "$output"
    echo "Tab Count Done!"
4

1 に答える 1

0

大文字と小文字の例を次に示しますが、文字の名前を関数に渡すことで、文字を大文字と小文字を区別する必要がなくなります。

function count_char
{
    flist=$1
    ch=$2

    case $ch in
       " ")  chName=space;;
       "\t") chName=tab;;
    esac

    while IFS= read file
    do 
        if [ -f "$file" ]; then
            tot=$(tr -cd $ch < "$file"  | wc -c);
            echo "$tot $chName in file $file" >> "/home/user/Desktop/tab-count.txt"
        fi
    done < "$flist"   
}

# setup code, find command here
# ....

count_char $output " "
count_char $output "\t"
于 2013-03-22T06:38:54.147 に答える