NASutils Knowledge BaseUnofficialPolski

Bash · projectA greeting at the SSH login

This script greets you when you log in over SSH and shows the state of the NAS in one framed banner: model, uptime, cores and logical processors, load, the free space of each data volume and a tip of the day. It runs with QTS's own Bash 3.2, so it needs no extra package.

Programming on the NAS › Bash in 30 lessons

╔═ Hello ══════════════════════════════════════════════════════════════════╗
║                                                                          ║
║   ░▒▓█ Good morning, root █▓▒░                        2026-11-27 09:30   ║
║                                                                          ║
║   Model       TBS-h574TX                                                 ║
║   Up for      1 d 13 h 10 min                                            ║
║   CPU         10 cores, 12 logical processors                            ║
║   Load        1.17  0.90  0.88  (1, 5 and 15 minutes)                    ║
║                                                                          ║
║   Free space on the data volumes                                         ║
║   CACHEDEV1_DATA  ████░░░░░░░░░░░░░░░░  1044.8 GiB free of 1327.1 GiB    ║
║   CACHEDEV2_DATA  ██░░░░░░░░░░░░░░░░░░  1054.6 GiB free of 1232.3 GiB    ║
║                                                                          ║
║   Tip  cat /proc/mdstat shows the state of every RAID group.             ║
║                                                                          ║
╚══════════════════════════════════════════════════════════════════════════╝
The banner at 09:30: "Good morning", the model TBS-h574TX, 10 cores and 12 logical processors, two data volumes with green bars, and the tip for SEED=7.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/bash/hello.sh

hello.sh Download hello.sh

#!/usr/bin/env bash
# hello.sh: a greeting for the SSH login. It prints a framed banner with a greeting for the time of day, the NAS
# model, its uptime and load, the free space of the data volumes and a tip of the day, then ends.
# Run it: bash hello.sh   (NOW, SEED, PROC and DF may replace the clock, the tip's number, /proc and df -k)
# It uses nothing newer than Bash 3.2, the version of QTS's own /bin/sh.
set -uo pipefail

PROC=${PROC:-/proc}
NOW=${NOW:-$(date '+%Y-%m-%d %H:%M:%S')}
SEED=${SEED:-$(date '+%j')}
A=$'\e[38;5;214m' G=$'\e[32m' R=$'\e[31m' D=$'\e[2m' B=$'\e[1m' N=$'\e[0m'
W=76 # the width of the frame

edge() { # LEFT RIGHT TITLE: the top or the bottom edge
  local line
  if [[ -n $3 ]]; then
    printf -v line '%*s' $((W - 5 - ${#3})) ''
    line="═ $3 ${line// /═}"
  else
    printf -v line '%*s' $((W - 2)) ''
    line=${line// /═}
  fi
  printf '%s%s%s%s%s\n' "$A" "$1" "$line" "$2" "$N"
}

row() { # TEXT: one line inside the frame; \e[NG puts the right edge at column W whatever the text holds
  printf '%s║%s   %s\e[%dG%s║%s\n' "$A" "$N" "$1" "$W" "$A" "$N"
}

bar() { # PERCENT WIDTH: green up to 69 %, amber up to 89 %, red from 90 %
  local p=$1 w=$2 full colour=$G fill rest
  ((p >= 70)) && colour=$A
  ((p >= 90)) && colour=$R
  full=$((p * w / 100))
  printf -v fill '%*s' "$full" ''
  printf -v rest '%*s' $((w - full)) ''
  printf '%s%s%s%s%s' "$colour" "${fill// /█}" "$D" "${rest// /░}" "$N"
}

gib() { # KIB: the size in GiB with one decimal
  local t=$((($1 * 10 + 524288) / 1048576))
  printf '%d.%d' $((t / 10)) $((t % 10))
}

hour=$((10#${NOW:11:2}))
if ((hour >= 5 && hour < 12)); then
  greeting="Good morning"
elif ((hour >= 12 && hour < 18)); then
  greeting="Good afternoon"
elif ((hour >= 18 && hour < 22)); then
  greeting="Good evening"
else
  greeting="Working late"
fi

tips=(
  "getsysinfo cputmp prints the temperature of the CPU."
  "cat /proc/mdstat shows the state of every RAID group."
  "getcfg System Version prints the version of QTS."
  "ls /share lists the shared folders of the NAS."
  "df -h shows the size and the free space of every volume."
  "uptime prints how long the NAS has run and its load."
)
tip=${tips[$((10#$SEED % ${#tips[@]}))]}

model=$(getsysinfo model 2>/dev/null) || model=$(uname -n)
read -r up _ <"$PROC/uptime"
up=${up%.*}
read -r l1 l5 l15 _ <"$PROC/loadavg"
cpus=$(grep -c '^processor' "$PROC/cpuinfo")
cores=$(awk -F': ' '/^physical id/ {p = $2} /^core id/ {print p "-" $2}' "$PROC/cpuinfo" | sort -u | wc -l)

edge ╔ ╗ "Hello"
row ""
row "$A░▒▓█$N $B$greeting, ${USER:-$(id -un)}$N $A█▓▒░$N$(printf '\e[%dG' $((W - 19)))$D${NOW:0:16}$N"
row ""
row "$(printf '%-12s' Model)$B$model$N"
row "$(printf '%-12s' 'Up for')$((up / 86400)) d $((up % 86400 / 3600)) h $((up % 3600 / 60)) min"
row "$(printf '%-12s' CPU)$cores cores, $cpus logical processors"
row "$(printf '%-12s' Load)$l1  $l5  $l15  $D(1, 5 and 15 minutes)$N"
row ""
row "${B}Free space on the data volumes$N"
while read -r _ size used avail pct mnt; do
  [[ $mnt == /share/* && $mnt != /share/*/* ]] || continue
  row "$(printf '%-16s' "${mnt#/share/}")$(bar "${pct%\%}" 20)  $G$(gib "$avail") GiB$N free of $(gib "$size") GiB"
done < <(if [[ -n ${DF:-} ]]; then cat "$DF"; else df -k; fi)
row ""
row "${A}Tip$N  $tip"
row ""
edge ╚ ╝ ""

What you learn

  • \e[76G moves the cursor to column 76, so the right edge of the frame lines up whatever colours the line holds.
  • ${NOW:11:2} cuts the hour out of a date string, and 10# reads "09" as nine, not as a wrong octal number.
  • ${tips[$((SEED % ${#tips[@]}))]} picks one entry of an array by a number, here the day of the year.
  • [[ $mnt == /share/* && $mnt != /share/*/* ]] keeps the data volumes and skips folders mounted inside them.
  • Where QTS reads the login files, and why a line added to them is gone after a restart.

How it works

The script draws the banner from the top down. edge prints the top and the bottom edge, row one line between them. row prints the left edge, the text, then \e[76G and the right edge, so it never has to count the width of coloured text.

The greeting depends on the hour: morning from 5, afternoon from 12, evening from 18, and "Working late" from 22. The tip is one of six commands of QTS; its number is the day of the year, so you get a new tip each day. In the test, SEED=7 fixes it.

The facts come from the same places as in the menu: getsysinfo model, /proc/uptime, /proc/loadavg, /proc/cpuinfo and df -k. The script counts cores as the distinct pairs of physical id and core id, and logical processors as the processor lines. On a real NAS, df -k also lists folders that Container Station and Samba mount inside a volume, such as /share/CACHEDEV1_DATA/.samba/lock/msg.lock. The test of the mount point leaves only /share/CACHEDEV1_DATA and its kind.

Notes to the lines

  • 5Arrays, [[ ]], printf -v and $'\e' all work in Bash 3.2, so the script also runs as QTS's own bash.
  • 9–10Without NOW and SEED the script takes the clock and the day of the year (date '+%j').
  • 14–24edge makes a line of ═ from spaces and writes the title into it when there is one.
  • 26–28row prints the text between the edges; \e[%dG jumps to column W for the right edge.
  • 4510# makes Bash read the hour in base 10, so "08" and "09" do not stop the script.
  • 46–54The greeting by the hour.
  • 64The remainder of the division by the number of tips is always a valid index of the array.
  • 71awk prints one pair of physical id and core id per logical processor; sort -u | wc -l counts the distinct pairs, the cores.
  • 83–86One row per data volume: name, bar of the used space, free space and size in GiB.

Run it

bash hello.sh

The script prints the banner and ends.

To see it at every SSH login, copy hello.sh to a data volume, for example /share/homes/admin/hello.sh (that folder exists when Enable home folder for all users is on under Users, Home Folder). The admin account logs in over SSH with the home folder /root and QTS's /bin/sh, which is Bash 3.2. That shell reads /etc/profile and then /root/.profile. QTS keeps its root file system in RAM (df / shows none), so both files start fresh after every restart. A line you add to them is gone after a restart. TurboShell adds its own line again each time it starts for the same reason.

Add the line again after each restart:

echo '[ -f /share/homes/admin/hello.sh ] && bash /share/homes/admin/hello.sh' >> /root/.profile

The next SSH login shows the banner. QTS also has a boot hook: with Run user defined processes during startup on (Control Panel, Hardware, General), it runs autorun.sh from its configuration partition at every boot, and that script could add the line. Editing autorun.sh means mounting that partition by hand, so this page keeps to the line above.

Try it yourself

Add a row with the CPU temperature from getsysinfo cputmp. Add a seventh tip to the array and run the script with SEED=6 to see it.

More projects in Bash