Jump to content

BASH test


Recommended Posts

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)

 

:wall:

Edited by willisoften
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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 :screwy: sorry :P

Edited by aru
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...