Skip to content

Commit 53bc492

Browse files
authored
Make shell command substitutions safe with || true (#13279)
Add defensive fallbacks (|| true) to multiple command substitutions to prevent non-zero exits when commands produce no output or are unavailable. Changes touch misc/api.func, misc/build.func and misc/tools.func and cover places like lspci, /proc/cpuinfo parsing, /etc/os-release reads, hostname -I usage, grep reads from vars files and maps, pct config parsing, storage/template lookups, tool version detection, NVIDIA driver version extraction, and MeiliSearch config parsing. These edits do not change functional behavior aside from ensuring the scripts continue running (variables will be empty) instead of failing in stricter shells or when commands return non-zero status.
1 parent de356fa commit 53bc492

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

misc/api.func

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ detect_gpu() {
504504
GPU_PASSTHROUGH="unknown"
505505

506506
local gpu_line
507-
gpu_line=$(lspci 2>/dev/null | grep -iE "VGA|3D|Display" | head -1)
507+
gpu_line=$(lspci 2>/dev/null | grep -iE "VGA|3D|Display" | head -1 || true)
508508

509509
if [[ -n "$gpu_line" ]]; then
510510
# Extract model: everything after the colon, clean up
@@ -543,7 +543,7 @@ detect_cpu() {
543543

544544
if [[ -f /proc/cpuinfo ]]; then
545545
local vendor_id
546-
vendor_id=$(grep -m1 "vendor_id" /proc/cpuinfo 2>/dev/null | cut -d: -f2 | tr -d ' ')
546+
vendor_id=$(grep -m1 "vendor_id" /proc/cpuinfo 2>/dev/null | cut -d: -f2 | tr -d ' ' || true)
547547

548548
case "$vendor_id" in
549549
GenuineIntel) CPU_VENDOR="intel" ;;
@@ -557,7 +557,7 @@ detect_cpu() {
557557
esac
558558

559559
# Extract model name and clean it up
560-
CPU_MODEL=$(grep -m1 "model name" /proc/cpuinfo 2>/dev/null | cut -d: -f2 | sed 's/^ *//' | sed 's/(R)//g' | sed 's/(TM)//g' | sed 's/ */ /g' | cut -c1-64)
560+
CPU_MODEL=$(grep -m1 "model name" /proc/cpuinfo 2>/dev/null | cut -d: -f2 | sed 's/^ *//' | sed 's/(R)//g' | sed 's/(TM)//g' | sed 's/ */ /g' | cut -c1-64 || true)
561561
fi
562562

563563
export CPU_VENDOR CPU_MODEL
@@ -1347,8 +1347,8 @@ post_addon_to_api() {
13471347
# Detect OS info
13481348
local os_type="" os_version=""
13491349
if [[ -f /etc/os-release ]]; then
1350-
os_type=$(grep "^ID=" /etc/os-release | cut -d= -f2 | tr -d '"')
1351-
os_version=$(grep "^VERSION_ID=" /etc/os-release | cut -d= -f2 | tr -d '"')
1350+
os_type=$(grep "^ID=" /etc/os-release | cut -d= -f2 | tr -d '"' || true)
1351+
os_version=$(grep "^VERSION_ID=" /etc/os-release | cut -d= -f2 | tr -d '"' || true)
13521352
fi
13531353

13541354
local JSON_PAYLOAD

misc/build.func

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ get_current_ip() {
173173
# Check for Debian/Ubuntu (uses hostname -I)
174174
if grep -qE 'ID=debian|ID=ubuntu' /etc/os-release; then
175175
# Try IPv4 first
176-
CURRENT_IP=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | head -n1)
176+
CURRENT_IP=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | head -n1 || true)
177177
# Fallback to IPv6 if no IPv4
178178
if [[ -z "$CURRENT_IP" ]]; then
179-
CURRENT_IP=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E ':' | head -n1)
179+
CURRENT_IP=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E ':' | head -n1 || true)
180180
fi
181181
# Check for Alpine (uses ip command)
182182
elif grep -q 'ID=alpine' /etc/os-release; then
@@ -1704,8 +1704,8 @@ ensure_storage_selection_for_vars_file() {
17041704

17051705
# Read stored values (if any)
17061706
local tpl ct
1707-
tpl=$(grep -E '^var_template_storage=' "$vf" | cut -d= -f2-)
1708-
ct=$(grep -E '^var_container_storage=' "$vf" | cut -d= -f2-)
1707+
tpl=$(grep -E '^var_template_storage=' "$vf" | cut -d= -f2- || true)
1708+
ct=$(grep -E '^var_container_storage=' "$vf" | cut -d= -f2- || true)
17091709

17101710
if [[ -n "$tpl" && -n "$ct" ]]; then
17111711
TEMPLATE_STORAGE="$tpl"
@@ -1840,7 +1840,7 @@ advanced_settings() {
18401840
if [[ -n "$BRIDGES" ]]; then
18411841
while IFS= read -r bridge; do
18421842
if [[ -n "$bridge" ]]; then
1843-
local description=$(grep -A 10 "iface $bridge" /etc/network/interfaces 2>/dev/null | grep '^#' | head -n1 | sed 's/^#\s*//;s/^[- ]*//')
1843+
local description=$(grep -A 10 "iface $bridge" /etc/network/interfaces 2>/dev/null | grep '^#' | head -n1 | sed 's/^#\s*//;s/^[- ]*//' || true)
18441844
BRIDGE_MENU_OPTIONS+=("$bridge" "${description:- }")
18451845
fi
18461846
done <<<"$BRIDGES"
@@ -3322,7 +3322,7 @@ configure_ssh_settings() {
33223322
tag="${tag%\"}"
33233323
tag="${tag#\"}"
33243324
local line
3325-
line=$(grep -E "^${tag}\|" "$MAPFILE" | head -n1 | cut -d'|' -f2-)
3325+
line=$(grep -E "^${tag}\|" "$MAPFILE" | head -n1 | cut -d'|' -f2- || true)
33263326
[[ -n "$line" ]] && printf '%s\n' "$line" >>"$SSH_KEYS_FILE"
33273327
done
33283328
;;
@@ -3349,7 +3349,7 @@ configure_ssh_settings() {
33493349
tag="${tag%\"}"
33503350
tag="${tag#\"}"
33513351
local line
3352-
line=$(grep -E "^${tag}\|" "$MAPFILE" | head -n1 | cut -d'|' -f2-)
3352+
line=$(grep -E "^${tag}\|" "$MAPFILE" | head -n1 | cut -d'|' -f2- || true)
33533353
[[ -n "$line" ]] && printf '%s\n' "$line" >>"$SSH_KEYS_FILE"
33543354
done
33553355
else
@@ -4050,7 +4050,7 @@ EOF
40504050
# Fix Debian 13 LXC template bug where / is owned by nobody:nogroup
40514051
# This must be done from the host as unprivileged containers cannot chown /
40524052
local rootfs
4053-
rootfs=$(pct config "$CTID" | grep -E '^rootfs:' | sed 's/rootfs: //' | cut -d',' -f1)
4053+
rootfs=$(pct config "$CTID" | grep -E '^rootfs:' | sed 's/rootfs: //' | cut -d',' -f1 || true)
40544054
if [[ -n "$rootfs" ]]; then
40554055
local mount_point="/var/lib/lxc/${CTID}/rootfs"
40564056
if [[ -d "$mount_point" ]] && [[ "$(stat -c '%U' "$mount_point")" != "root" ]]; then
@@ -5142,7 +5142,7 @@ create_lxc_container() {
51425142
fi
51435143

51445144
msg_info "Validating storage '$CONTAINER_STORAGE'"
5145-
STORAGE_TYPE=$(grep -E "^[^:]+: $CONTAINER_STORAGE$" /etc/pve/storage.cfg | cut -d: -f1 | head -1)
5145+
STORAGE_TYPE=$(grep -E "^[^:]+: $CONTAINER_STORAGE$" /etc/pve/storage.cfg | cut -d: -f1 | head -1 || true)
51465146

51475147
if [[ -z "$STORAGE_TYPE" ]]; then
51485148
msg_error "Storage '$CONTAINER_STORAGE' not found in /etc/pve/storage.cfg"
@@ -5181,7 +5181,7 @@ create_lxc_container() {
51815181
msg_ok "Storage '$CONTAINER_STORAGE' ($STORAGE_TYPE) validated"
51825182

51835183
msg_info "Validating template storage '$TEMPLATE_STORAGE'"
5184-
TEMPLATE_TYPE=$(grep -E "^[^:]+: $TEMPLATE_STORAGE$" /etc/pve/storage.cfg | cut -d: -f1)
5184+
TEMPLATE_TYPE=$(grep -E "^[^:]+: $TEMPLATE_STORAGE$" /etc/pve/storage.cfg | cut -d: -f1 || true)
51855185

51865186
if ! pvesm status -content vztmpl 2>/dev/null | awk 'NR>1{print $1}' | grep -qx "$TEMPLATE_STORAGE"; then
51875187
msg_warn "Template storage '$TEMPLATE_STORAGE' may not support 'vztmpl'"

misc/tools.func

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,12 @@ is_tool_installed() {
524524
case "$tool_name" in
525525
mariadb)
526526
if command -v mariadb >/dev/null 2>&1; then
527-
installed_version=$(mariadb --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
527+
installed_version=$(mariadb --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)
528528
fi
529529
;;
530530
mysql)
531531
if command -v mysql >/dev/null 2>&1; then
532-
installed_version=$(mysql --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
532+
installed_version=$(mysql --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)
533533
fi
534534
;;
535535
mongodb | mongod)
@@ -539,7 +539,7 @@ is_tool_installed() {
539539
;;
540540
node | nodejs)
541541
if command -v node >/dev/null 2>&1; then
542-
installed_version=$(node -v 2>/dev/null | grep -oP '^v\K[0-9]+')
542+
installed_version=$(node -v 2>/dev/null | grep -oP '^v\K[0-9]+' || true)
543543
fi
544544
;;
545545
php)
@@ -4837,7 +4837,7 @@ _setup_nvidia_gpu() {
48374837
# Use regex to extract version number (###.##.## or ###.## pattern)
48384838
local nvidia_host_version=""
48394839
if [[ -f /proc/driver/nvidia/version ]]; then
4840-
nvidia_host_version=$(grep -oP '\d{3,}\.\d+(\.\d+)?' /proc/driver/nvidia/version 2>/dev/null | head -1)
4840+
nvidia_host_version=$(grep -oP '\d{3,}\.\d+(\.\d+)?' /proc/driver/nvidia/version 2>/dev/null | head -1 || true)
48414841
fi
48424842
48434843
if [[ -z "$nvidia_host_version" ]]; then
@@ -7321,7 +7321,7 @@ function setup_meilisearch() {
73217321
MEILI_HOST="${MEILISEARCH_HOST:-127.0.0.1}"
73227322
MEILI_PORT="${MEILISEARCH_PORT:-7700}"
73237323
MEILI_DUMP_DIR="${MEILISEARCH_DUMP_DIR:-/var/lib/meilisearch/dumps}"
7324-
MEILI_MASTER_KEY=$(grep -E "^master_key\s*=" /etc/meilisearch.toml 2>/dev/null | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d ' ')
7324+
MEILI_MASTER_KEY=$(grep -E "^master_key\s*=" /etc/meilisearch.toml 2>/dev/null | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d ' ' || true)
73257325
73267326
# Create dump before update if migration is needed
73277327
local DUMP_UID=""
@@ -7387,7 +7387,7 @@ function setup_meilisearch() {
73877387
# We choose option 2: backup and proceed with warning
73887388
if [[ "$NEEDS_MIGRATION" == "true" ]] && [[ -z "$DUMP_UID" ]]; then
73897389
local MEILI_DB_PATH
7390-
MEILI_DB_PATH=$(grep -E "^db_path\s*=" /etc/meilisearch.toml 2>/dev/null | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d ' ')
7390+
MEILI_DB_PATH=$(grep -E "^db_path\s*=" /etc/meilisearch.toml 2>/dev/null | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d ' ' || true)
73917391
MEILI_DB_PATH="${MEILI_DB_PATH:-/var/lib/meilisearch/data}"
73927392
73937393
if [[ -d "$MEILI_DB_PATH" ]] && [[ -n "$(ls -A "$MEILI_DB_PATH" 2>/dev/null)" ]]; then
@@ -7407,7 +7407,7 @@ function setup_meilisearch() {
74077407
# If migration needed and dump was created, remove old data and import dump
74087408
if [[ "$NEEDS_MIGRATION" == "true" ]] && [[ -n "$DUMP_UID" ]]; then
74097409
local MEILI_DB_PATH
7410-
MEILI_DB_PATH=$(grep -E "^db_path\s*=" /etc/meilisearch.toml 2>/dev/null | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d ' ')
7410+
MEILI_DB_PATH=$(grep -E "^db_path\s*=" /etc/meilisearch.toml 2>/dev/null | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d ' ' || true)
74117411
MEILI_DB_PATH="${MEILI_DB_PATH:-/var/lib/meilisearch/data}"
74127412
74137413
msg_info "Removing old MeiliSearch database for migration"

0 commit comments

Comments
 (0)