Skip to contentSkip to conversation

Make the script complaint with 'sh' shell

PATCH Superseded Hubert Mazur 2 participants 4 messages Mar 18 – Apr 1 Kselftest

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Make the script complaint with 'sh' shell

Hello,

I'm sending a change that modifies the script a bit to make it compliant with the 'sh' shell. The reason behind it is that the test cannot be executed on x86 embedded devices without bash. The introduced changes are:
- /bin/bash -> /bin/sh
- Usage of env $UID changed to command execution $(id -u)
- $FUNCNAME env changed to local variables with function names

I'm aware that the last one may be the most controversial since it is not the most elegant one, though I didn't find any better solution to retrieve the name of currently executed function in 'sh'.

Please take a look.

Regards,
Hubert

Hubert Mazur (1):
selftests/cpu-hotplug: Make the script complaint with sh shell

 tools/testing/selftests/cpu-hotplug/cpu-on-off-test.sh | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
#1 lore raw

PATCH 1/1 selftests/cpu-hotplug: Make the script complaint with sh shell

The script uses by default the bash shell that is inaccessible in the x86 embedded devices. This leads to the syntax errors. To mitigate this, change the interpreter to /bin/sh and properly assign the FUNCNAME variable for each of the functions as 'sh' doesn't set this env. Change the conditional check for being a root from not existent UID and replace with command execution "id -u".

---
 tools/testing/selftests/cpu-hotplug/cpu-on-off-test.sh | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/cpu-hotplug/cpu-on-off-test.sh +8 -2
index 6232a46ca6e1..9d5d61413443 100755
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 # SPDX-License-Identifier: GPL-2.0
 
 SYSFS=
@@ -10,7 +10,7 @@ prerequisite()
 {
 	msg="skip all tests:"
 
-	if [ $UID != 0 ]; then
+	if [ $(id -u) -ne 0 ]; then
 		echo $msg must be run as root >&2
 		exit $ksft_skip
 	fi
@@ -99,6 +99,7 @@ offline_cpu()
 
 online_cpu_expect_success()
 {
+	FUNCNAME="online_cpu_expect_success()"
 	local cpu=$1
 
 	if ! online_cpu $cpu; then
@@ -112,6 +113,8 @@ online_cpu_expect_success()
 
 online_cpu_expect_fail()
 {
+
+	FUNCNAME="online_cpu_expect_fail()"
 	local cpu=$1
 
 	if online_cpu $cpu 2> /dev/null; then
@@ -125,6 +128,8 @@ online_cpu_expect_fail()
 
 offline_cpu_expect_success()
 {
+
+	FUNCNAME="offline_cpu_expect_success()"
 	local cpu=$1
 
 	if ! offline_cpu $cpu; then
@@ -139,6 +144,7 @@ offline_cpu_expect_success()
 offline_cpu_expect_fail()
 {
 	local cpu=$1
+	FUNCNAME="offline_cpu_expect_fail()"
 
 	if offline_cpu $cpu 2> /dev/null; then
 		echo $FUNCNAME $cpu: unexpected success >&2
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
maintainer
#2 lore raw

Re: [PATCH v1 1/1] selftests/cpu-hotplug: Make the script complaint with sh shell

Quoted 2 lines · Hubert Mazur

The script uses by default the bash shell that is inaccessible in the x86 embedded devices. This leads to the syntax errors. To mitigate

some x86 embedded devices - which ones?

Quoted 4 lines

this, change the interpreter to /bin/sh and properly assign the FUNCNAME variable for each of the functions as 'sh' doesn't set this env. Change the conditional check for being a root from not existent UID and replace with command execution "id -u".

Please include errors in the change log

Quoted 6 lines
---
  tools/testing/selftests/cpu-hotplug/cpu-on-off-test.sh | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)

thanks,
-- Shuah

#3 lore raw

Re: [PATCH v1 1/1] selftests/cpu-hotplug: Make the script complaint with sh shell

Quoted 6 lines · Shuah Khan
Quoted 2 lines · Hubert Mazur

The script uses by default the bash shell that is inaccessible in the x86 embedded devices. This leads to the syntax errors. To mitigate

some x86 embedded devices - which ones?

Actually - this isn't particularly accurate since it applies to any devices which rely on busybox or other small environments.

Quoted 6 lines
Quoted 4 lines

this, change the interpreter to /bin/sh and properly assign the FUNCNAME variable for each of the functions as 'sh' doesn't set this env. Change the conditional check for being a root from not existent UID and replace with command execution "id -u".

Please include errors in the change log

The first error comes from the "$UID" environment variable, and the error is:
./cpu-on-off-test.sh[193]: [: 0: unexpected operator/operand

Regarding the changes to "$FUNCNAME" - there's no error printed but also the function name is missing in print:
"echo $FUNCNAME $cpu: unexpected success >&2"

The v1 already addresses the most urgent issues with $UID and $FUNCNAME which I faced but in the meantime I took a look at the sashiko.dev review tool:
https://sashiko.dev/#/patchset/20260318080825.3063959-2-hmazur%40google.com

and uploaded v2 patch which addresses other issues reported by the tool:
https://lore.kernel.org/all/[email protected]/

Please let me know if I should revert to v1 and update the changelog and commit message or we can continue with v2.

Regards
Hubert