LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Printf and a sort of "null character"? Possible? (https://www.linuxquestions.org/questions/programming-9/printf-and-a-sort-of-null-character-possible-547718/)

BlueSpirit 04-20-2007 06:40 PM

Printf and a sort of "null character"? Possible?
 
Hello all,

I will give you the "bugged" of one of my scripts...

Its an issue with "printf".
I modified it a bit (replaced variables by words, etc.) to show only the problem...

Code:

printf "%5s" hello65
printf "\b\b\b\b\b"
printf "%15s" world77
printf "\n"

The result is that it only shows "world77", not "hello65".
But I would like it to show BOTH "hello65" and "world77".

I found it was not working b/c of the "%15s", because its like if I write characters "reserved" for the "world77" expression, ereasing the "hello65" word.

Then, I tried this (but I was 99.9% sure it would not work, b/c spaces are considered like characters...):

Code:

printf "%5s" hello65
printf "\b\b\b\b\b"
printf "              world77"
printf "\n"

Again, not working, b/c spaces replaces the "hello65".

Now, what I would like to know is a way to write (with printf or something else) a sort of character which is equivalent of 1 space, but that DOES NOT erease the "hello65"...

Any ideas guys?

[Note that hello65 and world77 are just things I have replaced... B/c a script constitued of ONLY this would be annoying...]


Thank you,

Sincerely & Regards,

BlueSpirit

tuxdev 04-20-2007 07:44 PM

Um, this smells kind of like a homework assignment to me...

nadroj 04-20-2007 09:56 PM

ive never used it, but, i imagine using the ncurses library would be what you want.

edit: why do you want do to this again?

Dark_Helmet 04-20-2007 10:43 PM

Are you sure the spaces are your problem? I don't think you've looked at the man page for printf (man printf) - with particular attention to what '\b' means. In other words, your script would do well not to display the '\b' if you want to display the two strings.

Otherwise ncurses (or other special terminal control characters) would be necessary.

BlueSpirit 04-21-2007 01:04 PM

No...

I know \b is backspace...

But \b erases nothing...

Dark_Helmet 04-21-2007 02:10 PM

Quote:

But \b erases nothing...
But '\b' moves the cursor to the left by one space. Meaning anything you print after that replaces any existing text. The \b sequence is your problem -- not the spaces. If you want to show both text values, the '\b's shouldn't be displayed to begin with.

You'll need to avoid displaying them or use either ncurses or terminal control sequences. Neither ncurses or the control sequences are exceptionally nice. That is, with ncurses, you'd be using a sledgehammer to kill a fly. With terminal control sequences, you are limiting portability to one (or a handful) of terminals that support the code(s) you decide to use.

A page on VT100 escape sequences: here--with particular attention to 'ESC[nC'--which simply counteracts one '\b' that, again, shouldn't be displayed in the first place.

BlueSpirit 04-27-2007 09:50 AM

No.

Cause I need the \b in this script or it won't work...
Believe me, its not that...

But I found the \t (tab) won't erase nothing and do like a space.
So i'm currently doing a script whichv will "exploit" the tab to do a "space" character.

In the script, all you need is to specify the number of characters before tab_spacechar() is executed and the desired number of spaces.

But I'll check out ncurses too...

And, what are these VT100 esc sequences?
How can I use it???

theNbomr 04-28-2007 02:06 PM

Quote:

Originally Posted by BlueSpirit
No.

Cause I need the \b in this script or it won't work...
Believe me, its not that...

Dude, you are the guy who hasn't yet figured out what arrays are. Why do you think anyone should, on blind faith, believe you?

Your code snippet does the following:

printf "%5s" hello65

Print a character string at least 5 characters in length, padded with spaces if necessary, and then leave the cursor at the first character cell after the last character.

printf "\b\b\b\b\b"

Move the cursor back 5 character cells, erasing the contents of the each cell, and replacing them with 'space' characters. Note that the '\b' backspace character is both a cursor control and active character.


printf " world77"

Print the string " world77", and leave the cursor at the end of said string.
Note that the leading space characters are not 'empty' or 'transparent'. They are characters in their own right, and occupy their own character cell like any other

printf "\n"

Go to the next line. On some terminals, this includes an implied 'carriage return', to go to the beginning of the next line.


Your program erases the original string not once, but twice. You need to learn about cursor control to accomplish what you want.

BTW, it is helpful to the readers, and to yourself, by extension, if you don't leave it up to them to figure out what language you are talking about.

--- rod.

osor 04-28-2007 02:44 PM

You’re almost right here…
Quote:

Originally Posted by theNbomr
Move the cursor back 5 character cells, erasing the contents of the each cell, and replacing them with 'space' characters.

The backspace character (ascii 0x08) does not erase the contents of each cell — for that, you use del/rubout/erase. If you don’t believe me, try this:
Code:

printf hello65
sleep 5
printf "\b\b\b\b\b"
sleep 5
printf "\n"

You will see the cursor move to the first “l”. Of course, you can set your terminal to use backspace as rubout, but this is not the default.

That said, it still means whatever is written next (unless it is “\n”) will overwrite the following text. The OP does not need to use \b to control as s/he can use terminal escape sequences to position the cursor as s/he sees fit.

BlueSpirit 04-30-2007 08:32 PM

But guys...

All you said. I already know it...
Why saying it?

I have always known this...

I did a script which exploited tab as a space whithout earsing everything. You don't know the context of the program. I NEED this \b or it WONT work fine.

Here an example near my program context :

Code:

VALUE1                                VALUE2
Its in a while loop.

When VALUE1 is finsihed being written, VALUE2 will be writed.
When VALUE2 is finished being written, VALUE1 will be re-writed, but at the same place than VALUE1 the first time. Same thing for VALUE2. Figure this out...


Now, what I would like to know is : How can I use the ESC sequences?

After that, I will shut up.

echo -e "esc_sequence" ?

B/c I would like to know how to use these sequences, b/c even if it works with my function which uses tab to print a character equivalent to a space whithout replacing others, I would just like to see the possibilities with ESC sequences.

Dark_helmet said to see the ESC seq there. But, when I'm trying using it (im almost sure its with echo -e), it won't work. For example, when I did echo -e "\e[nChello\e[0m", stdout was "Chello".

Thank you,

Cordially,

Blue Spirit

Dark_Helmet 05-01-2007 12:00 AM

I'm not trying to be difficult, but I get the impression this is a homework assignment. I say that because of two reasons:
  1. You have not posted the rest of the script to show us why the '\b' sequence is necessary; nor have you given a reason aside from "it won't work without it" - which is questionable because there is always more than one way to perform a given task.
  2. Having separate echo statements to display text (with one moving back in preparation to overwrite the original) doesn't make sense - it feels too much like an academic exercise to give the student a problem to solve. If it were a script being developed on your own, the script would likely perform some steps to build the string (concatenation, deletion, etc.) and then use only one echo to display it.
But that's just my opinion. The forum rules do not allow asking homework questions, and I'm of the opinion that this seems like a homework assignment. So, I won't post an answer. If someone else thinks differently, they are free to post the answer if they like.

The most I will offer is this: you are close. Do some Google searching. I'm sure you'll be able to find an explanation, or perhaps an example, of how to display that particular escape sequence.

chrism01 05-01-2007 02:26 AM

I think possibly what the OP is after (and badly explained I'll agree) is that he wants to be able to re-write the same piece of screen estate over and over... see his value1, value2 example.
In that case, he wants CR without LF ...
Of course, I could be wrong... ;)

bigearsbilly 05-01-2007 03:02 AM

for example:

printf "\033c\033[2;2fHELLO\n\n"


you can work out the rest I am sure...


http://www.termsys.demon.co.uk/vtansi.htm

theNbomr 05-01-2007 10:28 AM

To give the original poster some ideas to base his own research upon, 'escape sequences' are sets of characters, often prefixed with the 'esc' character, that specific terminals trap within the stream of data that they are processing. These strings are not printed, per se, but are used to modify how the rest of the data is rendered. They contain instructions to the terminal. As such, each terminal type has it's own set of escape sequences that define what its capabilities are, and what 'language' is used to convey the instructions to the terminal. Conceptually, this is similar, if not identical, to the differences between printers and their many escape-code languages. In terminals, the features that are controlled using escape sequences includes cursor control, font & character set control, character attribute control (eg reverse video, bold, underline, etc), printer management, and a few other capabilities. The exact features of any particular terminal type are documented by the manufacturer, typically in an owners manual.

Several facilities to manage the diversity of terminal types have become commonplace in Unixish OS's. the termcap database, along with the $TERM shell variable, and the 'curses/ncurses' libraries. In order to make use of the abilities of a particular terminal type, one can use these facilities, and embed escape sequences into a stream of data being written to a terminal. The ncurses library can be used to consolidate the job of dealing with the diverse numbers and capabilities in the universe of terminal types, and relieves the programmer from having specific terminal types hard coded into an application.


--- rod.

BlueSpirit 05-01-2007 06:13 PM

Well...

We have two ppl who thinks It is an homework assignment...
Duh. Not really... For the simple reason I only have 13 years old, so I'm still at high school I don't have time to learn C intensively b/c i'm in a private school which gives me lot of homeworksn + I.S.P. ...

Two -> This is NOT a C program, only a BASH shell, and b/c BASH is an easy language, im not sure they give hws at university for that... C is NOT very difficult too, as soon as I will buy a book...


Well, now some ppl won't stop asking this thing so I will answer them : I want to make something that will act LIKE a loading...

Here is the complete model :

Quote:

< 100% > || [**************************************]
Value1 was the 100%
Value2 is the * , b/c at each new percent, it adds a *

I use sleep 0.25 between each +1 in the loading.

Now some will probably ask this dumb question, so I will answer it now :

Q: Why making a program acting like a loading? It's useless...

A: I want to do this because:

1) Before, I was not able to figure out how we could do that (for example in the WGET program). So I decided to stop worrying about it and make one in BASH. I did it, it is easy, but I used non-standard methods, like using TAB char to do a SPACE char.

2) B/c it could be used later in REAL programs that I will make...

3) B/c I love watching a loading... (huh.)


Ok. I answered it... Now, think what you want, but it is not an hw assignment (w/e I cant figure why you thought this, this homework would be too much easy...)



I did a lil search on ESC sequences and for this it is OK. No problem. But I will still use my tabspacecharf() function, b/c as I read, ESC sequences are not universal...

I tested my current version with different terminals and it works fine for all.
Even tested successfully on MAC OS X.


All times are GMT -5. The time now is 11:25 AM.