Find the answer to your Linux question:
Results 1 to 9 of 9
Can any one tell me how to print in same line again and again in shell scripting example: echo "-" ------------------------- so that i can draw a line like the ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    37

    Can i print again and again in same line?

    Can any one tell me how to print in same line again and again in shell scripting
    example:
    echo "-"
    -------------------------
    so that i can draw a line like the above, i know to print like echo "--------------" but i want to do it in loop like

    while [ a -le b]
    do
    echo "-"
    done

    This command printing in next line after one loop but i want to print in same line can any one help?

    and also i saw while booting some live cd during the booting process "/" "-" "\" were used and made a circular motion when process is going on.. can any one tell how to do that by shell scripting

  2. #2
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    Use echo's -n flag to not print the trailing newline. Check the echo man page.

    Make sure you remember to just put a final echo with no arguments after the loop so you don't get _everything_ on the same line.

    As for the rotating line... I think you need to use curses to do this, so it wouldn't be easily done in a simple bash script. That's not to say it can't be done, I just don't know how without a bit of research.
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Here is some spinner code that runs until the process ID seen as the argument to the function does not exist. As provided, it runs until the sleep 10 terminates:
    Code:
    #!/bin/bash
    
    # ^H is \b (for echo, others), 010 (octal), 08 (hex)
    
    spinner(){
    PROC=$1
    while [ -d /proc/$PROC ];do
    echo -ne '/\b' ; sleep 0.05
    echo -ne '-\010' ; sleep 0.05
    echo -n '' ; sleep 0.05
    echo -n '' ; sleep 0.05
    done
    return 0
    }
    
    # du /usr >/dev/null 2>&1 &
    sleep 10 &
    # spinner $(pidof du)
    # spinner $(pidof sleep)
    spinner $!
    Figuring this out will be good practice for you ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    drl's code is excellent, though I do question these lines:
    Code:
    echo -n '' ; sleep 0.05
    echo -n '' ; sleep 0.05
    Here's basically the same approach to your original question, but using syntax that may (or may not!) be more intelligible. It also demonstrates an additional feature or two which may be of interest.
    Code:
    #!/bin/bash
    
    while true
    do
      dasher=-$dasher
      echo -n -e '\r'$dasher/
      sleep 1
      echo -n -e '\b-'
      sleep 1
      echo -n -e '\b\\'
      sleep 1
      echo -n -e '\b|'
      sleep 1
    done
    The echo command is normally not executed by the echo program, so the man page would not be quite the authority here; usually it's executed as a bash internal command, so to get help on it, do this at the command line:
    Code:
    help echo
    Hope this, um, helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi, Bill.

    It's not my code, just something I salted away when I ran across it someplace.

    I'm always happy to see improvements ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Just Joined!
    Join Date
    Nov 2008
    Posts
    37

    Thanks a lot guys.....

    Sure this helped me a lot... i learned much from you guys today... thanks guys.. but i cant understand one thing.. why we are using sleep here? i know its for slowing the process down but is there any other reason for that?


    Thaks for all....

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    why we are using sleep here?
    Just for demonstration. Without the sleep, the action would happen too fast for the demo scripts to be useful as teaching tools.

    In real life you wouldn't use the sleep, unless it served some other purpose for you.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Just Joined!
    Join Date
    Nov 2008
    Posts
    37
    Thanks... and those two exampls worked like a cake....

  9. #9
    Just Joined!
    Join Date
    Nov 2008
    Posts
    37
    #!/bin/bash

    fwd ()
    {
    a=0
    while [ $a -le 20 ]
    do
    d=-$d
    echo -ne "\r$d"
    sleep 0.05
    echo -ne "\b-"
    sleep 0.05
    echo -ne "\b\\"
    sleep 0.05
    echo -ne "\b|"
    sleep 0.05
    echo -ne "\b/"
    a=$(( $a + 1 ))
    done
    echo -ne " "
    }
    rev()
    {
    while [ 1 -le $a ]
    do
    echo -ne "\b"
    sleep 0.05
    echo -ne "\b-"
    sleep 0.05
    echo -ne "\b/"
    sleep 0.05
    echo -ne "\b|"
    sleep 0.05
    echo -ne "\b\\"
    sleep 0.05
    echo -ne "\b "
    a=$(( $a - 1 ))
    done
    echo -ne "\r"
    }

    fwd
    rev


    #Guys this is the simple thing i done from your help..... Thanks....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...