Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
05-01-2007, 07:41 PM
|
#16
|
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
And then my question (and I think the question of all those wondering why you need to revert to vt escape sequences) is: What’s wrong with something like this:
Code:
for x in `seq 1 100`; do
printf "< %3d > || [" $x
for i in `seq 1 40`; do
if [ $(($i * 100 / 40 )) -le $x ]; then
printf "*"
else
printf " "
fi
done
# printf "]\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
printf "]\r"
sleep 0.25
done
printf "\n"
As you see the row of backspaces has been replaced by a carriage return. As you can also see NO VT ESCAPE SEQUENCES.
P.S., I’m sure someone else can find a way to do all that in one line or something 
Last edited by osor; 05-01-2007 at 09:40 PM.
Reason: typo
|
|
|
|
05-01-2007, 08:36 PM
|
#17
|
|
Member
Registered: Jun 2006
Distribution: Slackware
Posts: 144
Original Poster
Rep:
|
This script is excellent too...
Only that As I said, I dont want a loop which rewrites the model each time...
Last edited by BlueSpirit; 05-01-2007 at 08:47 PM.
|
|
|
|
05-01-2007, 08:47 PM
|
#18
|
|
Member
Registered: Jun 2006
Distribution: Slackware
Posts: 144
Original Poster
Rep:
|
Okay, I have no more questions...
Last edited by BlueSpirit; 05-01-2007 at 08:53 PM.
|
|
|
|
05-01-2007, 09:10 PM
|
#19
|
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
|
Originally Posted by BlueSpirit
I dont want a loop which rewrites the model each time
|
I must have missed that… why not?
|
|
|
|
05-01-2007, 09:15 PM
|
#20
|
|
Member
Registered: Jun 2006
Distribution: Slackware
Posts: 144
Original Poster
Rep:
|
BTW too... I'm almost sure ur script won't write the last * b/c of the -lt...
Try it, see... Me I can't even paste it b/c I don't have firefox yet... But Lynx.
Last edited by BlueSpirit; 05-01-2007 at 09:17 PM.
|
|
|
|
05-01-2007, 09:40 PM
|
#21
|
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
|
Originally Posted by BlueSpirit
I'm almost sure ur script won't write the last * b/c of the -lt
|
Nice catch… changed it
|
|
|
|
05-01-2007, 10:18 PM
|
#22
|
|
Member
Registered: Mar 2007
Posts: 119
Rep:
|
>>> 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"...
I am going to assume you want a forward space.
printf "hello\b\033[1Cthere\n"
Now, I had a little trouble tracking this down, so I will put in my thinking:
printf the program is a wrapper over printf the function.
printf works with ansi.
ascii is a subset of ansi.
so \033 is the ESC (in hexadecimal) Decimal 27 in ASCII.
ESC[1C is the ANSI escape sequence for right 1 column.
so \033[1C is your forward space character.
Of course the terminal has to be ansi compliant.
If you want to be more compliant, then you need to maintain the state in some way.
so:
Code:
StrMult() {
# print a line of characters
for (( k = 0; k < ${1}; ++k ))
do
printf "%s" $2
done
}
#-----------------------------------------------------------
BS() {
# backspace number of characters
for (( k = 0; k < ${1}; ++k ))
do
printf "\b"
done
}
#-----------------------------------------------------------
Main() {
# entry point
msg="A Message "
printf "%s" "$msg"
toggle=1
for (( c = 0; c < 40; ++c ))
do
if [[ toggle -eq 1 ]]
then
StrMult $c "+"
else
StrMult $c "-"
fi
(( toggle *= -1 ))
sleep 0.1
BS $c
done
echo
}
Main
#-----------------------------------------------------------
So, that is a sort of flavour you can play with.
Last edited by Zention; 05-01-2007 at 10:25 PM.
|
|
|
|
05-01-2007, 11:48 PM
|
#23
|
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
I'm glad you got it working.
Some advice to avoid problems in getting help in the future.
Don't make the people trying to help you fight to get information from you. Why did you not come out immediately and say:
Quote:
|
Originally Posted by BlueSpirit
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% > || [**************************************]
|
|
That's a normal thing to want to do. I've tried to do that in the past. Just say what you're trying to accomplish. Not only will you get answers more quickly, you may get suggestions on how to do it better.
Also:
Quote:
|
Originally Posted by BlueSpirit
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 brought up that point earlier (in reply #6 of this thread):
Quote:
|
Originally Posted by Dark_Helmet
With terminal control sequences, you are limiting portability to one (or a handful) of terminals that support the code(s) you decide to use.
|
And lastly:
Quote:
|
Originally Posted by BlueSpirit
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...
|
Nobody has suggested a C program (from my reading of the responses). You might have been referring to the use of printf. There is a printf() function in C, yes, but there is also a printf command to execute from a command prompt. The printf command functions in much the same way as the C printf(), but because it's a command, it can be used in shell scripts.
I'm sure you probably feel like I'm being a jerk...maybe I am. But whenever I see a post from a user asking for help but the user refuses to explain why something must be the way they say, it makes me assume someone else (i.e. an instructor) has imposed that requirement.
Just be open about your question. Don't play "hide the ball" if you get a question from someone trying to help. Then you won't run into people like me that will stop short of giving you the final answer because they don't know if it's a homework assignment or not.
Last edited by Dark_Helmet; 05-01-2007 at 11:50 PM.
|
|
|
|
05-02-2007, 02:25 AM
|
#24
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
did you not try what's in my last post?
that should be all you need.
|
|
|
|
05-02-2007, 11:49 AM
|
#25
|
|
Member
Registered: Jun 2006
Distribution: Slackware
Posts: 144
Original Poster
Rep:
|
Well, I'm sorry... No, you didn't act like a jerk Dark_Hamlet, only that I thought if I would have writed the reason why I want to do this, ppl would just have said : This is totally useless to do this... Or, you are ridiculous to make a prog like that...
But I admit i was wrong...
Also, YES bigbearsbilly, that's your tutorial about ESC sequences that I read. Before, I could not figure what the hell was the ESC in ESC[nC, but I read later that it was the ascii num utilised to do different category of things.
|
|
|
|
05-02-2007, 12:37 PM
|
#26
|
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
|
Originally Posted by BlueSpirit
I dont want a loop which rewrites the model each time
|
I still don’t see what’s wrong with that.
As an aside, this method (i.e., using a carriage return rather than vt escape sequences) is used for the progress bars found in wget, e2fsck, bar, pv, Term-ProgressBar, and ruby-progressbar. Of course none of these are written in “bash”, but four of them in C, one in perl, and one in ruby. In fact, I was looking for a progress bar in a “real, live program” which did use escape sequences or ncurses but was unable to find one (NOTE, this doesn’t mean such a program doesn’t exist). The closest thing I could find was bar which uses escape sequences for ANSI colors.
|
|
|
|
05-02-2007, 05:17 PM
|
#27
|
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221
Rep:
|
Quote:
|
Originally Posted by BlueSpirit
Code:
printf "%5s" hello65
printf "\b\b\b\b\b"
printf "%15s" world77
printf "\n"
...
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"...
|
Code:
var1=hello65
var2=world77
printf "%s\r" "$var1"
## sleep 2 ## uncomment to see what happens
printf "\e[%dC%s\r" $(( 15 - ${#var2} )) "$var2"
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:56 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.
|
Latest Threads
LQ News
|
|