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 ...
- 12-04-2008 #1Just 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
- 12-04-2008 #2
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
- 12-04-2008 #3Linux Engineer
- 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:
Figuring this out will be good practice for you ... cheers, drlCode:#!/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 $!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 )
- 12-04-2008 #4
drl's code is excellent, though I do question these lines:
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:echo -n '' ; sleep 0.05 echo -n '' ; sleep 0.05
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:#!/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
Hope this, um, helps.Code:help echo
--
Bill
Old age and treachery will overcome youth and skill.
- 12-04-2008 #5Linux Engineer
- 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, drlWelcome - 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 )
- 12-04-2008 #6Just 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....
- 12-04-2008 #7Just for demonstration. Without the sleep, the action would happen too fast for the demo scripts to be useful as teaching tools.Code:
why we are using sleep here?
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.
- 12-04-2008 #8Just Joined!
- Join Date
- Nov 2008
- Posts
- 37
Thanks... and those two exampls worked like a cake....
- 12-06-2008 #9Just 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....


Reply With Quote