Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Good morning everyone..
It's a wierd question, but I am curious about it.
Let's say I have a script
Code:
for i in $file
do
echo "Command is working..."
<command>
done
You can see, I have used 3 dots in echo. So in a shell script is it possible to fill such an effect that until the command works, the 3 dots keep blinking? So it make an effect that some processing is going on. Although we used to see such effects in GUIs, but not in terminals. But can we do this?
Any opinion is most welcome!!
As long as <command> doesn't produce any output, you can do some basic "animation" with carriage return "\r". This is a special character that moves the cursor the beginning of the line, so you can then overwrite the previous contents:
Code:
blink=0
while true ; do
if ((blink)) ; then
echo -n $'\rworking...'
else
echo -n $'\rworking '
fi
sleep 0.5
((blink = !blink))
done
A more common thing is a sort of "hourglass" type animation:
Thanks, but how can I impliment this code in my script? My test script is:
Code:
for i in $file
do
echo "Command is working...."
cmd=$(more $i | grep -w "20/Oct/2012" | wc -l)
echo $cmd
done
Suppose erroelogs i.e. $i is very large log file and command is taking 4 or 5 minutes to accomplish it, then in this period it want to insert that animation with my echo statement "Command is working....". I tried to add your code but failed...
Could you show me the way by modifying my script? Thanks a lot again!
#!/bin/bash
blinker() {
blink=0
while true ; do
if ((blink)) ; then
echo -n $'\rworking...'
else
echo -n $'\rworking '
fi
sleep 0.5
((blink = !blink))
done
}
for i in $file
do
blinker &
blinker_pid=$!
# piping from more was redundant, and grep's -c counts matching
# lines so wc is not needed. The -F tells grep the pattern is just
# a plain string, which may be more efficient to search for than a
# regex.
cmd=$(grep -F -c -w "20/Oct/2012" "$i")
kill $blinker_pid
echo $cmd
done
i heard them referred to as spinners. this is my favorite spinner design:
Code:
frames=('\__' '_|_' '__/' '_|_')
Hmmm.. it's not so impressive! but what @ntubski has suggested is a really good interpretation of running process i.e frames=('|' '/' '-' '\').
Is there any way that same thing can be done with same type of symbols? I mean somthing like this: .(wait some mili seconds).(wait).(wait)... Then again first .(wait) and so on...
Hmmm.. it's not so impressive! but what @ntubski has suggested is a really good interpretation of running process i.e frames=('|' '/' '-' '\').
Is there any way that same thing can be done with same type of symbols? I mean somthing like this: .(wait some mili seconds).(wait).(wait)... Then again first .(wait) and so on...
#!/bin/bash
## Blinker function
blinker ()
{
frames=('. ' '.. ' '... ' '....')
frame=0
while true ; do
echo -n $'\r'"Calculating count ${frames[frame]}"
sleep 0.2
((frame = (frame + 1) % ${#frames[@]}))
done
}
## Script
blinker &
blinker_pid=$!
cmd=`grep -F -c -w "20/10/2012" /tmp/largefile.txt` ## largefile.txt is a large text file, so command is taking 2 or 3 minutes to finish it.
echo "Total counts is: $cmd"
kill $blinker_pid
It's working ok and result a correct value but,
Problem 1# In output of the script, it gives me an error,
Quote:
"scriptname...1234 terminated blinker"
Which I want to avoid. So how to stop the function normally, instead of killing it's PID at the end.
Problem 2# If I want to stop the script in between processing using CTRL+C, it's not working and function is keep going on. One way is to kill script's PID, which I don't want. So what's a safe way to stop the script if I want to?
Last edited by colucix; 10-23-2012 at 03:02 AM.
Reason: Removed redirection to another thread (threads have been merged here)
Try this.. What the trap command does is it traps the SIGINT signal and passes control to the control_c function.
Code:
#!/bin/bash
## Blinker function
blinker ()
{
frames=('. ' '.. ' '... ' '....')
frame=0
while true ; do
echo -n $'\r'"Calculating count ${frames[frame]}"
sleep 0.2
((frame = (frame + 1) % ${#frames[@]}))
done
}
control_c()
# run if user hits control-c
{
kill $blinker_pid 2>/dev/null
echo -en "\n*** Why did you hit ctrl-c? I was grepping a large file! ***\n"
exit 1
}
# trap keyboard interrupt (control-c)
trap control_c SIGINT
## Script
blinker &
blinker_pid=$!
cmd=`grep -F -c -w "20/10/2012" /tmp/largefile.txt` ## largefile.txt is a large
text file, so command is taking 2 or 3 minutes to finish it.
echo "Total counts is: $cmd"
kill $blinker_pid 2>/dev/null
I don't know why the redirect to the /dev/null didn't work for you. The redirect to the /dev/null is meant to send the output to a black hole and never be seen again.
The "terminated" message doesn't come directly from the kill command but from the shell when it sees a child process that exited due to a signal. You need to insert this line at the top of the blinker() function:
Code:
trap "exit 0" TERM
Now the function will exit with a zero return code, and you will not get any message.
The "terminated" message doesn't come directly from the kill command but from the shell when it sees a child process that exited due to a signal. You need to insert this line at the top of the blinker() function:
Code:
trap "exit 0" TERM
Now the function will exit with a zero return code, and you will not get any message.
I used it, but making no use. My script is working OK without this line. Could you elaborate it little more, please? What difference it can make in above mentioned script (by Raj)?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.