I'm trying to write simple script that compress specific directory periodically. Here is my code
#!/bin/bash
exec &> zipLogFile.log
log(){
CURRENT_TIME=$(date '+%Y-%m-%d %H:%M:%S')
if [ -n "$1" ]
then
LEVEL=${1}
MESSAGE=${2}
echo "$LEVEL $CURRENT_TIME $MESSAGE"
else
read IN
LEVEL="INFO"
echo "$LEVEL $CURRENT_TIME $IN"
fi
}
YESTERDAY=$(date -d "yesterday" '+%Y%m%d')
ROOT="/home/ubuntu/test"
YESTERDAY_DIR=$ROOT"/"$YESTERDAY
log "INFO" "checking directory $YESTERDAY_DIR"
if [ -d "$YESTERDAY_DIR" ]; then
zip -r $ROOT"/"$YESTERDAY".zip" $ROOT"/"$YESTERDAY | log
log "INFO" "Zip file $YESTERDAY_DIR.zip has been created"
else
log "ERROR" "$YESTERDAY_DIR not exist."
fi
My problem is I want to get zip command's output to log function and print it from function. When I try to use pipe, it prints only first line of output and break zip command. Here is what I get in my logFile when I run script.
INFO 2017-05-21 15:09:22 checking directory /home/ubuntu/test/20170520
INFO 2017-05-21 15:09:22 adding: home/ubuntu/test/20170520/ (stored 0%)
INFO 2017-05-21 15:09:22 Zip file /home/ubuntu/test/20170520.zip has been created
What am I doing wrong here? Thanks for the help.