-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.sh
More file actions
executable file
·62 lines (55 loc) · 1.42 KB
/
log.sh
File metadata and controls
executable file
·62 lines (55 loc) · 1.42 KB
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
#!/bin/bash
help() {
echo "Usage: $(basename "$0") [-v] [-n SECONDS] [-o FILE] [-t] [-h] <command>"
echo "Run a command every N seconds."
echo
echo "Options:"
echo " -v Print output to terminal"
echo " -n SECONDS Interval in seconds (default: 1)"
echo " -o FILE Log output to FILE (only if -o used)"
echo " -t Prepend each line with Unix timestamp"
echo " -h, --help Show this help message and exit"
exit 0
}
# Default values
verbose=false
output_file=""
interval=1
timestamp_flag=false
# Parse flags
while getopts "vn:o:th" opt; do
case $opt in
v) verbose=true ;;
n) interval="$OPTARG" ;;
o) output_file="$OPTARG" ;;
t) timestamp_flag=true ;;
h) help ;;
*) help ;;
esac
done
shift $((OPTIND-1))
# Command to run
command="$1"
if [[ -z "$command" ]]; then
echo "Error: No command provided." >&2
help
fi
# Main loop
while true; do
output=$(eval "$command")
timestamp=""
if [[ "$timestamp_flag" == true ]]; then
timestamp="[$(date +%s)] "
fi
# Output logic
if [[ "$verbose" == true && -n "$output_file" ]]; then
echo "${timestamp}${output}" | tee -a "$output_file"
elif [[ "$verbose" == true ]]; then
echo "${timestamp}${output}"
elif [[ -n "$output_file" ]]; then
echo "${timestamp}${output}" >> "$output_file"
else
echo "${timestamp}${output}" >> "$(echo "$command" | awk '{print $1}').log"
fi
sleep "$interval"
done