Computing.Net > Forums > Unix > Help with korn shell

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with korn shell

Reply to Message Icon

Name: orangepanther
Date: December 10, 2003 at 05:36:13 Pacific
OS: Unix
CPU/Ram: Pentium4 256 MB
Comment:

Hi
I'm trying to create a script that generates a random number only one number at a time. I have this
RANDOM=12342
while true
do
echo "$RANDOM"
done
That generates random numbers but an endless string of them how do I generate only one number?



Sponsored Link
Ads by Google

Response Number 1
Name: Jerry Lemieux
Date: December 10, 2003 at 06:44:27 Pacific
Reply:

Why not use

print $RANDOM

You don't need to set RANDOM, since it is a system variable. Just get rid of the loop, which is why you are getting a stream of numbers.


0

Response Number 2
Name: Stephen Moss
Date: December 14, 2003 at 01:00:37 Pacific
Reply:

If what you are looking for is a random single digit, check "parameter substitution" section of man for ksh, or find good korn shell manual. As an example use the following to generate a single random number from the first position of any number generated by $RANDOM:

#!/bin/ksh
NUM=$RANDOM
echo $NUM
case ${#NUM} in
1) echo ${NUM}
;;
2) echo ${NUM#?}
;;
3) echo ${NUM#??}
;;
4) echo ${NUM#???}
;;
5) echo ${NUM#????}
;;
*) echo "huh?"
;;
esac

NOTE: ${NUM%?} will use the last positional number rather than the first.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Help with korn shell

help with korn shell if command www.computing.net/answers/unix/help-with-korn-shell-if-command/4543.html

I need help with my skript. www.computing.net/answers/unix/i-need-help-with-my-skript/5903.html

Korn shell script help ! www.computing.net/answers/unix/korn-shell-script-help-/7240.html