ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...):
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
Last edited by BlueSpirit; 04-20-2007 at 08:03 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.
Last edited by Dark_Helmet; 04-20-2007 at 10:47 PM.
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.
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???
Last edited by BlueSpirit; 04-27-2007 at 09:58 AM.
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.
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.
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".
I'm not trying to be difficult, but I get the impression this is a homework assignment. I say that because of two reasons:
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.
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.
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...
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.
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...
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.
Last edited by BlueSpirit; 05-01-2007 at 06:35 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.