From 69b536300dc642b3b27d68cf471e73178e487c73 Mon Sep 17 00:00:00 2001 From: Jason Hee Date: Mon, 5 Dec 2016 15:28:09 +0800 Subject: [PATCH] Refactored code based on shellcheck --- setup.sh | 46 ++++++++++++++++++++++----------------------- setupLibrary.sh | 17 ++++++++++------- tests/tests.sh | 2 +- tests/unit-tests.sh | 29 ++++++++++++++++++---------- 4 files changed, 52 insertions(+), 42 deletions(-) diff --git a/setup.sh b/setup.sh index 241f532..fdc446e 100644 --- a/setup.sh +++ b/setup.sh @@ -5,10 +5,11 @@ set -e function getCurrentDir() { local current_dir="${BASH_SOURCE%/*}" if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi - echo ${current_dir} + echo "${current_dir}" } function includeDependencies() { + # shellcheck source=./setupLibrary.sh source "${current_dir}/setupLibrary.sh" } @@ -24,13 +25,7 @@ function setupSwap() { } function hasSwap() { - local swap_status=$(sudo swapon -s) - - if [[ "$(sudo swapon -s)" == *"/swapfile"* ]]; then - echo "true" - else - echo "false" - fi + [[ "$(sudo swapon -s)" == *"/swapfile"* ]] } function cleanup() { @@ -41,15 +36,17 @@ function cleanup() { function logTimestamp() { local filename=${1} - echo "===================" >>"${filename}" 2>&1 - echo "Log generated on $(date)" >>"${filename}" 2>&1 - echo "===================" >>"${filename}" 2>&1 + { + echo "===================" + echo "Log generated on $(date)" + echo "===================" + } >>"${filename}" 2>&1 } -read -p "Enter the username of the new user account:" username -read -s -p "Enter new UNIX password:" password +read -rp "Enter the username of the new user account:" username +read -s -rp "Enter new UNIX password:" password printf "\n" -read -s -p "Retype new UNIX password:" password_confirmation +read -s -rp "Retype new UNIX password:" password_confirmation printf "\n" if [[ "${password}" != "${password_confirmation}" ]]; then @@ -65,23 +62,24 @@ read -rp $'Paste in the public SSH key for the new user:\n' sshKey echo 'Running setup script...' logTimestamp "${output_file}" -disableSudoPassword "${username}" >>"${output_file}" 2>&1 -addSSHKey "${username}" "${sshKey}" >>"${output_file}" 2>&1 -changeSSHConfig >>"${output_file}" 2>&1 -setupUfw >>"${output_file}" 2>&1 +exec 3>&1 >>"${output_file}" 2>&1 +disableSudoPassword "${username}" +addSSHKey "${username}" "${sshKey}" +changeSSHConfig +setupUfw -if [[ $(hasSwap) == "false" ]]; then - setupSwap >>"${output_file}" 2>&1 +if ! hasSwap; then + setupSwap fi timezone="Asia/Singapore" -setTimezone "${timezone}" >>"${output_file}" 2>&1 -echo "Timezone is set to ${timezone}" +setTimezone "${timezone}" +echo "Timezone is set to ${timezone}" >&3 -configureNTP >>"${output_file}" 2>&1 +configureNTP sudo service ssh restart cleanup -echo "Setup Done! Log file is located at ${output_file}" \ No newline at end of file +echo "Setup Done! Log file is located at ${output_file}" >&3 \ No newline at end of file diff --git a/setupLibrary.sh b/setupLibrary.sh index aac93fb..7b5fc16 100644 --- a/setupLibrary.sh +++ b/setupLibrary.sh @@ -17,7 +17,7 @@ function addUserAccount() { fi echo "${username}:${password}" | sudo chpasswd - sudo usermod -aG sudo ${username} + sudo usermod -aG sudo "${username}" } # Add the local machine public SSH Key for the new user account @@ -45,8 +45,9 @@ function execAsUser() { } # Modify the sshd_config file +# shellcheck disable=2116 function changeSSHConfig() { - sudo sed -re 's/^(\#?)(PasswordAuthentication)([[:space:]]+)yes/\2\3no/' -i.$(echo 'old') /etc/ssh/sshd_config + sudo sed -re 's/^(\#?)(PasswordAuthentication)([[:space:]]+)yes/\2\3no/' -i."$(echo 'old')" /etc/ssh/sshd_config sudo sed -re 's/^(\#?)(PermitRootLogin)([[:space:]]+)(.*)/PermitRootLogin no/' -i /etc/ssh/sshd_config } @@ -61,7 +62,7 @@ function createSwap() { local swapmem=$(($(getPhysicalMemory) * 2)) # Anything over 4GB in swap is probably unnecessary as a RAM fallback - if [[ ${swapmem} > 4 ]]; then + if [ ${swapmem} -gt 4 ]; then phymem=4 fi @@ -85,8 +86,8 @@ function tweakSwapSettings() { local swappiness=${1} local vfs_cache_pressure=${2} - sudo sysctl vm.swappiness=${swappiness} - sudo sysctl vm.vfs_cache_pressure=${vfs_cache_pressure} + sudo sysctl vm.swappiness="${swappiness}" + sudo sysctl vm.vfs_cache_pressure="${vfs_cache_pressure}" } # Save the modified swap settings @@ -113,11 +114,13 @@ function configureNTP() { # Gets the amount of physical memory in GB (rounded up) installed on the machine function getPhysicalMemory() { - local phymem=$(free -g|awk '/^Mem:/{print $2}') + local phymem + phymem="$(free -g|awk '/^Mem:/{print $2}')" + if [[ ${phymem} == '0' ]]; then echo 1 else - echo ${phymem} + echo "${phymem}" fi } diff --git a/tests/tests.sh b/tests/tests.sh index 1e94e34..cbc6854 100644 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -10,7 +10,7 @@ set -ex function getCurrentDir() { local current_dir="${BASH_SOURCE%/*}" if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi - echo ${current_dir} + echo "${current_dir}" } function runUnitTest() { diff --git a/tests/unit-tests.sh b/tests/unit-tests.sh index 075c6e5..672802b 100644 --- a/tests/unit-tests.sh +++ b/tests/unit-tests.sh @@ -3,7 +3,7 @@ function getCurrentDir() { local current_dir="${BASH_SOURCE%/*}" if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi - echo ${current_dir} + echo "${current_dir}" } current_dir=$(getCurrentDir) @@ -21,12 +21,14 @@ function testSetup () { } function testUserAccountCreated() { - local user_exists_code=$(id -u ${test_user_account} > /dev/null 2>&1; echo $?) - assertEquals 0 ${user_exists_code} + local user_exists_code + user_exists_code="$(id -u ${test_user_account} > /dev/null 2>&1; echo $?)" + assertEquals 0 "${user_exists_code}" } function testIfUserIsSudo() { - local user_access=$(sudo -l -U ${test_user_account}) + local user_access + user_access="$(sudo -l -U ${test_user_account})" assertContains "(ALL : ALL) ALL" "${user_access}" } @@ -36,14 +38,16 @@ function testAddingOfSSHKey() { local dummy_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBGTO0tsVejssuaYR5R3Y/i73SppJAhme1dH7W2c47d4gOqB4izP0+fRLfvbz/tnXFz4iOP/H6eCV05hqUhF+KYRxt9Y8tVMrpDZR2l75o6+xSbUOMu6xN+uVF0T9XzKcxmzTmnV7Na5up3QM3DoSRYX/EP3utr2+zAqpJIfKPLdA74w7g56oYWI9blpnpzxkEd3edVJOivUkpZ4JoenWManvIaSdMTJXMy3MtlQhva+j9CgguyVbUkdzK9KKEuah+pFZvaugtebsU+bllPTB0nlXGIJk98Ie9ZtxuY3nCKneB+KjKiXrAvXUPCI9mWkYS/1rggpFmu3HbXBnWSUdf localuser@machine.local" addSSHKey "${test_user_account}" "${dummy_key}" - local ssh_file="$(sudo cat /home/${test_user_account}/.ssh/authorized_keys)" + local ssh_file + ssh_file="$(sudo cat /home/${test_user_account}/.ssh/authorized_keys)" assertEquals "${ssh_file}" "${dummy_key}" } function testChangeSSHConfig() { changeSSHConfig - local ssh_config="$(sudo cat /etc/ssh/sshd_config)" + local ssh_config + ssh_config="$(sudo cat /etc/ssh/sshd_config)" assertContains "PasswordAuthentication no" "${ssh_config}" assertContains "PermitRootLogin no" "${ssh_config}" } @@ -51,7 +55,8 @@ function testChangeSSHConfig() { function testUfw() { setupUfw - local ufw_status=$(sudo ufw status) + local ufw_status + ufw_status="$(sudo ufw status)" assertContains "Status: active" "${ufw_status}" assertContains "OpenSSH" "${ufw_status}" } @@ -64,8 +69,11 @@ function testSwap() { } function testSwapSettings() { - local swappiness=$(cat /proc/sys/vm/swappiness) - local cache_pressure=$(cat /proc/sys/vm/vfs_cache_pressure) + local swappiness + local cache_pressure + + swappiness="$(cat /proc/sys/vm/swappiness)" + cache_pressure="$(cat /proc/sys/vm/vfs_cache_pressure)" tweakSwapSettings 10 50 @@ -76,7 +84,8 @@ function testSwapSettings() { } function testTimezone() { - local timezone="$(cat /etc/timezone)" + local timezone + timezone="$(cat /etc/timezone)" setTimezone "America/New_York" assertEquals "America/New_York" "$(cat /etc/timezone)"