Tôi đã vật lộn trong một thời gian dài để viết một kịch bản có 2 đối số, 1 yêu cầu chọn một năm và 2 yêu cầu chọn nếu tôi muốn tối thiểu, tối đa, trung bình hoặc tất cả được hiển thị là dòng cuối cùng từ các tệp liên quan đến năm đã chọn.
Về cơ bản, tôi có một thư mục chứa các thư mục con của các năm khác nhau (2000, 2001, 2002, v.v.) trong các thư mục đó là các thư mục con trong nhiều tháng và ngày có chứa (a) tệp thông báo về dân số (mặc dù không phải thông tin thực) thành phố là dòng cuối cùng. Đó là một phần của cây thư mục:
.
|-- 2000
| |-- 01
| | `-- 18
| | `-- ff_1177818640
| |-- 02
| | |-- 02
| | | `-- ff_1669027271
| | |-- 03
| | | `-- ff_234075290
| | |-- 10
| | | `-- ff_1584524530
| | |-- 14
| | | `-- ff_113807345
| | `-- 17
| | `-- ff_1452228827
| |-- 03
| | |-- 06
| | | `-- ff_58914249
| | `-- 11
| | `-- ff_2828212321
| |-- 04
| | `-- 17
| | `-- ff_302131884
| |-- 06
| | `-- 13
| | `-- ff_2175615745
| |-- 07
| | |-- 07
| | | `-- ff_918426998
| | `-- 24
| | `-- ff_2808316425
| |-- 08
| | `-- 27
| | `-- ff_1449825497
| |-- 09
| | `-- 19
| | `-- ff_110255856
| `-- 12
| `-- 08
| `-- ff_1621190
|-- 2001
| |-- 03
| | `-- 21
| | `-- ff_517010375
| |-- 05
| | `-- 27
| | `-- ff_1458621098
| |-- 06
| | |-- 07
| | | `-- ff_155853916
| | |-- 25
| | | |-- ff_2382312387
| | | `-- ff_270731174
| | `-- 29
| | `-- ff_3228522859
| |-- 07
| | `-- 28
| | `-- ff_3215021752
| |-- 09
| | `-- 24
| | `-- ff_1080314364
| `-- 11
| `-- 24
| `-- ff_2313722442
Tất cả các tệp được định dạng theo cùng một cách:
2019-04-03
Wednesday
Newcastle-upon-Tyne
255362
Tôi cần phải viết một kịch bản để được hỏi năm nào tôi cần (chọn thư mục đó) và sau đó hỏi tôi có muốn hiển thị trung bình, tối thiểu, tối đa hoặc tất cả các mục trên để hiển thị cho dân số không (dòng cuối cùng của các tệp).
Đây là những gì tôi có cho đến nay:
#!/bin/bash
function min () {
echo $(sort -n populations | head -1)
}
function max () {
echo $(sort -n populations | tail -1)
}
function avg () {
count=0
sum=0
while read line ; do
num='echo ${line#* }'
sum='expr $sum + $num'
count='expr $count + 1'
done < populations
avg='expr $sum / $count'
echo $avg
}
echo "Please enter the year: "
read s1
echo "
Enter an option:
1. Minimum
2. Maximum
3. Average
4. All"
read s2
#echo $s2
for file in $(find ~/filesToSort/$s1 -type f) ; do
tail -1 $file >> populations
done
echo $(cat populations)
#min
#max
#avg
rm populations
Điều này cho phép tôi chọn các thư mục, nhưng không cung cấp cho tôi câu trả lời mà tôi cần, chỉ nhổ những dòng cuối cùng của tệp.