Skip to content

Commit

Permalink
Fixed issues pointed out by shellcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
stablestud committed Jan 16, 2018
1 parent 8922d1c commit f4718bf
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion adsorber.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Config file for Adsorber v0.2.2
## Config file for Adsorber v0.2.2
#
# To reset the config file just delete it and run 'adsorber.sh install', a new
# config file will be created.
Expand Down
Empty file modified bin/colours.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion bin/components/hosts_header
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file has been edited by Adsorber.
#
# To make any changes to this file please edit @${HOSTS_FILE_BACKUP_PATH}@
# 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.
#
Expand Down
Empty file modified bin/components/hosts_title
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion bin/cron/80adsorber
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# adsorber.sh install --cron:
# This file is going to be copied into /etc/cron.weekly/ if not other specified.

@/some/path/adsorber.sh update # line will be overwritten by script (DO NOT EDIT)@
#@/some/path/adsorber.sh update # line will be overwritten by script (DO NOT EDIT)#@
2 changes: 1 addition & 1 deletion bin/default/default-adsorber.conf
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
@# Config file for Adsorber v?.?.? (DO NOT EDIT, WILL BE OVERWRITTEN BY SCRIPT)@
#@ Config file for Adsorber v?.?.? (DO NOT EDIT, WILL BE OVERWRITTEN BY SCRIPT)@
#
# To reset the config file just delete it and run 'adsorber.sh install', a new
# config file will be created.
Expand Down
Empty file modified bin/default/default-blacklist
100644 → 100755
Empty file.
Empty file modified bin/default/default-sources.list
100644 → 100755
Empty file.
Empty file modified bin/default/default-whitelist
100644 → 100755
Empty file.
11 changes: 5 additions & 6 deletions bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ installCronjob() {
fi

# Replace the @ place holder line with SCRIPT_DIR_PATH and copy the content to cron's directory
sed "s|@.*|${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/cron/80adsorber" > "${CRONTAB_DIR_PATH}/80adsorber"
sed "s|^#@.\+#@$|${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/cron/80adsorber" > "${CRONTAB_DIR_PATH}/80adsorber"
chmod u=rwx,g=rx,o=rx "${CRONTAB_DIR_PATH}/80adsorber"

INSTALLED_SCHEDULER="cronjob"
Expand All @@ -77,15 +77,14 @@ installSystemd() {
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"
sed "s|^#@ExecStart.\+#@$|ExecStart=${SCRIPT_DIR_PATH}\/adsorber\.sh update|g" "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.service" > "${SYSTEMD_DIR_PATH}/adsorber.service"
cp "${SCRIPT_DIR_PATH}/bin/systemd/adsorber.timer" "${SYSTEMD_DIR_PATH}/adsorber.timer"

chmod u=rwx,g=rx,o=rx "${SYSTEMD_DIR_PATH}/adsorber.service" "${SYSTEMD_DIR_PATH}/adsorber.timer"

printf "${PREFIX}"
# Enable the systemd service
systemctl daemon-reload \
&& systemctl enable adsorber.timer \
&& systemctl enable adsorber.timer | printf "%s" "${PREFIX}" \
&& systemctl start adsorber.timer || echo -e "${PREFIX_WARNING}Couldn't start Systemd service." 1>&2

INSTALLED_SCHEDULER="systemd"
Expand All @@ -96,7 +95,7 @@ installSystemd() {

promptInstall() {
if [ -z "${REPLY_TO_PROMPT}" ]; then
read -p "${PREFIX_INPUT}Do you really want to install Adsorber? [Y/n]: " REPLY_TO_PROMPT
read -r -p "${PREFIX_INPUT}Do you really want to install Adsorber? [Y/n]: " REPLY_TO_PROMPT
fi

case "${REPLY_TO_PROMPT}" in
Expand All @@ -116,7 +115,7 @@ promptInstall() {

promptScheduler() {
if [ -z "${REPLY_TO_SCHEDULER_PROMPT}" ]; then
read -p "${PREFIX_INPUT}What scheduler should be used to update hosts file automatically? [(S)ystemd/(C)ron/(N)one]: " REPLY_TO_SCHEDULER_PROMPT
read -r -p "${PREFIX_INPUT}What scheduler should be used to update hosts file automatically? [(S)ystemd/(C)ron/(N)one]: " REPLY_TO_SCHEDULER_PROMPT
fi

case "${REPLY_TO_SCHEDULER_PROMPT}" in
Expand Down
4 changes: 2 additions & 2 deletions bin/remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ removeSystemd() {
if [ -f "${SYSTEMD_DIR_PATH}/adsorber.service" ] || [ -f "${SYSTEMD_DIR_PATH}/adsorber.timer" ]; then

systemctl stop adsorber.timer 2>/dev/null
systemctl disable adsorber.timer | ( printf "${PREFIX}" && cat )
systemctl disable adsorber.timer | ( printf "%s" "${PREFIX}" && cat )
systemctl stop adsorber.service 2>/dev/null 1>&2
systemctl disable adsorber.service 2>/dev/null 1>&2 # The service is not enabled by default

Expand Down Expand Up @@ -83,7 +83,7 @@ removeCronjob() {

promptRemove() {
if [ -z "${REPLY_TO_PROMPT}" ]; then
read -p "${PREFIX_INPUT}Do you really want to remove Adsorber? [Y/n] " REPLY_TO_PROMPT
read -r -p "${PREFIX_INPUT}Do you really want to remove Adsorber? [Y/n] " REPLY_TO_PROMPT
fi

case "${REPLY_TO_PROMPT}" in
Expand Down
2 changes: 1 addition & 1 deletion bin/systemd/adsorber.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ After=network.target
[Service]
Type=oneshot
User=root
@ExecStart=/some/path/adsorber.sh update - line will be overwritten by script (DO NOT EDIT)@
#@ExecStart=/some/path/adsorber.sh update - line will be overwritten by script (DO NOT EDIT)#@
54 changes: 27 additions & 27 deletions bin/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ checkBackupExist() {

if [ -z "${REPLY_TO_FORCE_PROMPT}" ]; then
echo -e "${PREFIX_FATAL}Backup of ${HOSTS_FILE_PATH} does not exist. To backup run '${0} install'.${COLOUR_RESET}" 1>&2
read -p "${PREFIX_INPUT}Ignore issue and continue? (May break your system, not recommended) [YES/n]: " REPLY_TO_FORCE_PROMPT
read -r -p "${PREFIX_INPUT}Ignore issue and continue? (May break your system, not recommended) [YES/n]: " REPLY_TO_FORCE_PROMPT
fi

case "${REPLY_TO_FORCE_PROMPT}" in
Expand Down Expand Up @@ -83,8 +83,7 @@ readSourceList() {
else
# Only read sources with http(s) at the beginning
# Remove inline # comments
cat "${SOURCELIST_FILE_PATH}" \
| sed -n '/^\s*http.*/p' \
sed -n '/^\s*http.*/p' "${SOURCELIST_FILE_PATH}" \
| sed 's/\s\+#.*//g' \
> "${TMP_DIR_PATH}/sourceslist-filtered"

Expand Down Expand Up @@ -140,16 +139,16 @@ fetchSources() {
echo -e "${PREFIX_INFO}Getting${COLOUR_RESET}: ${domain}"

# Is wget installed? If yes download the hosts files.
if [ $(type -fP wget) ]; then
printf "${PREFIX}"
if [ "$(type -fP wget)" ]; then
printf "%s" "${PREFIX}"

if wget "${domain}" --show-progress -L --timeout=30 -t 1 -nv -O - >> "${TMP_DIR_PATH}/fetched"; then
(( successful_count++ ))
else
echo -e "${PREFIX_WARNING}wget couldn't fetch: ${domain}" 1>&2
fi
# Is curl installed? If yes download the hosts files.
elif [ $(type -fP curl) ]; then
elif [ "$(type -fP curl)" ]; then
if curl "${domain}" -sS -L --connect-timeout 30 --fail --retry 1 >> "${TMP_DIR_PATH}/fetched"; then
(( successful_count++ ))
else
Expand All @@ -166,7 +165,7 @@ fetchSources() {
if [ "${successful_count}" -eq 0 ]; then
echo -e "${PREFIX_WARNING}Nothing to apply [${successful_count}/${total_count}]." 1>&2
return 1
elif [ "${IGNORE_DOWNLOAD_ERROR}" == "false" ] && [ "${successful_count} != ${total_count}" ]; then
elif [ "${IGNORE_DOWNLOAD_ERROR}" == "false" ] && [ "${successful_count}" == "${total_count}" ]; then
echo -e "${PREFIX_WARNING}Couldn't fetch all hosts sources [${successful_count}/${total_count}]. Aborting ..."
cleanUp
exit 1
Expand All @@ -189,8 +188,7 @@ filterDomains() {
# - replace tabs and multiple spaces with one space
# - remove domains without a dot (e.g localhost , loopback , ip6-allnodes , etc...)
# - remove domains that are redirecting to *.local
cat "${TMP_DIR_PATH}/${input_file}" \
| sed 's/\r/\n/g' \
sed 's/\r/\n/g' "${TMP_DIR_PATH}/${input_file}" \
| sed 's/^\s*127\.0\.[01]\.1/0\.0\.0\.0/g' \
| sed -n '/^\s*0\.0\.0\.0\s\+.\+/p' \
| sed 's/\s\+#.*//g' \
Expand Down Expand Up @@ -250,8 +248,6 @@ applyWhiteList() {


mergeBlackList() {
local input_file="${1}"

if [ ! -s "${TMP_DIR_PATH}/blacklist-sorted" ]; then
echo "${PREFIX}Blacklist is empty, ignoring ..."
return 1
Expand Down Expand Up @@ -284,41 +280,45 @@ isCacheEmpty() {


preBuildHosts() {
# Replace @...@ with the path to the backup hosts
sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header" > "${TMP_DIR_PATH}/hosts"
{
# Replace @...@ with the path to the backup hosts
sed "s|#@.\+#@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header"

echo -e "" >> "${TMP_DIR_PATH}/hosts"
echo ""

# Add hosts.original
cat "${HOSTS_FILE_BACKUP_PATH}" >> "${TMP_DIR_PATH}/hosts" \
|| echo "${PREFIX}You may want to add your hostname to ${HOSTS_FILE_PATH}" 1>&2
# Add hosts.original
cat "${HOSTS_FILE_BACKUP_PATH}" \
|| echo "${PREFIX}You may want to add your hostname to ${HOSTS_FILE_PATH}" 1>&2

echo -e "" >> "${TMP_DIR_PATH}/hosts"
echo ""

# Add hosts_title
cat "${SCRIPT_DIR_PATH}/bin/components/hosts_title" >> "${TMP_DIR_PATH}/hosts"
# Add hosts_title
cat "${SCRIPT_DIR_PATH}/bin/components/hosts_title"
} > "${TMP_DIR_PATH}/hosts"

return 0
}


buildHostsFile() {
echo -e "" >> "${TMP_DIR_PATH}/hosts"
{
echo ""

# Add the fetched ad-domains to the hosts file
cat "${TMP_DIR_PATH}/cache" >> "${TMP_DIR_PATH}/hosts"
# Add the fetched ad-domains to the hosts file
cat "${TMP_DIR_PATH}/cache"

echo -e "" >> "${TMP_DIR_PATH}/hosts"
echo ""

# Add the hosts_header to the hosts file in the temporary folder, filter out the line with @ and replace with HOSTS_FILE_BACKUP_PATH
sed "s|@.\+@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header" >> "${TMP_DIR_PATH}/hosts"
# Add the hosts_header to the hosts file in the temporary folder, filter out the line with @ and replace with HOSTS_FILE_BACKUP_PATH
sed "s|#@.\+#@|${HOSTS_FILE_BACKUP_PATH}|g" "${SCRIPT_DIR_PATH}/bin/components/hosts_header"
} >> "${TMP_DIR_PATH}/hosts"

return 0
}


countBlockedDomains() {
readonly COUNT_BLOCKED="$(cat ${TMP_DIR_PATH}/cache | wc -l)"
readonly COUNT_BLOCKED="$(wc -l < "${TMP_DIR_PATH}/cache")"

return 0
}
Expand Down

0 comments on commit f4718bf

Please sign in to comment.