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 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 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 |
Um, this smells kind of like a homework assignment to me...
|
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? |
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. |
No...
I know \b is backspace... But \b erases nothing... |
Quote:
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. |
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??? |
Quote:
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. |
You’re almost right here…
Quote:
Code:
printf hello65 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. |
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 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 |
I'm not trying to be difficult, but I get the impression this is a homework assignment. I say that because of two reasons:
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. |
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... ;) |
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 |
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. |
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:
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. |