Reputation: 61
I was wondering how to get dynamic output in a shell. I mean the kind of output you get with "top" command or the status bar for wget downloads: the command line output changes without any newline.
my specific needs are to get a feedback from a script (which takes a lot to be executed with a lot of operations) without getting a newline for every single operation feedback for example a discovery script which send pings in a very large network and can dynamically tell the status of the discovery.
(no I cannot use nmap :D anyway it is for study purpose)
thanks!
Upvotes: 4
Views: 3825
Reputation: 139
You can use cursor movement with bash. For cursor movements refer to http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
If say you want to print dates dynamically, you can have something like this:
echo -ne '\033[s'; clear ; for i in `seq 10`;do echo -ne '\033[0;0H' ; date;sleep 1;done ;echo -ne '\033[u'
Upvotes: 1
Reputation: 189327
The "dynamic" display is just a sequence of print statements. Some of those print statements print screen control codes which move the cursor or repaint parts of the screen. The de facto standard framework for this is ncurses
. For access from within a shell script, you want tput
.
Here is a simple "animated" spinner.
#!/bin/sh
# Restore cursor and move to new line when terminated
trap 'tput cnorm; echo' EXIT
trap 'exit 127' HUP INT TERM
# Make text cursor invisible
tput civis
# Save cursor position
tput sc
while true; do
for char in '-' '\' '|' '/'; do
# Back to saved position
tput rc
printf "%s" "$char"
sleep 1
done
done
Upvotes: 3