LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-04-2008, 03:28 AM   #1
enigma87
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Rep: Reputation: 0
Simple shell script problem


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---"

Last edited by enigma87; 10-04-2008 at 03:31 AM.
 
Old 10-04-2008, 04:16 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The problem is that your white space gets lost in :

Code:
    number=$(echo $text | wc -c)
and is not counted by wc, so your string length is short. Use:

Code:
    number=$(echo "$text" | wc -c)
to quote the text so as not to loose your white space in your string.
 
Old 10-04-2008, 04:27 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
An alternative:
Code:
number=${#text}
endnumber=$[ $number - 1 ]
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.
 
Old 10-04-2008, 12:26 PM   #4
enigma87
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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!
 
Old 10-05-2008, 07:47 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
BTW, you can't sleep less than 1 second: http://www.ss64.com/bash/sleep.html
 
Old 10-05-2008, 08:14 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
Try the posted app - it works.
 
Old 10-06-2008, 04:31 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Indeed, from the sleep man page
Quote:
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.
 
Old 10-06-2008, 08:40 AM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
You don't need a recent kernel to use fractional times with sleep. Perhaps a POSIX sleep can't do this.
 
Old 10-06-2008, 08:48 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I stand corrected.
 
  


Reply



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
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM
Simple shell script mikz Linux - General 1 02-24-2005 07:18 AM
Simple shell script problem? Corallis Linux - Newbie 3 03-21-2004 11:25 PM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 PM.

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
Open Source Consulting | Domain Registration