2017-02-23 17:13:11 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
__DIR__=$(dirname $(realpath $0));
|
|
|
|
|
|
|
|
source $__DIR__/../../lib/include/bash/const;
|
|
|
|
source $__DIR__/../../lib/include/bash/func;
|
|
|
|
|
|
|
|
|
|
|
|
# [1] Check arguments
|
|
|
|
#========================================================#
|
|
|
|
|
|
|
|
# (1) If argument missing #
|
|
|
|
if [ $# -lt 2 ]; then
|
2017-05-09 10:18:42 +00:00
|
|
|
slog "Missing argument(s)" "file:truncate" "update";
|
2017-05-09 11:42:28 +00:00
|
|
|
echo 127;
|
|
|
|
exit 127;
|
2017-02-23 17:13:11 +00:00
|
|
|
fi;
|
|
|
|
|
|
|
|
# (2) Check argument #1 #
|
|
|
|
if [ ! -e "$DATA_DIR/$1" ]; then
|
2017-05-09 10:18:42 +00:00
|
|
|
slog "Data file '$1' does not exist in '$DATA_DIR'" "file:truncate" "update";
|
2017-05-09 11:42:28 +00:00
|
|
|
echo 127;
|
|
|
|
exit 127;
|
2017-02-23 17:13:11 +00:00
|
|
|
fi;
|
|
|
|
|
|
|
|
# (3) Check number of lines to truncate #
|
|
|
|
if [ $2 -gt $(wc -l "$DATA_DIR/$1" | awk '{print $1}') ]; then
|
2017-05-09 10:21:03 +00:00
|
|
|
slog "Failure: asked to remove $2 lines out of `wc -l "$DATA_DIR/$1" | awk '{print $1}'` lines" "file:truncate" "update";
|
2017-05-09 11:42:28 +00:00
|
|
|
echo 127;
|
|
|
|
exit 127;
|
2017-02-23 17:13:11 +00:00
|
|
|
fi;
|
|
|
|
|
|
|
|
|
|
|
|
# [2] Truncate file
|
|
|
|
#========================================================#
|
|
|
|
# (1) Increment by 1 (because exlusive) #
|
|
|
|
incr=$(expr $2 + 1);
|
|
|
|
|
|
|
|
# (2) Truncate file #
|
|
|
|
tail -n +$incr "$DATA_DIR/$1" | tee "$DATA_DIR/$1" > /dev/null;
|
|
|
|
|
|
|
|
|
2017-05-09 10:18:42 +00:00
|
|
|
slog "Succesfully truncated $2 lines" "file:truncate" "update";
|
2017-02-23 17:13:11 +00:00
|
|
|
echo 0;
|
|
|
|
exit 0;
|