-
Notifications
You must be signed in to change notification settings - Fork 0
/
filechk.sh
executable file
·163 lines (129 loc) · 4.3 KB
/
filechk.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
<<OVERVIEW
## レポジトリチェックスクリプト
* リポジトリディレクトリ内部のファイル構造を簡易的に集計し画面に出力するシェルスクリプトです。
* 以前Qiitaに記載した記事をスクリプト化しました。
* Qiita記事URL: https://qiita.com/UmedaTakefumi/items/4947fa2494a1f726bc2b
OVERVIEW
## コマンドの使い方を画面に出力します
function print_how_to_use () {
echo " -b --binary-files"
echo -e " バイナリファイルを存在するか簡易的に集計確認し、画面に出力します\n"
echo " -c --check-files"
echo -e " 簡易的にファイルをチェックしたのちにファイルの種類単位で集計し、画面に出力します\n"
echo " -h --help"
echo -e " コマンドの使い方を画面に出力します。\n"
}
## コマンド実行時に出力されるヘッダー出力
function print_header () {
check_dirname=$(pwd | awk -F/ '{ print $NF }')
date=$(LANG=C date)
echo -e "# -- $date --"
echo -e "# check repository DIR:$check_dirname\n"
}
## 調査対象のためのディレクトリ移動
function cd_dir_view () {
cd $DIR_PATH
echo "--------------------------------------------------------------------------"
echo "export: $DIR_PATH"
echo "--------------------------------------------------------------------------"
pathname=$(pwd)
echo "pwd: $pathname"
echo "--------------------------------------------------------------------------"
ls -lasF $DIR_PATH
echo "--------------------------------------------------------------------------"
}
## 簡易的にファイルをチェックしたのちにファイルの種類単位で集計し、画面に出力します
function check_files () {
cd_dir_view
find . -type d -name .git -prune -o -type f -exec file {} \; | \
awk -F: '{ print $2 }' | env LANG=c sort | uniq -c | env LANG=c sort -nr
}
## バイナリファイル(executable, Mach-O, ELF)が存在するか簡易的に集計し画面に出力します
function check_binary_files () {
cd_dir_view
bin_exe=($(find . -type d -name .git -prune -o -type f -exec file {} \; \
| grep executable | grep -v text | grep -v ELF | grep -v Mach-O \
| awk -F: '{print $1}'))
bin_macho=($(find . -type d -name .git -prune -o -type f -exec file {} \; \
| grep Mach-O | grep -v text | awk -F: '{print $1}'))
bin_elf=($(find . -type d -name .git -prune -o -type f -exec file {} \; \
| grep ELF | grep -v text | awk -F: '{print $1}'))
if [ ${#bin_exe[@]} == 0 ]; then
echo -e "## Exe\n"
echo -e "[good] count: ${#bin_exe[@]}\n"
else
echo -e "## Exe\n"
echo -e "[need to confirm] count: ${#bin_exe[@]}\n"
for filepath in ${bin_exe[@]}; do
echo -e " $filepath"
done
echo -e "\n"
fi
if [ ${#bin_macho[@]} == 0 ]; then
echo -e "## Mach-O\n"
echo -e "[good] count: ${#bin_macho[@]}\n"
else
echo -e "## Mach-O\n"
echo -e "[need to confirm] count: ${#bin_macho[@]}\n"
for filepath in ${bin_macho[@]}; do
echo -e " $filepath"
done
echo -e "\n"
fi
if [ ${#bin_elf[@]} == 0 ]; then
echo -e "## ELF\n"
echo -e "[good] count: ${#bin_elf[@]}\n"
else
echo -e "## ELF\n"
echo -e "[need to confirm] count: ${#bin_elf[@]}\n"
for filepath in ${bin_elf[@]}; do
echo -e " $filepath"
done
echo -e "\n"
fi
}
## copy and paste
##
## ref: https://qiita.com/b4b4r07/items/dcd6be0bb9c9185475bb
for OPT in "$@"
do
case $OPT in
-h | --help)
print_header
print_how_to_use
exit 1
;;
-b | --binary-files)
print_header
check_binary_files
shift 1
;;
-c | --check-files)
print_header
check_files
shift 1
;;
-- | -)
shift 1
param+=( "$@" )
break
;;
-*)
echo "$PROGNAME: illegal option -- '$(echo $1 | sed 's/^-*//')'" 1>&2
exit 1
;;
*)
if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
#param=( ${param[@]} "$1" )
param+=( "$1" )
shift 1
fi
;;
esac
done
if [ -z "$param" ]; then
echo "$PROGNAME: too few arguments" 1>&2
echo "Try '$PROGNAME --help' for more information." 1>&2
exit 1
fi