# UNIX or Linux code

**URL:** <https://forums.speedlife.net/t/unix-or-linux-code/41951>\
**Category:** NYSpeed Off Topic\
**Created:** [January 16, 2008, 3:45pm UTC](https://forums.speedlife.net/t/unix-or-linux-code/41951 "2008-01-16T15:45:31Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![boardjnky4](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@boardjnky4](https://forums.speedlife.net/u/boardjnky4)\
**Post date:** [January 16, 2008, 9:33pm UTC](https://forums.speedlife.net/t/unix-or-linux-code/41951/21 "2008-01-16T21:33:23Z")

</div>

> [@ILCisDEAD](#):
>
> lol I could do it in C++ and Perl… not shell script though ☹

its just different syntax, same ideas behind the code

---

<div class="post-metadata">

**Author:** ![Matt\_Danger](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/matt_danger/32/4981_2.png) [@Matt\_Danger](https://forums.speedlife.net/u/Matt_Danger)\
**Post date:** [January 16, 2008, 9:49pm UTC](https://forums.speedlife.net/t/unix-or-linux-code/41951/22 "2008-01-16T21:49:54Z")

</div>

My code’s at work but it’s similar to boardjnky’s except I used a while for the loop and read to read the numbers in. For some of the questions I used a case (aka switch) statement instead of embedded ifs

I sent them to the OP, if he wants to post them up feel free.

---

<div class="post-metadata">

**Author:** ![Matt\_Danger](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/matt_danger/32/4981_2.png) [@Matt\_Danger](https://forums.speedlife.net/u/Matt_Danger)\
**Post date:** [January 17, 2008, 12:37pm UTC](https://forums.speedlife.net/t/unix-or-linux-code/41951/23 "2008-01-17T12:37:21Z")

</div>

```auto
#!/bin/ksh
# Question 1
COUNT=0
while read NUM; do
        # Set initials
	if [$COUNT -eq 0]; then
		HIGHEST=$NUM
		LOWEST=$NUM
		SUM=$NUM
	fi
	# If 999 is entered, break out
	if [$NUM -eq 999]; then
		break
	else
		# See if inputted number is the highest number
		if [$NUM -gt $HIGHEST]; then
			HIGHEST=$NUM
		fi

		# See if inputted number is the lowest number
		if [$NUM -lt $LOWEST]; then
			LOWEST=$NUM;
		fi
		# Calculate sum
		SUM=$(($SUM+$NUM))
	fi
	COUNT=$(($COUNT+1))
done
AVERAGE=$(($SUM/$COUNT))
echo "Highest number: $HIGHEST"
echo "Lowest number: $LOWEST"
echo "Sum of the numbers: $SUM"
echo "Average of the numbers: $AVERAGE"

```

```auto
#!/bin/ksh
# Question 2
RAN=$((RANDOM%60))
COUNTER=1
echo -n "I've guessed a number between 0-60, what is it? "
while read GUESS; do
	if [$GUESS -eq $RAN]; then
		break;
	else
		echo
		if [$GUESS -lt $RAN]; then
			echo -n "Nope, try a higher number: "
		else
			echo -n "Nope, try a lower number: "
		fi	
	fi
	COUNTER=$((COUNTER+1))
done
echo; echo "Yep, good job! It took you $COUNTER tries to guess correctly."

```

```auto
#!/bin/ksh
# Question 3
echo -n "Enter a number: "; read NUM
echo -n "Enter 1 to count up to $NUM and 2 to count down to 0: "; read CHOICE
case $CHOICE in
	1)	COUNTER=0
		while [$COUNTER -le $NUM]; do
			echo $COUNTER
			COUNTER=$(($COUNTER+1))
		done ;;

	2)	while [$NUM -ge 0]; do
			echo $NUM;
			NUM=$(($NUM-1))
		done ;;
esac

```

---

<div class="post-metadata">

**Author:** ![boardjnky4](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@boardjnky4](https://forums.speedlife.net/u/boardjnky4)\
**Post date:** [January 17, 2008, 1:13pm UTC](https://forums.speedlife.net/t/unix-or-linux-code/41951/24 "2008-01-17T13:13:51Z")

</div>

ohhhh, i didnt read the questions right I guess. For question 1 I inputted the numbers as arguments to the script instead of user input, oopsy

edit: good code matt danger

---

<div class="post-metadata">

**Author:** ![Dr.Stevil](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Dr.Stevil](https://forums.speedlife.net/u/Dr.Stevil)\
**Post date:** [January 17, 2008, 1:24pm UTC](https://forums.speedlife.net/t/unix-or-linux-code/41951/25 "2008-01-17T13:24:04Z")

</div>

:tup:

[Previous page](https://forums.speedlife.net/t/unix-or-linux-code/41951.md?page=1)
