Sponsored Content
Top Forums Shell Programming and Scripting A puzzle with a printing function executing in background Post 302488013 by klease on Friday 14th of January 2011 02:34:29 PM
Old 01-14-2011
A puzzle with a printing function executing in background

Somebody on a thread in the (french) Mandriva Forum recently suggested a script, designed to provide a tool to display kind of "temporisation widgets" on the console (to be ultimately pasted in other more complex scripts).
One version of this script was something like the following, which seems to work fine (it displays a spinning cross and it end with just ONE ENTER keystroke) :

(sorry for not having translated comments from french)
Code:
    #!/bin/bash

    # Arrêt par création de fichier
    # Version:1.0.2c
    # Au choix: croix tournante ou barre d'avancement;
    # inverser les commentaires pour sélectionner la boucle du
    # mode choisi. Pour arrêter et revenir à l'invite : taper ENTREE.
    #
    #
    clear;echo

    #+++++++++++++++++++++++++++++++++++++++
    # partie à insérer dans un script (supprimer la boucle superflue)

      PID=$$ # PID du processus père

        trap "rm -f $STOP; exit"  HUP  INT  QUIT  ABRT  TERM

    # Affiche la barre d'avancement (ici 5 motifs/s)
      function barre()
      {
        tput civis;tput bold;tput sc;x=0
        
        # Displays 5 sharps/seconds
       #--------------------------
       # COLS=$(tput cols) # Mémorise la largeur du terminal
       # while : ; do
       #    sleep 0.2
       #    tput rc;tput hpa $x;tput el
       #    echo -n "#"
       #    let "x += 1"
       #    if [ $x = $COLS ]; then tput rc;tput dl1;x=0;fi
       #    if [ -f "$STOP" ]; then break;fi
       #    if [ $((x % 5)) = 0 ]; then # si Ctrl-c a tué le père, arrête le fils
       #        if [ "$(ps h $PID)" = "" ]; then break;fi
       #    fi
       # done
        #--------------------------

        # Displays spinning cross
        #---------------------------------------
           while : ; do
               sleep 0.125;tput rc;tput dl1;echo -n "      |"
               sleep 0.125;tput rc;tput dl1;echo -n "      /"
               sleep 0.125;tput rc;tput dl1;echo -n "      -"
               sleep 0.125;tput rc;tput dl1;echo -n "      \\"
               sleep 0.125;tput rc;tput dl1;echo -n "      |"
               sleep 0.125;tput rc;tput dl1;echo -n "      /"
               sleep 0.125;tput rc;tput dl1;echo -n "      -"
               sleep 0.125;tput rc;tput dl1;echo -n "      \\"
               if [ -f $STOP ]; then break;fi
               if [ "$(ps h $PID)" = "" ]; then break;fi
            done
            #---------------------------------------
        
        rm -f $STOP
        tput rc;tput dl1;tput sgr0;tput cnorm;tput ed
      }

    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # fin de la fonction pour un script

    # Lancement de la fonction barre
      barre &
    # Instructions d'arrêt de la fonction
      read
      STOP=$(mktemp  --suffix=.stop-barre --tmpdir=$HOME/tmp)
      tput rc;tput dl1
      sleep 1       # faire une pause pour revenir en avant-plan
                     # avant d'envoyer une commande d'avant plan.
      exit 0

The problem is now for another version of this script, which is exactly the same with just some commented and uncommented lines, and which displays a line of sharps (#) (five # / second).

It works but - for some reason - it ends and returns to shell invite with TWO ENTER keystrokes.

Code:
    #!/bin/bash

    # Arrêt par création de fichier
    # Version:1.0.2c
    # Au choix: croix tournante ou barre d'avancement;
    # inverser les commentaires pour sélectionner la boucle du
    # mode choisi. Pour arrêter et revenir à l'invite : taper ENTREE.
    #
    #
    clear;echo

    #+++++++++++++++++++++++++++++++++++++++
    # partie à insérer dans un script (supprimer la boucle superflue)

      PID=$$ # PID du processus père

        trap "rm -f $STOP; exit"  HUP  INT  QUIT  ABRT  TERM

    # Displays a bar (with sharps) or a cross
      function barre()
      {
        tput civis;tput bold;tput sc;x=0

        #--------------------------
        COLS=$(tput cols)    # Mémorise la largeur du terminal
        while : ; do
           sleep 0.2
           tput rc;tput hpa $x;tput el
           echo -n "#"
           let "x += 1"
           if [ $x = $COLS ]; then tput rc;tput dl1;x=0;fi
           if [ -f "$STOP" ]; then break;fi
           if [ $((x % 5)) = 0 ]; then # si Ctrl-c a tué le père, arrête le fils
               if [ "$(ps h $PID)" = "" ]; then break;fi
           fi
        done
        #--------------------------

        # Displays spinning cross
        #---------------------------------------
        #   while : ; do
        #      sleep 0.125;tput rc;tput dl1;echo -n "      |"
        #      sleep 0.125;tput rc;tput dl1;echo -n "      /"
        #      sleep 0.125;tput rc;tput dl1;echo -n "      -"
        #      sleep 0.125;tput rc;tput dl1;echo -n "      \\"
        #      sleep 0.125;tput rc;tput dl1;echo -n "      |"
        #      sleep 0.125;tput rc;tput dl1;echo -n "      /"
        #      sleep 0.125;tput rc;tput dl1;echo -n "      -"
        #      sleep 0.125;tput rc;tput dl1;echo -n "      \\"
        #      if [ -f $STOP ]; then break;fi
        #      if [ "$(ps h $PID)" = "" ]; then break;fi
        #   done
            #---------------------------------------
        
        rm -f $STOP
        tput rc;tput dl1;tput sgr0;tput cnorm;tput ed
      }

    #++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # fin de la fonction pour un script

    # Lancement de la fonction barre
      barre &
    # Instructions d'arrêt de la fonction
      read
      STOP=$(mktemp  --suffix=.stop-barre --tmpdir=$HOME/tmp)
      tput rc;tput dl1
      sleep 1       # faire une pause pour revenir en avant-plan
                     # avant d'envoyer une commande d'avant plan.
      exit 0

Why such different behaviours ? And how to get the second version to end and go back to the shell prompt with just ONE (ENTER) keystroke.

---------- Post updated at 08:34 PM ---------- Previous update was at 08:28 PM ----------

For the french-speaking of you see the discussion about some aspects of this script :
Code:
http://forum.mandriva.com/fr/viewtopic.php?p=840962#p840962


Last edited by klease; 01-15-2011 at 04:08 AM..
 

9 More Discussions You Might Find Interesting

1. Programming

want to run a function in background

consider the given prg. main() { ..... function1(); /* to write into a file or log */ printf(" "); ..... } when the control reaches function1(), it should get executed in the background.At the same time main's printf(" ") statement should also get executed.i.e... (5 Replies)
Discussion started by: bankpro
5 Replies

2. Shell Programming and Scripting

A question about the PID of a background function

Dear all, I'm writing a KornShell script that calls inside it a function in background mode #!/bin/ksh function myfunction { . . .} myfunction |& . . . How can I capture the PID of the function myfunction that runs in background? Thanks in advance :) (2 Replies)
Discussion started by: dariyoosh
2 Replies

3. UNIX for Dummies Questions & Answers

Executing a sequence of commands as a single background process

I'm writing a PHP script which will take a given media file and convert it into a flash (flv) file. In addition to this, once the flv file has been generated I create two thumbnails (of different sizes) from the newly generated flv file. In order to do this I'm calling ffmpeg from the command... (4 Replies)
Discussion started by: phatphug
4 Replies

4. Shell Programming and Scripting

printing messages on the screen when background process is going on!

hello all, In my script i have written some messages which should come in screen while some background process is going on.. Like for example: if i want to add 2 numbers a and b ...when the addition is going on in the background i should get "ADDING TWO NUMBERS>>PLEASE WAIT " message on... (3 Replies)
Discussion started by: smarty86
3 Replies

5. AIX

Command executing to be in the background !

Guys I'm working to make in AIX script and I have some commands need to be excited by that script Like the below commands ... startsrc -s sshd I want that executing to be in the background of the system I do not like to see the out put of that (3 Replies)
Discussion started by: Mr.AIX
3 Replies

6. UNIX for Advanced & Expert Users

stopped(sigttou) error while executing a script in background

Hi All, I have an issue where in when i execute a script in the background using '&', it exits with stopped(SIGTTOU) signal. There are two servers, where the Compute server is HP-Unix and Data server is Linux. I tried using the "stty - tostop" command to disable the SIGTTOU for background... (1 Reply)
Discussion started by: vmenon
1 Replies

7. Shell Programming and Scripting

Execute a function in background and then suspend it

Here is some back ground on the script. The script is to poll an arbitrary number of DB's. To do this I am creating a function that takes the file_path to the DB and the min poll interval as arguments. The function will be called for each DB and then ran in the background. The function I was... (6 Replies)
Discussion started by: ryandavison
6 Replies

8. Shell Programming and Scripting

Executing a background script using ssh keys

Greetings, i've been working with a user-friendly menu on ksh to allow users execute scripts located on a remote server, so they wont have to login and manually launch those scripts every single time. This is a HP-UX box and currently on a /usr/bin/ksh shell. I've setup ssh keys on both... (1 Reply)
Discussion started by: nbriozzo
1 Replies

9. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
All times are GMT -4. The time now is 05:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy