Linux - NewbieThis 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
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.
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
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
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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.