LinuxQuestions.org
Register a domain and help support LQ
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 12-11-2008, 08:28 AM   #1
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Korn shell script


Hi guys

Please help me with the following:

Write a script to do the following: Print ““Hello World! – date and time” every second to the screen until stopped. Linux SuSE

Last edited by mmahulo; 12-11-2008 at 08:45 AM.
 
Old 12-11-2008, 09:01 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
It sounds as homework. You cannot expect that someone does it for you, but you can at least show us what have you tried so far and what the problem (or the difficulty) is.
 
Old 12-15-2008, 02:23 AM   #3
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
OK

here is what i did already. all i need now is a code that can help me achieve the per-second loop thing:

DATE=$date to capture the results of the date function after it executed
echo 'Hello World!' $DATE to print the message Hello World and the date on the same line.

Just the per-second loop please. this loop must be continuous and only stop when i press Ctrl+c.

Thanks
 
Old 12-15-2008, 09:26 AM   #4
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
Look into the while statement. This will loop until a certain condition is met. and the date looks wrong. I think you will want DATE=`date`. The loop will need the following Echo "Hello World " $DATE. This should leave you with trying to figure out the loop.
 
Old 12-15-2008, 09:28 AM   #5
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
Oh and here is a hint for your while loop that you might want to use. Not the cleanest but will work. What is the largest Number in our math system.
 
Old 12-15-2008, 01:50 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Yes, as jstephens84 already pointed out, you have to use command substitution either using back ticks or the bash syntax $(command). The former will be used when compatibility with the older Bourne Shell /bin/sh is requested, the former will be used since it has some advanced features as for nesting multiple commands.

For the loop part, take in mind that you have to do an endless loop since it can be interrupted by Ctrl-C, so that you have to choose a condition always true or see if the shell provides a tool (command) to satisfy this condition (a command that does essentially... nothing, but whose exit code is 0).
 
Old 12-17-2008, 08:09 AM   #7
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Thumbs up

Thanks guys for all your advices. My script now looks like:
DATE=$(date)
while true
do
echo 'Hello World' $DATE
sleep 1
DATE=$(date)
done

and it's running quite nicely and it makes me proud. Thanks again!!
 
Old 12-17-2008, 08:43 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Awesome! Well done! You can save some lines of code if you don't assign the output of the date command to a variable, but use the command substitution directly in the echo statement:
Code:
#!/bin/bash
while true
do
  echo "Hello World! $(date)"
  sleep 1
done
 
Old 12-17-2008, 09:19 AM   #9
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by mmahulo View Post
Thanks guys for all your advices. My script now looks like:
DATE=$(date)
while true
do
echo 'Hello World' $DATE
sleep 1
DATE=$(date)
done

and it's running quite nicely and it makes me proud. Thanks again!!
Nicely done. Glad to hear that it works.
 
Old 12-17-2008, 10:19 AM   #10
GazL
Senior Member
 
Registered: May 2008
Posts: 4,081
Blog Entries: 1

Rep: Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527Reputation: 1527
Quote:
Originally Posted by mmahulo View Post
Thanks guys for all your advices. My script now looks like:
DATE=$(date)
while true
do
echo 'Hello World' $DATE
sleep 1
DATE=$(date)
done

and it's running quite nicely and it makes me proud. Thanks again!!
Nice job. If this was for homework, then using indentations within the while/do/done loop to improve readability like colucix did might get you a slightly higher mark from your teacher.


Just to show you a slightly different approach, as echo like most command will itself always return a rc=0 (same as 'true') you could have done away with the need to run 'true' and coded something like:
Code:
#!/bin/ksh

while echo "Hello World $(date)"
do
  sleep 1
done
or (if it doesn't matter whether the pause comes before or after the echo).

Code:
#!/bin/ksh

while sleep 1
do
  echo "hello World $DATE"
done
or even,

Code:
#!/bin/ksh

alias hell_freezes_over=false

until hell_freezes_over
do
  echo "Hello World $(Date)"
  sleep 1
done

That last one was mostly a joke, but it does show what you can do to make things more readable. At the end of the day, as long as the code does what its meant to the rest is all a matter of preferred style.

Hope you found this interesting.

Gaz.

Last edited by GazL; 12-17-2008 at 10:21 AM.
 
  


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
script in korn shell disruptive Programming 15 02-22-2007 11:18 AM
Korn shell and Arrays jpc82 Programming 1 06-28-2005 03:27 AM
Using a Korn shell in rc scripts desbyleo Solaris / OpenSolaris 3 11-12-2004 06:27 PM
unix korn shell script that interact with windows java program ann124 Programming 5 11-09-2004 10:14 PM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 01:47 PM


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