Read input data within a specified period of time


 
Thread Tools Search this Thread
Operating Systems AIX Read input data within a specified period of time
# 8  
Old 12-14-2010
HI ROHON, I do have perl installed in my system.
But the requirement is such that I dont have to hit "Enter key" and I have to give "date" as user input in the format like 2010-12-15, so it has length of 10.

I think short perl code wud be fine as I am not much familiar with PERL.

Appreciate ur time,
Regards,
Raj.
# 9  
Old 12-14-2010
My perl code exactly match your requirement. It will not accept "Enter Key" as an input. Actually it will, but doesn't react on it. So, if u want to accept only 10 characters than it will be helpful. Also I have seen your previous posts, in which u were demanding a time limit. So, I have modified my perl program and storing input value in one file. After 10 seconds, I will check if file is empty than assign default value else assign entered value.

Your shell script
Code:
echo "Enter input:\c"
charReading.pl 10 &
pid=$!
sleep 10
if [ ! -f "/tmp/optFile" ] ; then
    echo "Using input value"
else
    kill -9 $pid
    echo "Using default value"    
fi

My perl code
Code:
 #! /usr/bin/perl -w
#
# Usage:
#     charReading.pl <Number of Chars to read>
#     charReading.pl 10        # In your case
#

my $maxChars = $ARGV[0];

use strict;
use FileHandle;
my $FOBJ = new FileHandle ">/tmp/optFile";

$| = 1;
my $finalString = "";

while ($maxChars != 0) {
    my $khatm = 0;
    for (1..$maxChars) {
        my $cChar;
        $cChar = getOneChar();
        if ($cChar ne "\n") {
        if ($cChar eq "\b") {
            chop($finalString);
            print "$cChar \b";
            $khatm += 2;
        } else {
            $finalString .= $cChar;
            print "$cChar";
        }
        } else {
            $khatm += 1;
        }
    }
    $maxChars = $khatm;
}

print $FOBJ "$finalString";
exit; 

BEGIN {
    use POSIX qw(:termios_h);  
    my ($term, $oterm, $echo, $noecho, $fd_stdin);
    $fd_stdin = fileno(STDIN);
    $term = POSIX::Termios->new();
    $term->getattr($fd_stdin); 
    $oterm = $term->getlflag();
    $echo = ECHO | ECHOK | ICANON;
    $noecho = $oterm & ~$echo; 

    sub cbreak {
        $term->setlflag($noecho);
        $term->setcc(VTIME, 1);
        $term->setattr($fd_stdin, TCSANOW); 
    }  
   
    sub cooked {
        $term->setlflag($oterm);
        $term->setcc(VTIME, 0);
        $term->setattr($fd_stdin, TCSANOW);  
    }    
    sub getOneChar { 
        my $key = '';
        cbreak();  
        sysread(STDIN, $key, 1); 
        cooked(); 
        return $key;  
    }  
}
END {
    cooked()
}

After this u need to validate the input as it should be in the date formate.
R0H0N
# 10  
Old 12-14-2010
Quote:
Originally Posted by rajsharma
HI ROHON, I do have perl installed in my system.
But the requirement is such that I dont have to hit "Enter key" and I have to give "date" as user input in the format like 2010-12-15, so it has length of 10.

I think short perl code wud be fine as I am not much familiar with PERL.

Appreciate ur time,
Regards,
Raj.
you can try this Smilie

Code:
 ## justdoit ##
#!/bin/ksh
trap "" 2
readit() {
case $2 in
year ) message="year[YYyy]" ;;
month ) message="month[mm]" ;;
day ) message="day[dd]" ;;
esac
printf "... %s $message ...= "  && read -n $1 $2
while [ "$(echo $(eval echo "\$$2"|sed 's/[0-9][0-9]*//g' ))" != "" ]
 do
   printf "\n%s pls input only digits!!\n" ;
   printf "... %s $message ...= " && read -n $1 $2
 done
echo ;
}
readit 4 year ; readit 2 month ; readit 2 day
if [ $? -eq 0 ] ; then echo "$year-$month-$day" ; fi

# 11  
Old 12-15-2010
Hi ROHON, thanks for the code.

Here is how I tried ur code.
I named the script as dlay.sh
Code:
#!/usr/bin/sh
echo "Enter input:\c"
/usrdir/charReading.pl 10 &
pid=$!
sleep 10
if [ ! -f "/tmp/optFile" ] ; then
    echo "Using input value"
else
    kill -9 $pid
    echo "Using default value"    
fi

and I saved perl code charReading.pl in the dir:/usrdir/

Then I run the script dlay.sh
I hope this is how You run ur script as well.

But , I get an error :
Code:
Enter input:2010-12-12kill: 5517524: 0403-003 The specified process does not exist.
Using default value

That was one way.....

##############
2nd way:
If I treat the perl code as a function, so the script and perl code are in the same program say testdelay.sh then it gives following error:
Code:
Enter input:2010-12-12
kill: 6877242: 0403-003 The specified process does not exist.
Using default value
testdelay.sh[18]: my:  not found.
testdelay.sh[20]: use:  not found.
testdelay.sh[21]: use:  not found.
testdelay.sh[22]: my:  not found.
testdelay.sh[24]: $:  not found.
testdelay.sh[24]: =:  not found.
testdelay.sh[25]: my:  not found.
testdelay.sh[27]: 0403-057 Syntax error at line 28 : `{' is not expected.

Hope its clear... and thanks again for your time.

Regards,
Raj.

Last edited by Scott; 12-15-2010 at 05:34 AM.. Reason: Please use code tags
# 12  
Old 12-15-2010
Quote:
If I treat the perl code as a function, so the script and perl code are in the same program say testdelay.sh
Do u understand what u have written? First confirm me that charReading.pl program is running perfectly. I mean execute it as below and make sure it accepts only 5 characters. After than control comes back to the prompt. Also confirm that /tmp/optFile contains exactly what u have entered.

Code:
$ charReading.pl 5
rohon
$ cat /tmp/optFile
rohon

R0H0N
# 13  
Old 12-16-2010
bsduser: perl charReading.pl 5
rohon
bsduser: cat /tmp/optFile
rohonbsduser:

That is /tmp/optFile contains "rohonbsduser:" instead of "rohon".
I am not sure...but I think ... once I do "cat /tmp/optFile" and then hit "enter", this
"bsduser:" just gets appeneded to "rohon" which is what we are expecting.


Regards,
raj.

Last edited by rajsharma; 12-16-2010 at 03:37 AM..
# 14  
Old 12-16-2010
Ok. Thats fine. charReading.pl is running fine. Now run follwing code. There was a small error in logic. Now it is correct.

Code:
#!/usr/bin/ksh
echo "Enter input:\c"
/usrdir/charReading.pl 10 &
pid=$!
sleep 10
if [ -s "/tmp/optFile" ] ; then
    echo "Using input value"
else
    kill -9 $pid
    echo "Using default value"    
fi

R0H0N
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

2. Shell Programming and Scripting

To get the Files between Time Period

All, How to get the list of files through a unix command which exists / created / updated between 8 PM to 11:59 PM from a particular location. Regards Oracle User (3 Replies)
Discussion started by: Oracle_User
3 Replies

3. Shell Programming and Scripting

Get connection count over a period of time

I used this script to get the connection to a domain in two specific minutes. I need to extend to give result over a range of minutes. The below gives total number of connections in the minutes 00:40 and 01:13 on 22nd March. egrep "22/Mar/2013:00:40|22/Mar/2013:01:13"... (1 Reply)
Discussion started by: anil510
1 Replies

4. Shell Programming and Scripting

Read user input, Encrypt the data and write to file

Hi, can some one help me how to encrypt and decrypt a file. AIM: reade user input, encrypt it and save it to file. while decryption read the encrypted file decrypt it and save the output in some variable. Example: consider we have Credentials.txt file with content username: password... (5 Replies)
Discussion started by: saichand1985
5 Replies

5. HP-UX

memory consumption over a time period

Hi, Can some one please tell me how do I generate a report of the Memory Consumption over a time period: HP-UX B.11.31 U ia64 0440531406 unlimited-user license I normally use glance to monitor memory in run time. Note: I do not have root privileges. Thanks Danish ... (5 Replies)
Discussion started by: danish0909
5 Replies

6. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

7. UNIX for Dummies Questions & Answers

Start Time and period of a PID

Hi, Below is my OS details. uname -an SunOS mymachine 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220 I need to know when was my Apache server last started. Whats is the best and most reliable way to find out not just for Apache but for any PID per say? I am... (16 Replies)
Discussion started by: mohtashims
16 Replies

8. Shell Programming and Scripting

Calculate Time Period in Scripting

Hi all, now i am writting one bash script. in that my requirement is i need to create one directory and that the directory details to be stored in one file Ex. date/time and all in one file. after that i need to delete the folder automatically exactly after 3months. between these time... (5 Replies)
Discussion started by: anishkumarv
5 Replies

9. Shell Programming and Scripting

read/writte/input data in file.

Hi, how can i do to show when i do with scrip list entris, or list specific entry, it show 2 lines when i specify the entry to search...¿? i paste the script: # Global variables film=/opt/etc/film/film.txt export film confirm() { echo -en "$@" read ans ans=`echo $ans | tr... (0 Replies)
Discussion started by: dorek
0 Replies

10. Shell Programming and Scripting

Run job for a period of time

I have a job that runs for an unspecified amount of time. I want to run this as a cron job for a specified amount of time, say 2 hours. Once the time is up, the program should be killed in the middle of execution. How can I do this? Thanks. (5 Replies)
Discussion started by: cooldude
5 Replies
Login or Register to Ask a Question