Skip to content

Commit e6bc301

Browse files
committed
check append file of -a option oldratlee#70
1 parent 83a2a13 commit e6bc301

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

show-busy-java-threads

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ colorPrint() {
3737
shift
3838

3939
colorEcho "$color" "$@"
40-
[ -n "$append_file" ] && echo "$@" >> "$append_file"
40+
# use "{} &> /dev/null" to discard bash error message:
41+
# line 42: xxx.log: Permission denied
42+
{ [ -n "$append_file" ] && echo "$@" >> "$append_file"; } &> /dev/null
4143
}
4244

4345
redPrint() {
@@ -189,6 +191,23 @@ count=${count:-5}
189191
use_ps=${use_ps:-false}
190192
top_delay=${top_delay:-0.5}
191193

194+
# check the directory of append-file(-a) mode, create if not exsit.
195+
if [ -n "$append_file" ]; then
196+
if [ -e "$append_file" ]; then
197+
[ ! -f "$append_file" ] && fatal "Error: $append_file(specified by option -a, for storing run output files) exists but is not a file!"
198+
[ ! -w "$append_file" ] && fatal "Error: file $append_file(specified by option -a, for storing run output files) exists but is not writable!"
199+
else
200+
append_file_dir="$(dirname "$append_file")"
201+
if [ -e "$append_file_dir" ]; then
202+
[ ! -d "$append_file_dir" ] && fatal "Error: directory $append_file_dir(specified by option -a, for storing run output files) exists but is not a directory!"
203+
[ ! -w "$append_file_dir" ] && fatal "Error: directory $append_file_dir(specified by option -a, for storing run output files) exists but is not writable!"
204+
else
205+
mkdir -p "$append_file_dir" && fatal "Error: fail to create directory $append_file_dir(specified by option -a, for storing run output files)!"
206+
fi
207+
fi
208+
fi
209+
210+
# check jstack-file directory(-S) mode, create directory if not exsit.
192211
if [ -n "$jstack_file_dir" ]; then
193212
if [ -e "$jstack_file_dir" ]; then
194213
[ ! -d "$jstack_file_dir" ] && fatal "Error: $jstack_file_dir(specified by option -S, for storing jstack output files) exists but is not a directory!"

0 commit comments

Comments
 (0)