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.
I'm having a problem with a shell script I've been writing this past hour. I'm trying to make a function that will simulate typing. The problem is that for small strings it does it really well, but not for longer strings. If anyone can take a look at my code and make suggestions as to how to make it as robust as possible, I'd be a million times grateful Thanks guys!
Code:
### Start of typing function ###
echoType(){
text=$*
startnumber=0
number=$(echo $text | wc -c)
endnumber=$[ $number - 2 ]
#echo ${text:1:1}
#echo $text
#echo $endnumber
while [ $startnumber -le $endnumber ]; do
echo -e "${text:$startnumber:1}\c"
startnumber=$[ $startnumber + 1 ]
sleep .05
done
echo
}
### End of typing function ###
echoType " IDENTIFICATION NOT RECOGNIZED BY THE SYSTEM"
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echoType " ---CONNECTION TERMINATED---"
the first gives the length of the string, including spaces. The second is modified, since the string does not contain the newline character appended by the echo command.
Thanks Mr. C, I dont know why I didnt try putting quotes around the $text. Also thanks colucix, learned another way of counting characters without needing to use wc Thanks guys!
Hey chrism01... actually, sleep can accept values < 1 second.
Code:
$ cat sleep
#!/bin/bash
sleep $1
$ strace -f ./sleep 2 2>&1 | grep nanosleep
[pid 4609] nanosleep({2, 0}, NULL) = 0
$ strace -f ./sleep .24 2>&1 | grep nanosleep
[pid 4613] nanosleep({0, 240000000}, NULL) = 0
man nanosleep
Name
nanosleep - pause execution for a specified time
Synopsis
#define _POSIX_C_SOURCE 199309 #include <time.h>
int nanosleep(const struct timespec *req, struct timespec *rem);
Description
nanosleep() delays the execution of the program for at least the time specified in *req. The function can return earlier if a signal has been delivered to the process. In this case, it returns -1, sets errno to EINTR, and writes the remaining time into the structure pointed to by rem unless rem is NULL. The value of *rem can then be used to call nanosleep() again and complete the specified pause.
The structure timespec is used to specify intervals of time with nanosecond precision. It is specified in <time.h> and has the form
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
The value of the nanoseconds field must be in the range 0 to 999999999.
Unlike most implementations that require NUMBER be an integer, here NUMBER
may be an arbitrary floating point number.
I think this new capability is tied to High Resolution Timers, which are supported
by the most recent kernels. See this post for some information about checking for
High Resolution Timers support into the installed kernel.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.