Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Busybox/bash does not support string subscripting, which causes an error. #466

Open
AlfredWJ opened this issue Dec 19, 2023 · 2 comments
Open

Comments

@AlfredWJ
Copy link

In some scenarios, scripts are executed using busybox/bash in the system environment, which does not support string subscripting. When encountering string subscripting, an error will be reported.
For example,the following code will result in an error message 'syntax error: bad substitution' when executed.

    read -r line < "/proc/self/stat"
    stat=$line
    stime=${stat[21]}

Can we modify it to the following?

diff --git a/etc/rc.d/init.d/functions b/etc/rc.d/init.d/functions
index 0d973b3..b4898be 100644
--- a/etc/rc.d/init.d/functions
+++ b/etc/rc.d/init.d/functions
@@ -119,8 +119,7 @@ __kill_pids_term_kill_checkpids() {
         [ ! -e  "/proc/$pid" ] && continue
         read -r line < "/proc/$pid/stat" 2> /dev/null

-        stat=($line)
-        stime=${stat[21]}
+        stime=$(cat /proc/$pid/stat | cut -d" " -f22)

         [ -n "$stime" ] && [ "$base_stime" -lt "$stime" ] && continue
         remaining="$remaining$pid "
@@ -141,8 +140,7 @@ __kill_pids_term_kill() {

     # We can't initialize stat & base_stime on the same line where 'local'
     # keyword is, otherwise the sourcing of this file will fail for ksh...
-    stat=($(< /proc/self/stat))
-    base_stime=${stat[21]}
+    base_stime=$(cat /proc/self/stat | cut -d" " -f22)

     if [ "$1" = "-d" ]; then
         delay=$2
@lnykryn
Copy link
Member

lnykryn commented Dec 19, 2023

-    stat=($(< /proc/self/stat))
-    base_stime=${stat[21]}
+    base_stime=$(cat /proc/self/stat | cut -d" " -f22)

This is not equivalent, old code returns stime for the script while the new one returns the time for the "cat".
You probably want cat /proc/$$/stat.

@AlfredWJ
Copy link
Author

-    stat=($(< /proc/self/stat))
-    base_stime=${stat[21]}
+    base_stime=$(cat /proc/self/stat | cut -d" " -f22)

This is not equivalent, old code returns stime for the script while the new one returns the time for the "cat". You probably want cat /proc/$$/stat.

yes,you are right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants