willisoften Posted March 29, 2004 Report Share Posted March 29, 2004 (edited) I had a scripting test today which went well but there was one question I didn't get Write a script Without using the sleep command which will cause the message "One hour has now elapsed!" to be written to your terminal 1 hour later. Sounds simple but I just couldn't do it. The at command which we have been using in tutorials mails a message an hour later. That's the only idea I had. Scripting guru required so I can sleep tonight. (Obsessive personality) Edited March 29, 2004 by willisoften Quote Link to comment Share on other sites More sharing options...
willisoften Posted March 29, 2004 Author Report Share Posted March 29, 2004 Thanks aru I've a felling the lecturer meant us to use the at command and that the question might have been badly worded. I can't see any way to do what he asked with the things we've learned in previous weeks - thanks for your solution though, it's good to look at new stuff as well. Quote Link to comment Share on other sites More sharing options...
aru Posted March 29, 2004 Report Share Posted March 29, 2004 Scripting guru required so I can sleep tonight. Well, there are several ways of doing this. The more purist one would be the one that uses no external commands. So ONLY using bash builtins here you are: ~$ cat > willisoften_sleep #! /bin/bash TIME_LIMIT=20 while : do if [ $SECONDS -ge $TIME_LIMIT ]; then break fi done printf "%s seconds have now elapsed!\n" $SECONDS ~$ ~$ chmod +x willisoften_sleep ~$ ./willisoften_sleep & [1] 2549 ~$ <I DO NORMAL USE OF MY TERMINAL AND 20 SECONDS LATER...> ~$ ~$ 20 seconds have now elapsed! [1]+ Done ./willisoften_sleep ~$ Change the value of $TIME_LIMIT to 3600 and you'll have your HOUR wakener Trick: see the man bash page and search for the $SECONDS environment variable Hope you'll sleep tonight :P P.S. This code uses only bash builtings, it works, but the fact that the loop is always true until the N seconds have elapsed makes it a huge CPU eater. You can't have all, that's why there is a sleep command in LINUX/UNIX ;) Quote Link to comment Share on other sites More sharing options...
aru Posted March 29, 2004 Report Share Posted March 29, 2004 (edited) Thanks aru I've a felling the lecturer meant us to use the at command and that the question might have been badly worded. I can't see any way to do what he asked with the things we've learned in previous weeks - thanks for your solution though, it's good to look at new stuff as well. You're wellcome :) Those things continously happen with bad teachers. The at command afaik implies the use of a deamon (atd) which could be installed or not. I think too that the question was bad formated PS sorry, I removed the post by mistake and reposted it back again. Now your answer is above my post sorry :P Edited March 29, 2004 by aru Quote Link to comment Share on other sites More sharing options...
willisoften Posted March 30, 2004 Author Report Share Posted March 30, 2004 Actually he's one of the best lecturer's we have - truly entertaining, interested, enthusiastic and easy to talk to. I guess you just can't be good at all things all the time. He actually teaches operating system theory, but he was timetabled for a practical slot once a week. He decided we should all get some practical OS experience with a different operating system. (RedHat) It's actually extra work for him as it's not really part of his module. We get course work credit for it too. How good can you get? I've tried the script you wrote aru and it works like a charm. Wish I could thought of it at the time! Not enough experience- more practice required. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.