mirror of
https://github.com/serghey-rodin/vesta.git
synced 2024-12-03 19:00:24 -08:00
111 lines
2.7 KiB
Bash
Executable File
111 lines
2.7 KiB
Bash
Executable File
#!/bin/bash
|
|
# info: list user log
|
|
# options: USER [FORMAT]
|
|
#
|
|
# The function of obtaining the list of 100 last users commands.
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Variable&Function #
|
|
#----------------------------------------------------------#
|
|
|
|
# Argument definition
|
|
user=$1
|
|
format=${2-shell}
|
|
|
|
# Includes
|
|
source $VESTA/func/main.sh
|
|
|
|
# JSON list function
|
|
json_list() {
|
|
IFS=$'\n'
|
|
i=1
|
|
objects=$(echo "$logs" |wc -l)
|
|
echo "{"
|
|
for str in $logs; do
|
|
ID=$(echo "$str" |cut -f 2 -d \')
|
|
DATE=$(echo "$str" |cut -f 4 -d \')
|
|
TIME=$(echo "$str" |cut -f 6 -d \')
|
|
CMD=$(echo "$str" |cut -f 8 -d \')
|
|
CMD=${CMD//\"/\\\"}
|
|
echo -n ' "'$ID'": {
|
|
"CMD": "'$CMD'",
|
|
"UNDO": "'$UNDO'",
|
|
"TIME": "'$TIME'",
|
|
"DATE": "'$DATE'"
|
|
}'
|
|
if [ "$i" -lt "$objects" ]; then
|
|
echo ','
|
|
else
|
|
echo
|
|
fi
|
|
((i++))
|
|
done
|
|
echo '}'
|
|
}
|
|
|
|
shell_list() {
|
|
IFS=$'\n'
|
|
echo "DATE~TIME~CMD"
|
|
echo "----~----~---"
|
|
for str in $logs; do
|
|
DATE=$(echo "$str" |cut -f 4 -d \')
|
|
TIME=$(echo "$str" |cut -f 6 -d \')
|
|
CMD=$(echo "$str" |cut -f 8 -d \')
|
|
echo "$DATE~$TIME~$CMD"
|
|
done
|
|
}
|
|
|
|
# PLAIN list function
|
|
plain_list() {
|
|
IFS=$'\n'
|
|
for str in $logs; do
|
|
DATE=$(echo "$str" |cut -f 4 -d \')
|
|
TIME=$(echo "$str" |cut -f 6 -d \')
|
|
CMD=$(echo "$str" |cut -f 8 -d \')
|
|
echo -e "$ID\t$CMD\t$UNDO\t$TIME\t$DATE"
|
|
done
|
|
}
|
|
|
|
# CSV list function
|
|
csv_list() {
|
|
IFS=$'\n'
|
|
echo "ID,CMD,UNDO,TIME,DATE"
|
|
for str in $logs; do
|
|
DATE=$(echo "$str" |cut -f 4 -d \')
|
|
TIME=$(echo "$str" |cut -f 6 -d \')
|
|
CMD=$(echo "$str" |cut -f 8 -d \')
|
|
echo "$ID,\"$CMD\",\"$UNDO\",$TIME,$DATE"
|
|
done
|
|
}
|
|
|
|
#----------------------------------------------------------#
|
|
# Verifications #
|
|
#----------------------------------------------------------#
|
|
|
|
check_args '1' "$#" 'USER [FORMAT]'
|
|
is_format_valid 'user'
|
|
is_object_valid 'user' 'USER' "$user"
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Action #
|
|
#----------------------------------------------------------#
|
|
|
|
# Parsing history log
|
|
logs=$(tail -n 300 $USER_DATA/history.log 2>/dev/null)
|
|
|
|
case $format in
|
|
json) json_list ;;
|
|
plain) plain_list ;;
|
|
csv) csv_list ;;
|
|
shell) shell_list |column -t -s '~';;
|
|
esac
|
|
|
|
|
|
#----------------------------------------------------------#
|
|
# Vesta #
|
|
#----------------------------------------------------------#
|
|
|
|
exit
|