Procházet zdrojové kódy

Improve shell script by shellcheck

Signed-off-by: Teddysun <[email protected]>
Teddysun před 2 roky
rodič
revize
9d94a26b12
1 změnil soubory, kde provedl 60 přidání a 53 odebrání
  1. 60 53
      bench.sh

+ 60 - 53
bench.sh

@@ -57,12 +57,16 @@ next() {
 
 speed_test() {
     local nodeName="$2"
-    [ -z "$1" ] && ./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1 || \
-        ./speedtest-cli/speedtest --progress=no --server-id=$1 --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
+    if [ -z "$1" ];then 
+        ./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
+    else
+        ./speedtest-cli/speedtest --progress=no --server-id="$1" --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
+    fi
     if [ $? -eq 0 ]; then
-        local dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
-        local up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
-        local latency=$(awk '/Latency/{print $3" "$4}' ./speedtest-cli/speedtest.log)
+        local dl_speed up_speed latency
+        dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
+        up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
+        latency=$(awk '/Latency/{print $3" "$4}' ./speedtest-cli/speedtest.log)
         if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
             printf "\033[0;33m%-18s\033[0;32m%-18s\033[0;31m%-20s\033[0;36m%-12s\033[0m\n" " ${nodeName}" "${up_speed}" "${dl_speed}" "${latency}"
         fi
@@ -86,7 +90,7 @@ speed() {
 }
 
 io_test() {
-    (LANG=C dd if=/dev/zero of=benchtest_$$ bs=512k count=$1 conv=fdatasync && rm -f benchtest_$$) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
+    (LANG=C dd if=/dev/zero of=benchtest_$$ bs=512k count="$1" conv=fdatasync && rm -f benchtest_$$) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
 }
 
 calc_size() {
@@ -111,7 +115,7 @@ calc_size() {
         echo "${total_size}"
         return
     fi
-    total_size=$(awk 'BEGIN{printf "%.1f", '$raw' / '$num'}')
+    total_size=$(awk 'BEGIN{printf "%.1f", '"$raw"' / '$num'}')
     echo "${total_size} ${unit}"
 }
 
@@ -119,14 +123,15 @@ calc_size() {
 # to_kibyte converts zfs size from bytes to kilobyte
 to_kibyte() {
     local raw=$1
-    echo ${raw} / 1024
+    awk 'BEGIN{printf "%.0f", '"$raw"' / 1024}'
 }
 
-sum() {
+calc_sum() {
     local arr=("$@")
-    local s=0
+    local s
+    s=0
     for i in "${arr[@]}"; do
-        s=$(($s + $i))
+        s=$((s + i))
     done
     echo ${s}
 }
@@ -188,20 +193,21 @@ check_virt() {
 }
 
 ipv4_info() {
-    local org="$(wget -q -T10 -O- ipinfo.io/org)"
-    local city="$(wget -q -T10 -O- ipinfo.io/city)"
-    local country="$(wget -q -T10 -O- ipinfo.io/country)"
-    local region="$(wget -q -T10 -O- ipinfo.io/region)"
-    if [[ -n "$org" ]]; then
-        echo " Organization       : $(_blue "$org")"
+    local org city country region
+    org="$(wget -q -T10 -O- ipinfo.io/org)"
+    city="$(wget -q -T10 -O- ipinfo.io/city)"
+    country="$(wget -q -T10 -O- ipinfo.io/country)"
+    region="$(wget -q -T10 -O- ipinfo.io/region)"
+    if [[ -n "${org}" ]]; then
+        echo " Organization       : $(_blue "${org}")"
     fi
-    if [[ -n "$city" && -n "country" ]]; then
-        echo " Location           : $(_blue "$city / $country")"
+    if [[ -n "${city}" && -n "${country}" ]]; then
+        echo " Location           : $(_blue "${city} / ${country}")"
     fi
-    if [[ -n "$region" ]]; then
-        echo " Region             : $(_yellow "$region")"
+    if [[ -n "${region}" ]]; then
+        echo " Region             : $(_yellow "${region}")"
     fi
-    if [[ -z "$org" ]]; then
+    if [[ -z "${org}" ]]; then
         echo " Region             : $(_red "No ISP detected")"
     fi
 }
@@ -209,9 +215,10 @@ ipv4_info() {
 install_speedtest() {
     if [ ! -e "./speedtest-cli/speedtest" ]; then
         sys_bit=""
-        local sysarch="$(uname -m)"
+        local sysarch
+        sysarch="$(uname -m)"
         if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
-            local sysarch="$(arch)"
+            sysarch="$(arch)"
         fi
         if [ "${sysarch}" = "x86_64" ]; then
             sys_bit="x86_64"
@@ -231,10 +238,10 @@ install_speedtest() {
         [ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1
         url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
         url2="https://dl.lamp.sh/files/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
-        wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}
-        if [ $? -ne 0 ]; then
-            wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}
-            [ $? -ne 0 ] && _red "Error: Failed to download speedtest-cli.\n" && exit 1
+        if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}; then
+            if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}; then
+                _red "Error: Failed to download speedtest-cli.\n" && exit 1
+            fi
         fi
         mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
         rm -f speedtest.tgz
@@ -260,22 +267,22 @@ get_system_info() {
         LANG=C
         free | awk '/Mem/ {print $2}'
     )
-    tram=$(calc_size $tram)
+    tram=$(calc_size "$tram")
     uram=$(
         LANG=C
         free | awk '/Mem/ {print $3}'
     )
-    uram=$(calc_size $uram)
+    uram=$(calc_size "$uram")
     swap=$(
         LANG=C
         free | awk '/Swap/ {print $2}'
     )
-    swap=$(calc_size $swap)
+    swap=$(calc_size "$swap")
     uswap=$(
         LANG=C
         free | awk '/Swap/ {print $3}'
     )
-    uswap=$(calc_size $uswap)
+    uswap=$(calc_size "$uswap")
     up=$(awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days, %d hour %d min\n",a,b,c)}' /proc/uptime)
     if _exists "w"; then
         load=$(
@@ -293,7 +300,7 @@ get_system_info() {
     if _exists "getconf"; then
         lbit=$(getconf LONG_BIT)
     else
-        echo ${arch} | grep -q "64" && lbit="64" || lbit="32"
+        echo "${arch}" | grep -q "64" && lbit="64" || lbit="32"
     fi
     kern=$(uname -r)
     in_kernel_no_swap_total_size=$(
@@ -301,15 +308,15 @@ get_system_info() {
         df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $2 }'
     )
     swap_total_size=$(free -k | grep Swap | awk '{print $2}')
-    zfs_total_size=$(to_kibyte $(sum $(zpool list -o size -Hp 2> /dev/null)))
-    disk_total_size=$(calc_size $(($swap_total_size + $in_kernel_no_swap_total_size + $zfs_total_size)))
+    zfs_total_size=$(to_kibyte "$(calc_sum "$(zpool list -o size -Hp 2> /dev/null)")")
+    disk_total_size=$(calc_size $((swap_total_size + in_kernel_no_swap_total_size + zfs_total_size)))
     in_kernel_no_swap_used_size=$(
         LANG=C
         df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $3 }'
     )
     swap_used_size=$(free -k | grep Swap | awk '{print $3}')
-    zfs_used_size=$(to_kibyte $(sum $(zpool list -o allocated -Hp 2> /dev/null)))
-    disk_used_size=$(calc_size $(($swap_used_size + $in_kernel_no_swap_used_size + $zfs_used_size)))
+    zfs_used_size=$(to_kibyte "$(calc_sum "$(zpool list -o allocated -Hp 2> /dev/null)")")
+    disk_used_size=$(calc_size $((swap_used_size + in_kernel_no_swap_used_size + zfs_used_size)))
     tcpctrl=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}')
 }
 # Print System information
@@ -357,7 +364,7 @@ print_io_test() {
     if [ -z "${freespace}" ]; then
         freespace=$(df -m . | awk 'NR==3 {print $3}')
     fi
-    if [ ${freespace} -gt 1024 ]; then
+    if [ "${freespace}" -gt 1024 ]; then
         writemb=2048
         io1=$(io_test ${writemb})
         echo " I/O Speed(1st run) : $(_yellow "$io1")"
@@ -365,14 +372,14 @@ print_io_test() {
         echo " I/O Speed(2nd run) : $(_yellow "$io2")"
         io3=$(io_test ${writemb})
         echo " I/O Speed(3rd run) : $(_yellow "$io3")"
-        ioraw1=$(echo $io1 | awk 'NR==1 {print $1}')
-        [ "$(echo $io1 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw1=$(awk 'BEGIN{print '$ioraw1' * 1024}')
-        ioraw2=$(echo $io2 | awk 'NR==1 {print $1}')
-        [ "$(echo $io2 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw2=$(awk 'BEGIN{print '$ioraw2' * 1024}')
-        ioraw3=$(echo $io3 | awk 'NR==1 {print $1}')
-        [ "$(echo $io3 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw3=$(awk 'BEGIN{print '$ioraw3' * 1024}')
-        ioall=$(awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}')
-        ioavg=$(awk 'BEGIN{printf "%.1f", '$ioall' / 3}')
+        ioraw1=$(echo "$io1" | awk 'NR==1 {print $1}')
+        [ "$(echo "$io1" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw1=$(awk 'BEGIN{print '"$ioraw1"' * 1024}')
+        ioraw2=$(echo "$io2" | awk 'NR==1 {print $1}')
+        [ "$(echo "$io2" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw2=$(awk 'BEGIN{print '"$ioraw2"' * 1024}')
+        ioraw3=$(echo "$io3" | awk 'NR==1 {print $1}')
+        [ "$(echo "$io3" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw3=$(awk 'BEGIN{print '"$ioraw3"' * 1024}')
+        ioall=$(awk 'BEGIN{print '"$ioraw1"' + '"$ioraw2"' + '"$ioraw3"'}')
+        ioavg=$(awk 'BEGIN{printf "%.1f", '"$ioall"' / 3}')
         echo " I/O Speed(average) : $(_yellow "$ioavg MB/s")"
     else
         echo " $(_red "Not enough space for I/O Speed test!")"
@@ -381,10 +388,10 @@ print_io_test() {
 
 print_end_time() {
     end_time=$(date +%s)
-    time=$((${end_time} - ${start_time}))
+    time=$((end_time - start_time))
     if [ ${time} -gt 60 ]; then
-        min=$(expr $time / 60)
-        sec=$(expr $time % 60)
+        min=$((time / 60))
+        sec=$((time % 60))
         echo " Finished in        : ${min} min ${sec} sec"
     else
         echo " Finished in        : ${time} sec"
@@ -396,11 +403,11 @@ print_end_time() {
 ! _exists "wget" && _red "Error: wget command not found.\n" && exit 1
 ! _exists "free" && _red "Error: free command not found.\n" && exit 1
 # check for curl/wget
-command -v curl >/dev/null 2>&1 && local_curl=true || unset local_curl
+_exists "curl" && local_curl=true
 # test if the host has IPv4/IPv6 connectivity
-[[ ! -z ${local_curl} ]] && ip_check_cmd="curl -s -m 4" || ip_check_cmd="wget -qO- -T 4"
-ipv4_check=$((ping -4 -c 1 -W 4 ipv4.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -4 icanhazip.com 2> /dev/null)
-ipv6_check=$((ping -6 -c 1 -W 4 ipv6.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -6 icanhazip.com 2> /dev/null)
+[[ -n ${local_curl} ]] && ip_check_cmd="curl -s -m 4" || ip_check_cmd="wget -qO- -T 4"
+ipv4_check=$( (ping -4 -c 1 -W 4 ipv4.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -4 icanhazip.com 2> /dev/null)
+ipv6_check=$( (ping -6 -c 1 -W 4 ipv6.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -6 icanhazip.com 2> /dev/null)
 if [[ -z "$ipv4_check" && -z "$ipv6_check" ]]; then
     _yellow "Warning: Both IPv4 and IPv6 connectivity were not detected.\n"
 fi