LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Korn shell script (https://www.linuxquestions.org/questions/linux-newbie-8/korn-shell-script-689813/)

mmahulo 12-11-2008 07:28 AM

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

colucix 12-11-2008 08:01 AM

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.

mmahulo 12-15-2008 01:23 AM

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

jstephens84 12-15-2008 08:26 AM

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.

jstephens84 12-15-2008 08:28 AM

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.

colucix 12-15-2008 12:50 PM

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

mmahulo 12-17-2008 07:09 AM

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!!

colucix 12-17-2008 07:43 AM

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


jstephens84 12-17-2008 08:19 AM

Quote:

Originally Posted by mmahulo (Post 3378491)
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.

GazL 12-17-2008 09:19 AM

Quote:

Originally Posted by mmahulo (Post 3378491)
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.


All times are GMT -5. The time now is 05:35 AM.