-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcount lines in list of files.sh
53 lines (45 loc) · 1.03 KB
/
count lines in list of files.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
# Counting the number of lines in a list of files
# function version
# function storing list of all files in variable files
get_files () {
files="`ls *.[ch]`"
}
# function counting the number of lines in a file
count_lines () {
f=$1 # 1st argument is filename
l=`wc -l $f | sed 's/^\([0-9]*\).*$/\1/'` # number of lines
}
# the script should be called without arguments
if [ $# -ge 1 ]
then
echo "Usage: $0 "
exit 1
fi
# split by newline
IFS=$'\012'
echo "$0 counts the lines of code"
# don't forget to initialise!
l=0
n=0
s=0
# call a function to get a list of files
get_files
# iterate over this list
for f in $files
do
# call a function to count the lines
count_lines $f
echo "$f: $l"loc
# store filename in an array
file[$n]=$f
# store number of lines in an array
lines[$n]=$l
# increase counter
n=$[ $n + 1 ]
# increase sum of all lines
s=$[ $s + $l ]
done
echo "$n files in total, with $s lines in total"
i=5
echo "The $i-th file was ${file[$i]} with ${lines[$i]} lines"