| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
SOURCE="${BASH_SOURCE[0]}" |
| 4 |
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink |
| 5 |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" |
| 6 |
SOURCE="$(readlink "$SOURCE")" |
| 7 |
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located |
| 8 |
done |
| 9 |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" |
| 10 |
OUTFILE="log" |
| 11 |
OPTS=`getopt -o hw: --long help,outfile -n 'parse-options' -- "$@"` |
| 12 |
HELP=false |
| 13 |
|
| 14 |
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi |
| 15 |
|
| 16 |
eval set -- "$OPTS" |
| 17 |
|
| 18 |
while true; do |
| 19 |
case "$1" in |
| 20 |
-h | --help ) HELP=true; shift ;; |
| 21 |
-w | --outfile ) OUTFILE="$2" ; shift ; shift ;; |
| 22 |
* ) break ;; |
| 23 |
esac |
| 24 |
done |
| 25 |
|
| 26 |
shift |
| 27 |
|
| 28 |
if $HELP ; then |
| 29 |
echo "Usage: logwrap [-hw] command" |
| 30 |
exit |
| 31 |
fi |
| 32 |
|
| 33 |
if [[ $# -eq 0 ]] ; then |
| 34 |
echo failed to provide script to wrap |
| 35 |
exit 1 |
| 36 |
fi |
| 37 |
|
| 38 |
$@ 2>&1 | tee $OUTFILE |
| 39 |
exit ${PIPESTATUS[0]} |