4

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!

2

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Yep tput and stty are great for this.
1

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.