PDA

View Full Version : need help to understand why printf doesn't work in bash script



mantazasas
August 7th, 2015, 05:35 PM
Hi all.
I'm writing my first script but i have some troubles. All i want is to nicely update my same lines without reset or clear commands.
Here's my code:


#!/bin/bash
# My first script


echo "testas";
while [ 1 ]; do sensors;printf "\033[1A";sleep 1;done;



it is very simple , but i can't understand why my printf won't work.
By idea the command should met my requirements but it don't.
Or I'm wrong here?

PaulW2U
August 7th, 2015, 05:42 PM
Thread moved to Programming Talk.

I've also changed the title to be a little more meaningful.

Lars Noodén
August 7th, 2015, 06:00 PM
Your script uses ANSI escapes (http://ascii-table.com/ansi-escape-sequences.php) to move the cursor up a single line. The output of sensors is multiple lines, so you will have to count them and replace the 1 with the right number in the printf statement.

mantazasas
August 7th, 2015, 06:05 PM
Thank you for reply but can you in which part i should replace it ?

Lars Noodén
August 7th, 2015, 06:14 PM
The 1A should be something else, like 5A or 12A or whatever, depending on the number of lines that sensors (http://manpages.ubuntu.com/manpages/trusty/en/man1/sensors.1.html) produces on your system. Run 'sensors' by itself and then count the lines and use that number.

The \033 is octal for the escape 'character' On terminals, going way back in history, an Escape followed by [ followed by some chosen characters have special functions. nA will move the cursor up n lines. So if you print "\033[3A", it will move up three lines. See the link in #3 above.

mantazasas
August 7th, 2015, 06:16 PM
Thank you for answering everything works now :)