Skip to content

Commit 3e8f50e

Browse files
committed
syslog
1 parent 55a7e79 commit 3e8f50e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

backup_file.sh

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
# Function below creates a backup of a file. Returns non-zero status on error.
1+
#!/bin/bash
2+
3+
log() {
4+
# This function sends a message to syslog and to standard output if VERBOSE is true.
5+
6+
local MESSAGE="${@}"
7+
if [[ "${VERBOSE}" = 'true' ]]
8+
then
9+
echo "${MESSAGE}"
10+
fi
11+
logger -t backup_file.sh "${MESSAGE}"
12+
}
213

14+
# Function below creates a backup of a file. Returns non-zero status on error.
315
backup_file() {
416
local FILE="${1}"
5-
if [[ -f "{FILE}"]]
17+
if [[ -f "${FILE}" ]]
618
then
7-
local BACKUP_FILE="/var/tmp/$(basename ${FILE}).&(date +%F-%N)"
19+
local BACKUP_FILE="/var/tmp/$(basename ${FILE}).$(date +%F-%N)"
820
log "Backing up ${FILE} to ${BACKUP_FILE}"
921

1022
cp -p ${FILE} ${BACKUP_FILE}
@@ -14,4 +26,14 @@ backup_file() {
1426
fi
1527
}
1628

29+
readonly VERBOSE='true'
1730
backup_file '/etc/passwd'
31+
32+
# A decision based exit status of the function.
33+
if [[ "${?}" -eq '0' ]]
34+
then
35+
log 'File backup succeeded!'
36+
else
37+
log 'File backup failed!'
38+
exit 1
39+
fi

0 commit comments

Comments
 (0)