Last active
August 29, 2015 14:01
-
-
Save rabinshr/1659eb0d05c164c52afd to your computer and use it in GitHub Desktop.
A log viewer that allows viewing of log files in log folder. Particularly useful in situations where a user doesn't have root priviledges but requires to view logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
if [ "$(id -u)" != "0" ]; then | |
echo "This script must be run as root" 1>&2 | |
exit 1 | |
fi | |
usage=" | |
Usage: $(basename "$0") -d LOG_DIRECTORY [-r LOG_FILE] | |
List and read log files in LOG_DIRECTORY | |
" | |
if [[ "$1" == "" ]]; then | |
echo "${usage}" | |
exit 1; | |
fi | |
dir= | |
file= | |
while getopts ":d:r:" opt; do | |
#escape arguments | |
arg=$(printf %q $OPTARG); | |
case $opt in | |
d) | |
dir=$arg; | |
;; | |
r) | |
file=$arg; | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1; | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1; | |
;; | |
esac | |
done | |
if [[ ! -d "${dir}" ]]; then | |
echo "${dir} is not a directory"; | |
exit 1; | |
fi | |
if [[ ${dir} == */log* ]]; then | |
CMD="ls ${dir}"; | |
if [[ "${file}" != "" ]]; then | |
if [[ -f "${dir}/${file}" ]]; then | |
CMD="cat ${dir}/${file}"; | |
else | |
echo "Log file not found"; | |
exit 1; | |
fi | |
fi | |
${CMD} | |
else | |
echo "Not a valid log directory" | |
exit 1; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment