]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc/checkconfig: replace `cat | grep` with `grep`
authorSimon Deziel <simon.deziel@canonical.com>
Fri, 6 Oct 2023 05:43:04 +0000 (01:43 -0400)
committerSimon Deziel <simon.deziel@canonical.com>
Fri, 6 Oct 2023 05:43:04 +0000 (01:43 -0400)
Also speedup `grep` invokations while at it.

Signed-off-by: Simon Deziel <simon.deziel@canonical.com>
src/lxc/cmd/lxc-checkconfig.in

index 85333e5e1072cad3936d18af3f8237a1acb028ed..b007be664a620a723e9dc3b9394f62ecf5b585c9 100755 (executable)
@@ -1,11 +1,14 @@
 #!/bin/sh
 # SPDX-License-Identifier: LGPL-2.1+
 
+export LC_ALL=C.UTF-8
+export LANGUAGE=en
+
 # Allow environment variables to override config
 : ${CONFIG:=/proc/config.gz}
 : ${MODNAME:=configs}
 
-CAT="cat"
+GREP="grep"
 
 if [ -t 1 ]; then
     SETCOLOR_SUCCESS="printf \\033[1;32m"
@@ -20,7 +23,7 @@ else
 fi
 
 is_set() {
-    $CAT $CONFIG | grep "$1=[y|m]" > /dev/null
+    $GREP -wm1 "^${1}=[y|m]" "${CONFIG}" > /dev/null
     return $?
 }
 
@@ -61,7 +64,7 @@ is_probed() {
     if [ ! -f /proc/modules ]; then
         return
     fi
-    lsmod | grep $1 > /dev/null
+    lsmod | grep -wm1 "^${1}" > /dev/null
     if [ $? -eq 0 ]; then
         echo -n ", loaded"
     else
@@ -102,17 +105,17 @@ if [ ! -f $CONFIG ]; then
 fi
 
 if gunzip -tq < $CONFIG 2>/dev/null; then
-    CAT="zcat"
+    GREP="zgrep"
 fi
 
-KVER_MAJOR=$($CAT $CONFIG | grep '^# Linux.*Kernel Configuration' | \
-    sed -r 's/.* ([0-9])\.[0-9]{1,2}\.[0-9]{1,3}.*/\1/')
+KVER_MAJOR="$($GREP -m1 '^# Linux.*Kernel Configuration' "${CONFIG}" | \
+    sed -r 's/.* ([0-9])\.[0-9]{1,2}\.[0-9]{1,3}.*/\1/')"
 if [ "$KVER_MAJOR" = "2" ]; then
-KVER_MINOR=$($CAT $CONFIG | grep '^# Linux.*Kernel Configuration' | \
-    sed -r 's/.* 2.6.([0-9]{2}).*/\1/')
+  KVER_MINOR="$($GREP -m1 '^# Linux.*Kernel Configuration' "${CONFIG}" | \
+    sed -r 's/.* 2.6.([0-9]{2}).*/\1/')"
 else
-KVER_MINOR=$($CAT $CONFIG | grep '^# Linux.*Kernel Configuration' | \
-    sed -r 's/.* [0-9]\.([0-9]{1,3})\.[0-9]{1,3}.*/\1/')
+  KVER_MINOR="$($GREP -m1 '^# Linux.*Kernel Configuration' "${CONFIG}" | \
+    sed -r 's/.* [0-9]\.([0-9]{1,3})\.[0-9]{1,3}.*/\1/')"
 fi
 
 if [ -z "${KVER_MAJOR}" ]; then
@@ -184,14 +187,14 @@ for mnt in ${CGROUP_V2_MNTS}; do
 done
 
 if [ "${CGROUP_V2_MNTS}" != "/sys/fs/cgroup" ]; then
-    CGROUP_SYSTEMD_MNTPT=$(echo "$CGROUP_V1_MNTS" | grep "/systemd")
+    CGROUP_SYSTEMD_MNTPT=$(echo "$CGROUP_V1_MNTS" | grep -F "/systemd")
     if [ -z "$CGROUP_SYSTEMD_MNTPT" ]; then
         echo -n "Cgroup v1 systemd controller: "
         $SETCOLOR_FAILURE && echo -n "missing" && $SETCOLOR_NORMAL
         echo
     fi
 
-    CGROUP_FREEZER_MNTPT=$(echo "$CGROUP_V1_MNTS" | grep "/freezer")
+    CGROUP_FREEZER_MNTPT=$(echo "$CGROUP_V1_MNTS" | grep -F "/freezer")
     if [ -z "$CGROUP_FREEZER_MNTPT" ]; then
         echo -n "Cgroup v1 freezer controller: "
         $SETCOLOR_FAILURE && echo -n "missing" && $SETCOLOR_NORMAL