Script producing error, Program to calculate maximum number

Hi folks,

Here i have written a shell script to calculate a maximum number from 10 numbers entered on command line.

max=0
echo Enter 10 numbers , one at a time
for i in 1 2 3 4 5 6 7 8 9 10
do
read n
max=`expr $max + $n`
if[$n -gt max] 

--- At this last step there is some problem, it gives error message , what is the problem?

there should be space between if & [.

if[$n -gt max]

change it to ..

if [$n -gt max]

Add spaces after [ and before ]

if [ $n -gt max ]

1.you have not mentioned $symbol for max
2. No space between if and [

 
max=0
echo Enter 10 numbers , one at a time
for i in 1 2 3 4 5 6 7 8 9 10
do
read n
max=`expr $max + $n`
if[ $n -gt $max ]

Hey , Thank you everyone for detecting the error. :slight_smile: :b:

As, you have seen this code, this runs for only numbers from 1 to 10 ,and accordingly takes input from the user .

I want to run this program for n command line arguments. So I have done this :-

max=0
echo Enter the numbers from which max no. is to be chosen
for i in $* 

// Here, in the end, the problem is that $* stands for No. of command line line arguments , how do I read n command line arguments and work accordingly ? :confused:

Try with $# =)

#!/bin/sh

echo $#

exit 0
./script a b d f e c abcdef
7