Click here to Skip to main content
15,561,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a shell script that takes arguments from the command line and adds them only if they are greater than 10 I have coded it well but not able to understand why is showing the command not found error


What I have tried:

#!/bin/sh
sum=0
for i in $@
do
if [$@ -gt 10]
then
sum=$((sum+i))
else
continue
fi
done
echo $sum
Posted
Updated 10-Feb-21 3:06am

1 solution

Your if statement does not refer to the individual value, it should be $i not $@. Also the square brackets require spaces to separate them from the parameters. Try this:
Bash
#!/bin/sh
sum=0
for i in $@
do
    if [ $i -gt 10 ]
    then
        sum=$((sum+i))
    else
            continue
    fi
done
echo $sum
 
Share this answer
 
Comments
CPallini 10-Feb-21 8:09am    
5.
Richard MacCutchan 10-Feb-21 8:47am    
Thanks. I won't tell you how long it took me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900