1

I'm trying to fake show a spinner when I "need" to run a long running piece of code (ok, so, it isn't long running, but I want it to seem that it is to my end user...)

On Android 12+

So, I've got the following shell script:

#!/system/bin/sh

# the spinner function
spinner() { 
    # show the spinner
    (
        while :; do 
            for c in / - \\ \|; do 
                printf '%s\b' "$c"; 
                sleep 0.025; 
            done; 
        done
    )& 
    
    # sleep the length of time specified when calling this
    sleep $(($1-1)); 
    
    # backspace and kill the process
    { 
        printf '\b'; 
        kill $! && wait $!; 
    } 2>/dev/null 
}

# call it!
spinner 5

It shows my spinner for 5 seconds, but also displays the following: [1] 17823 before it... (which appears to be the pid of this, so that last number always changes...)

Any ideas how I can get rid if that?

Kevin
  • 2,684
  • 6
  • 35
  • 64
  • Look at [Running bash commands in the background without printing job and process ids](https://stackoverflow.com/questions/7686989/running-bash-commands-in-the-background-without-printing-job-and-process-ids). – Pierre François Sep 01 '23 at 14:46
  • 1
    @PierreFrançois The problem with that is it breaks the ability to get the PID with `$!`. – Barmar Sep 01 '23 at 15:08
  • 2
    When I run your script I don't see that message. That message is only shown in shells with job control, it shouldn't happen in a script. – Barmar Sep 01 '23 at 15:11
  • I also don't see the spinner, but that's because my terminal has an opaque cursor, and it covers up the spinner character. It would be better to do the backspace *before* printing the character instead of after. – Barmar Sep 01 '23 at 15:18
  • @PierreFrançois can you elaborate on that in an answer please? I did look through it, and and got it working with help from it. – Kevin Sep 01 '23 at 17:39
  • Are you perhaps testing this by sourcing it into an interactive shell? Don't do that (unless interactive shells are part of the "real" target environment); use a noninteractive shell so job control is off. – Charles Duffy Sep 01 '23 at 22:03
  • @CharlesDuffy I wouldn't need it in a non-interactive shell. This is for a set of tweaks for android devices that must be run in an interactive shell :) – Kevin Sep 02 '23 at 13:57

1 Answers1

1

With a couple of changes, this worked for me :

#!/bin/sh

# the spinner function
spinner() { 
    # show the spinner
    {
        while :; do
            for c in / - \\ \|; do
                printf '%s\b' "$c";
                sleep 0.025; 
            done; 
        done &
    } 2>/dev/null
    
    # sleep the length of time specified when calling this
    sleep $(($1-1));
    
    # backspace and kill the process
    { 
        printf '\b'; 
        kill $! && wait $!; 
    } 2>/dev/null
}

# call it!
spinner 5
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Perfect. Works great. One tweak to one-line it, put a `;` after the first `2>/dev/null` :) – Kevin Sep 02 '23 at 13:56