Skip to content

Commit

Permalink
Added proper exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
stablestud committed Jan 16, 2018
1 parent f4718bf commit ab6b430
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 30 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ To change the content of <strong>placed</strong> files go to:

## Todo for future releases

* Add proper exit codes as in [Reserved Exit Codes](http:https://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF)
* Add testing framework (travis etc.)
* Make script shell independent
* Create .deb
Expand Down
6 changes: 3 additions & 3 deletions adsorber.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ showHelp() {
echo " (with the help of the hosts file)"
echo ""
echo "Operations:"
echo " install - setup necessary things needed for adsorber"
echo " install - setup necessary things needed for Adsorber"
echo " e.g., create backup file of hosts file,"
echo " create scheduler which updates the host file once a week."
echo " update - update hosts file with newest ad servers"
echo " revert - revert hosts file to its original state"
echo " (it does not remove the schedule, so this should be used temporary)"
echo " remove - completely remove changes made by adsorber"
echo " remove - completely remove changes made by Adsorber"
echo " e.g., remove scheduler (if set)"
echo " revert hosts file (if not already done)"
echo " version - show version of this shell script"
Expand Down Expand Up @@ -108,7 +108,7 @@ showVersion() {
duplicateOption() {
echo "Adsorber: Duplicate option: '${option}'"

exit 1
exit 127
}


Expand Down
2 changes: 1 addition & 1 deletion bin/components/hosts_header
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# To make any changes to this file please edit #@${HOSTS_FILE_BACKUP_PATH}#@
# and run './adsorber.sh update' to apply your changes.
# Or else your changes will be overwritten by adsorber once a week.
# Or else your changes will be overwritten by Adsorber once a week.
#
# The domains will be updated once a week.
# For further information please run './adsorber --help'
Expand Down
10 changes: 5 additions & 5 deletions bin/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ copyConfig() {
sed "s|@.*|# Config file for Adsorber v${VERSION}|g" "${SCRIPT_DIR_PATH}/bin/default/default-adsorber.conf" > "${SCRIPT_DIR_PATH}/adsorber.conf"

errorCleanUp
exit 1
exit 126
fi

return 0
Expand Down Expand Up @@ -123,10 +123,10 @@ readConfig() {

isVariableSet() {
if [ -z "${HOSTS_FILE_PATH}" ] || [ -z "${HOSTS_FILE_BACKUP_PATH}" ] || [ -z "${CRONTAB_DIR_PATH}" ] || [ -z "${SYSTEMD_DIR_PATH}" ]; then
echo -e "! Missing setting(s) in adsorber.conf." 1>&2
echo -e "${PREFIX_FATAL}Missing setting(s) in adsorber.conf." 1>&2
echo "${PREFIX}Please delete adsorber.conf and run '${0} install' to create a new config file." 1>&2
errorCleanUp
exit 1
exit 127
fi

if [ -z "${PRIMARY_LIST}" ]; then
Expand All @@ -149,9 +149,9 @@ isVariableSet() {

isVariableValid() {
if [ ! -f "${HOSTS_FILE_PATH}" ]; then
echo -e "! Wrong HOSTS_FILE_PATH set in adsorber.conf. Can't access: ${HOSTS_FILE_PATH}" 1>&2
echo -e "${PREFIX_FATAL}Wrong HOSTS_FILE_PATH set in adsorber.conf. Can't access: ${HOSTS_FILE_PATH}" 1>&2
errorCleanUp
exit 1
exit 126
fi

return 0
Expand Down
2 changes: 1 addition & 1 deletion bin/cron/80adsorber
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#
# A(d)sorber's Cron file
# A(d)sorber's cron file
# Updates the hosts file once a week.
#
# For more information: https://github.com/stablestud/adsorber
Expand Down
4 changes: 2 additions & 2 deletions bin/default/default-adsorber.conf
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ HOSTS_FILE_BACKUP_PATH=/etc/hosts.original


## CRONTAB_DIR_PATH
# This defines the directory in which we'll set the Crontab.
# This defines the directory in which we'll set the crontab.
# Used to create a scheduler which supplies the hosts file regulary with
# new ad-domains.
#
Expand All @@ -89,7 +89,7 @@ CRONTAB_DIR_PATH=/etc/cron.weekly


## SYSTEMD_DIR_PATH
# This defines the directory in which we'll place the Systemd files.
# This defines the directory in which we'll place the systemd files.
# Used to create a scheduler which supplies the hosts file regulary with
# new ad-domains. Change if your system has an other path.
#
Expand Down
16 changes: 8 additions & 8 deletions bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ backupHostsFile() {


installCronjob() {
echo "${PREFIX}Installing Cronjob ..."
echo "${PREFIX}Installing cronjob ..."

if [ ! -d "${CRONTAB_DIR_PATH}" ]; then
echo -e "${PREFIX_FATAL}Wrong CRONTAB_DIR_PATH set. Can't access: ${CRONTAB_DIR_PATH}.${COLOUR_RESET}" 1>&2
errorCleanUp
exit 1
exit 126
fi

# Replace the @ place holder line with SCRIPT_DIR_PATH and copy the content to cron's directory
Expand All @@ -65,16 +65,16 @@ installSystemd() {
if [ ! -d "${SYSTEMD_DIR_PATH}" ]; then
echo -e "${PREFIX_FATAL}Wrong SYSTEMD_DIR_PATH set. Can't access: ${SYSTEMD_DIR_PATH}.${COLOUR_RESET}" 1>&2
errorCleanUp
exit 1
exit 126
fi

# Remove Systemd service if already installed (requires remove.sh)
# Remove systemd service if already installed (requires remove.sh)
if [ -f "${SYSTEMD_DIR_PATH}/adsorber.service" ] || [ -f "${SYSTEMD_DIR_PATH}/adsorber.timer" ]; then
echo "${PREFIX}Removing previous installed Systemd service ..."
echo "${PREFIX}Removing previous installed systemd service ..."
removeSystemd
fi

echo "${PREFIX}Installing Systemd service ..."
echo "${PREFIX}Installing systemd service ..."

# Replace the @ place holder line with SCRIPT_DIR_PATH and copy to its systemd directory
sed "s|^#@ExecStart.\+#@$|ExecStart=${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.service" > "${SYSTEMD_DIR_PATH}/adsorber.service"
Expand All @@ -85,7 +85,7 @@ installSystemd() {
# Enable the systemd service
systemctl daemon-reload \
&& systemctl enable adsorber.timer | printf "%s" "${PREFIX}" \
&& systemctl start adsorber.timer || echo -e "${PREFIX_WARNING}Couldn't start Systemd service." 1>&2
&& systemctl start adsorber.timer || echo -e "${PREFIX_WARNING}Couldn't start systemd service." 1>&2

INSTALLED_SCHEDULER="systemd"

Expand All @@ -105,7 +105,7 @@ promptInstall() {
* )
echo -e "${PREFIX_WARNING}Installation cancelled." 1>&2
errorCleanUp
exit 1
exit 130
;;
esac

Expand Down
6 changes: 3 additions & 3 deletions bin/remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ removeSystemd() {

rm "${SYSTEMD_DIR_PATH}/adsorber.timer" "${SYSTEMD_DIR_PATH}/adsorber.service" \
|| {
echo -e "${PREFIX_WARNING}Couldn't remove Systemd service files." 1>&2
echo -e "${PREFIX_WARNING}Couldn't remove systemd service files." 1>&2
return 1
}

Expand All @@ -72,7 +72,7 @@ removeSystemd() {
removeCronjob() {
if [ -f "${CRONTAB_DIR_PATH}/80adsorber" ]; then
rm "${CRONTAB_DIR_PATH}/80adsorber" \
&& echo "${PREFIX}Removed Adsorber's Cronjob."
&& echo "${PREFIX}Removed Adsorber's cronjob."
else
echo "${PREFIX}Cronjob not installed. Skipping ..." 1>&2
fi
Expand All @@ -93,7 +93,7 @@ promptRemove() {
* )
echo -e "${PREFIX_WARNING}Remove cancelled." 1>&2
errorCleanUp
exit 1
exit 130
;;
esac

Expand Down
2 changes: 1 addition & 1 deletion bin/systemd/adsorber.timer
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Unit]
Description=Adsorber timer that runs the Adsorber service weekly.
Description=Timer that runs the Adsorber service weekly.
Documentation=https://github.com/stablestud/Adsorber
Requires=network-online.target

Expand Down
10 changes: 5 additions & 5 deletions bin/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ checkBackupExist() {
* )
echo -e "${PREFIX_WARNING}Aborted." 1>&2
errorCleanUp
exit 1
exit 130
;;
esac
fi
Expand All @@ -75,7 +75,7 @@ readSourceList() {

if [ ! -s "${SCRIPT_DIR_PATH}/blacklist" ]; then
echo -e "${PREFIX_FATAL}Missing 'sources.list' and blacklist. To fix run '${0} install'.${COLOUR_RESET}" 1>&2
exit 1
exit 127
fi

echo "${PREFIX}No sources to fetch from, ignoring ..."
Expand Down Expand Up @@ -235,7 +235,7 @@ applyWhiteList() {
else
echo -e "${PREFIX_FATAL}Wrong USE_PARTIAL_MATCHING set, either set it to 'true' or 'false'.${COLOUR_RESET}" 1>&2
errorCleanUp
exit 1
exit 127
fi

done < "${TMP_DIR_PATH}/whitelist-sorted"
Expand Down Expand Up @@ -332,7 +332,7 @@ applyHostsFile() {
|| {
echo -e "${PREFIX_FATAL}Couldn't apply hosts file. Aborting.${COLOUR_RESET}" 1>&2
errorCleanUp
exit 1
exit 126
}

echo -e "${PREFIX_INFO}Successfully applied new hosts file with ${COUNT_BLOCKED} blocked domains.${COLOUR_RESET}"
Expand Down Expand Up @@ -372,7 +372,7 @@ update() {
* )
echo -e "${PREFIX_FATAL}Wrong PRIMARY_LIST set in adsorber.conf. Choose either 'whitelist' or 'blacklist'${COLOUR_RESET}" 1>&2
errorCleanUp
exit 1
exit 127
;;
esac

Expand Down

0 comments on commit ab6b430

Please sign in to comment.