0

嗨,我已经使用此代码从给定路径中查找 .java 文件,并从输出文件的路径生成 .java 文件列表,并计算每个 java 文件中制表符和空格的总数。现在我需要编写函数中的两个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 回答