LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newbie needs help with script (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-needs-help-with-script-852381/)

RavhisingRickRude 12-25-2010 06:23 PM

Newbie needs help with script
 
Hello Everyone,

Im a linux newbie and Im trying to create a very basic script that will echo a question like "What is your name?" but i would like for the question not to echo the whole sentence at once but word by word or even character by character, in other words to give the illusion that the computer is actually typing the question.I hope this makes sense. I played around with the sleep command, but it will echo each word in a different line rather than the same line. Is there a way to do this? Thank

- RRR

barriehie 12-25-2010 06:53 PM

Code:

echo -n
Will echo without the newline. Linux has 'manpages'; you can go
Code:

man command
to find info about a particular command. Failing knowing the command you can go:
Code:

man -k keyword
to find all the manpages containing the keyword.

HTH

speck 12-25-2010 07:35 PM

You could do this with an Expect script. There should already an Expect script named "beer.exp" installed on your computer (if Expect is installed) that will show you an example of how to do it.

frankbell 12-25-2010 08:31 PM

Beginner's Guide to BASH Scripting:

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/

SharpyWarpy 12-25-2010 10:28 PM

Try this:
echo -n "Hey"; sleep 1; echo -n " man,"; sleep 1; echo -n " what's"; sleep 1; echo -n ' up?'; sleep 1
all on one line. And try this:
echo -n "H"; sleep .5; echo -n e; sleep .5; echo -n y; sleep .5; echo -n " man,"; sleep 1; echo -n " what's"; sleep 1; echo -n ' up?'
The quotes around " man," and " what's" ensure the punctuation marks aren't enterpreted as something else by bash. And the single quotes around ' up?' negates the ? so bash doesn't interpret it as a built-in variable bash uses in certain cases, like returning error codes. You'll learn neat little things like that as you go along, take the advice of the other posters and read the manual for echo and check out the bash scripting guide suggested by the other post. There are guides you can download for offline reading that you can upload to an ipad or similar document reader. That way when you're on a bus or waiting somewhere you can study it. Bash is not as powerful as a legitimate programming language but it sure is handy for saving you a lot of time on otherwise time consuming, mundane tasks like renaming multiple files, tagging your mp3 files, etc. Have fun.

grail 12-26-2010 02:36 AM

How about:
Code:

#!/bin/bash

line="What is your name?"

for word in $line
do
    echo -n "$word "
    sleep 2
done

read name

echo "Your name is $name"


colucix 12-26-2010 03:12 AM

Or:
Code:

line="What is your name?"

for i in $(seq 0 ${#line})
do
  echo -n "${line:$i:1}"
  sleep 0.25
done



All times are GMT -5. The time now is 03:24 PM.