linux - Add a display to let user wait in bash

07
2014-04
  • Olivier M.

    I am looking for a pretty stupid piece of code for bash that will show a kind of "please wait" rolling display, with the characters : - \ | /

    The first problem is that I don't know the name of this thing, so it is a bit difficult to find, and the other is could you please provide a scriptlet please ?

    Thank you in advance,

    Olivier

  • Answers
  • Teun Vink

    They're called 'spinners'. Linux Journal has an article with example code.

  • Olivier M.

    Thanks a lot for the valuable help. I had a look at all pieces of code, and they all seem pretty long for what I am doing.

    So based on that I just redid a tiny piece of code I am sharing with you.

    #! /bin/bash
    state=1
    delay=0.5
    printf "-"
    sleep $delay
    
    while [ 1 ]
    do
            printf "\b"
            case $state in
                    0) printf "-";;
                    1) printf "\\";;
                    2) printf "|";;
                    3) printf "/";;
            esac
            if [ $state -eq 3 ]
            then
                    state=0;
            else
                    state=$((state+1));
            fi
            sleep $delay
    done
    

    Of course, this can work as a standalone code, but take out the bash line, and you can insert it easily in a script, and eventually shrink it a bit more. The only thing is about the condition, which is now running forever, so have to get out your own condition for the stop.

    Hope you'll find it useful. ;-)

  • Thomas M

    They're also called 'Throbbers'. For an implementation plus explanation how to use it see http://fitnr.com/showing-a-bash-spinner.html (disclaimer: I haven't tested it myself, but I overlooked it and it looks reasonable)

  • Ranjithkumar T

    Could you try the below code for "please wait" process bar / spinners.

     #!/bin/bash
     echo -e "Please wait ..."
     while true;
     do
     echo -ne '-\r'
     sleep .2
     echo -ne '\\\r'
     sleep .2
     echo -ne '|\r'
     sleep .2
     echo -ne '/''\r'
     sleep .2
     echo -ne '-\r'
     done
    

    or

    try

     #!/bin/bash
     echo -n "Please wait..."
     while true
     do
          echo -n "."
          sleep .2
     done
    

    Its working for me...


  • Related Question

    linux - Bash & 'su' script giving an error "standard in must be a tty"
  • sHz

    Folks, I'm having an issue with a bash script which runs a particular command as a different user.

    The background: Running on a Linux box (CentOS), the script is quite simple, it's starting the hudson-ci application.

    declare -r HOME=/home/hudson
    declare -r RUNAS=hudson
    declare -r HOME=/home/hudson
    declare -r LOG=hudson.log
    declare -r PID=hudson.pid
    declare -r BINARY=hudson.war
    
    su - ${RUNAS} -c "nohup java -jar ${HOME}/${BINARY} >> ${HOME}/${LOG} 2>&1; echo $! > ${HOME}/${PID}" &
    

    This is the abridged version of the script, when run, the script exits with "standard in must be a tty". Any ideas on what I could be doing wrong? I've tried Dr Google and all the advice hasn't helped thus far.

    Other reference: Mandriva Linux 'su' bug


  • Related Answers
  • Eric3

    In your /etc/sudoers file, try commenting out the line that says "Defaults requiretty". There are security implications of doing so, so you might want to instead add this line below:

    Defaults:[username] !requiretty
    

    Be sure to use the visudo command to edit this file, rather than a regular text editor.