LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-30-2005, 01:51 PM   #1
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Rep: Reputation: 30
echo typing text?


Hi all,

I'm a total noob at bash scripting and right now I'm just reading various tutorials trying to get a feel for it. As an exercise I'm trying to write up a script for my kids that asks a series of questions and waits for user input before proceeding to the next question.

I've got that part down, but I was thinking that it would be cool if I could get the script to "type out" the text in the questions rather than just blip out the whole question in one shot. In other words, the questions would appear as if someone were typing them live.

I hope this is clear.

For example, rather than blip out the question "what is your name?" I'm trying to get it to appear as if someone were typing it out so it would appear like "w (pause) h (pause) a (pause) t (pause)" and so on.

Is that possible? If so, how?

Thanks,

LocoMojo
 
Old 08-30-2005, 02:01 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
Well, thought it would be slow, you could do:
Code:
echo -n H
sleep 1
echo -n e
sleep 1 
echo -n l
sleep 1
echo -n l
sleep 1
echo -n o
Not very easy, either.
 
Old 08-30-2005, 02:12 PM   #3
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Matir
Well, thought it would be slow, you could do:
Code:
echo -n H
sleep 1
echo -n e
sleep 1 
echo -n l
sleep 1
echo -n l
sleep 1
echo -n o
Not very easy, either.
Hi Matir,

Thanks for that. That is exactly what I am looking for, but like you said it wouldn't be easy. Is there an easier way? Could I type out a text file then write a script to cat that text file and write it all up like you suggested?

Could I make the typing any faster?

Thanks for your help!

LocoMojo

Edit: I found a way to make it type faster. "sleep .2"

Last edited by LocoMojo; 08-30-2005 at 02:15 PM.
 
Old 08-30-2005, 02:15 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
Unfortunately, bash's sleep is only in whole-second increments. If you'd like, I could write you a program that would print strings at a somewhat faster speed. It would be trivial, and you wouldn't have to work with C code, I promise.
 
Old 08-30-2005, 02:20 PM   #5
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Matir
Unfortunately, bash's sleep is only in whole-second increments. If you'd like, I could write you a program that would print strings at a somewhat faster speed. It would be trivial, and you wouldn't have to work with C code, I promise.
Hi Matir,

Apparently my edit of my last post and your last post crossed.

I found a way to make it type faster.

sleep .2

So I guess it isn't always in whole second increments

I appreciate your offer in writing a program for me, but that would defeat the purpose of my learning to script. C coding is way, way, way out of my league.

LocoMojo

Last edited by LocoMojo; 08-30-2005 at 02:24 PM.
 
Old 08-30-2005, 02:46 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
Ah, you are correct. GNU sleep does support such an extension.

You could try something like:
Code:
function typestring {
    STRING="${*}" # extract all arguments
    for i in `seq 0 $((${#STRING}-1))` #offset 0, length-1
        do echo -n "${STRING:$i:1}" #extract one char
        sleep .2
    done
    echo
}

typestring "hello world!"

Last edited by Matir; 08-30-2005 at 02:48 PM.
 
Old 08-30-2005, 02:58 PM   #7
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Matir
Ah, you are correct. GNU sleep does support such an extension.

You could try something like:
Code:
function typestring {
    STRING="${*}" # extract all arguments
    for i in `seq 0 $((${#STRING}-1))` #offset 0, length-1
        do echo -n "${STRING:$i:1}" #extract one char
        sleep .2
    done
    echo
}

typestring "hello world!"
Wow, that looks awful complicated, but it works well except it doesn't put a space between hello and world.

Thanks a bunch for this. Now I'll have to learn how it works

LocoMojo
 
Old 08-30-2005, 03:15 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
it worked for me with a space between hello and world. Make sure all the quotes are preserved, ESPECIALLY on the echo -n line.

I'll try to break the script down here for general education:
Code:
function typestring {
This is bash's declaration of a function.
Code:
STRING="${*}"
We place all arguments to the function in the variable $STRING
Code:
for i in `seq 0 $((${#STRING}-1))`
This is a complex line. It sets up a for loop with values from the sequence 0 .. string length -1. ${#VARIABLE} is the length of a string. $(()) does math evaluation. seq generates a sequence. Bacticks (``) perform command substitution.
Code:
do echo -n "${STRING:$i:1}"
The -n to echo prevents echo from adding a newline. ${STRING:$i:1} tells bash to extract the $i'th character from the string $STRING. The quotes allow us to echo characters like spaces.
Code:
sleep .2
Sleeps for 2/10ths of a second.
Code:
done
End the loop.
Code:
echo
This blank echo adds a newline to the end of the task.
Code:
}
This ends the definition of the typestring function.
Code:
typestring "hello world!"
This calls typestring to print out hello world!

I hope that helps some.
 
Old 08-30-2005, 08:56 PM   #9
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Matir
it worked for me with a space between hello and world. Make sure all the quotes are preserved, ESPECIALLY on the echo -n line.

I'll try to break the script down here for general education:
Code:
function typestring {
This is bash's declaration of a function.
Code:
STRING="${*}"
We place all arguments to the function in the variable $STRING
Code:
for i in `seq 0 $((${#STRING}-1))`
This is a complex line. It sets up a for loop with values from the sequence 0 .. string length -1. ${#VARIABLE} is the length of a string. $(()) does math evaluation. seq generates a sequence. Bacticks (``) perform command substitution.
Code:
do echo -n "${STRING:$i:1}"
The -n to echo prevents echo from adding a newline. ${STRING:$i:1} tells bash to extract the $i'th character from the string $STRING. The quotes allow us to echo characters like spaces.
Code:
sleep .2
Sleeps for 2/10ths of a second.
Code:
done
End the loop.
Code:
echo
This blank echo adds a newline to the end of the task.
Code:
}
This ends the definition of the typestring function.
Code:
typestring "hello world!"
This calls typestring to print out hello world!

I hope that helps some.
Hi Matir,

Funnily enough, when I copied the code from your post the quotes from:

do echo -n "${STRING:$i:1}" #extract one char

didn't come with it...strange. I fixed it and it works like a charm.

I appreciate your breaking it down like you did and explaining each bit. It makes more sense to me now. Thanks much for that!

I have one last question and then I won't bother you anymore, I promise

How do you know when to use ( ) or { } or [ ] ?

Thanks again!

LocoMojo
 
Old 08-30-2005, 10:41 PM   #10
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
$(( EXPRESSION )) is math evaluation.
$( EXPRESSION ) is command substitution (think ` `)
${ VARIABLE } is a variable reference. For just a variable, it's equivalent to $VARIABLE.
[ ] is used to access an array... I suppose. Honestly, I've never used arrays in bash.

Check out this document: http://www.tldp.org/LDP/abs/html/str...ipulation.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo multiple lines of text twistedpair Linux - Software 9 08-08-2007 07:07 PM
C++ or perl how to not display text when typing true_atlantis Programming 6 10-03-2004 01:35 AM
Can't get echo to produce two lines of text sknarf Linux - Software 1 06-21-2004 12:48 PM
typing a quotation mark in text editors cyberhawk Linux - General 2 04-16-2004 08:51 PM
Delay in echo when entering text into Firefox 0.8 Shade Linux - Software 4 04-02-2004 01:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration