LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 06-12-2008, 01:25 AM   #1
JuanJose
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Rep: Reputation: 0
Question Read and Timeout Problem bash


Hello every one!
I have a little problem here, iam trying to make a bash script like this for example:

modules=`cat module`
while true
do
while read -t 3 $modules
do
read line
echo "the value of the line is $line"
done
echo "the value of modules is $modules"
done

Well what iam trying to do is make an action on modules every 3 seconds, and also taking a line from command line, if i run this, i can wait 3 seconds and i can work on modules for example making the echo "the value of modules is $modules", if i write something then i have to write 2 times becouse somehow i loose the information like this

hello!! #what iam writing
Hi! #what iam writing
the value of the line is Hi! # the result of the script

I really don know how to deal with this, I thought to use another script with sleep and bg or something else, but i need to do it with read and time out, I tryed using $* instead of read line, and dont work i dont receive nothing... I want to take the value of line at once and not typing 2 times to get an action on line

If u can give me a hand with this i will appreciate it very much,
Thanks for your time reading

Last edited by JuanJose; 06-12-2008 at 01:27 AM.
 
Old 06-12-2008, 01:57 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Your script does not really read the content of modules, since you're not passing any input to the while loop. You can do this using process substitution, for example
Code:
   while read -t 3 modules
   do
      echo "the value of modules is $modules"
      read line
      echo "the value of the line is $line"
   done < <(cat module)
Note that the argument of the read command is the name of the variable to which assign a value read from standard input, not the value to read. Also the -t option is a timeout not a sleeping time. After 3 seconds without receiving input the command exits with failure. Use sleep instead.

Last edited by colucix; 06-12-2008 at 03:31 PM. Reason: corrected syntax
 
Old 06-12-2008, 03:00 PM   #3
JuanJose
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks Colucix for the answer, iam new in bash scripting, and all this is for a wrapper (a shell over another shell) the problem is that sleep is not what i want, i need to do an update of my modules when the user is doing nothing after a few seconds just make the update. And the first script i posted, the big problem is that the user has to type 2 times (this is something that has to do with the read and timeout as u said).
Iam going to check it out
 
Old 06-12-2008, 03:26 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The user has to type two times because you have two read statements that accept input from terminal. If you want to prompt the user for each module listed in the file "module", line by line, you have to pass input as suggested (using process substitution).

I edited my previous message to correct the while read statement (stripped out the $ sign from $modules, since it is a variable name - not a value). Also I don't see the necessity for the embedding while true cycle.

Last edited by colucix; 06-12-2008 at 03:28 PM.
 
Old 06-12-2008, 03:40 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Sorry. I re-read the code and noticed that the input to the while read statement is taken also as input for the second read statement, and the user is never prompted. You can try something like this (if I don't misunderstand your requirement):
Code:
for modules in $(cat module)
do
  echo "the value of modules is $modules"
  read -t 3 line
  echo "the value of the line is $line"
done
 
Old 06-12-2008, 04:30 PM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Please use the more efficient "$(< module)" construct to read the file rather than execing cat unnecessarily.
 
Old 06-12-2008, 05:57 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by eddiebaby1023 View Post
Please use the more efficient "$(< module)" construct to read the file rather than execing cat unnecessarily.
This is a very poorly documented feature of command substitution in bash.
 
Old 06-12-2008, 07:10 PM   #8
JuanJose
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Hey colucix thanks for helping, you r right about iam reading 2 times, and after thinking a while i realise on how to fix the problem. Here it go:

Module=`cat modules`
while true
do
line=
read -t 3 line
if [ -z "line" ]
then
echo "the value of Modules is $Module"
continue
fi
echo "the value of line is $line"
done

this way i can work on line and check the module every 3 seconds when the user is doing nothing.
I thank u very much for ur help, becouse of this posted I could think better how to fix the problem.

Last edited by JuanJose; 06-12-2008 at 07:12 PM.
 
  


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
Bash 'read' problem raypen Programming 5 03-25-2006 04:35 PM
ad0:READ command timeout BongMong *BSD 1 07-08-2004 11:57 PM
Timeout - read? kalleanka Programming 2 02-15-2004 02:27 PM
HTTP error: 500 read timeout precioso77 Linux - Software 0 11-17-2003 04:59 PM


All times are GMT -5. The time now is 03:03 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