Mini Program should be used for shell scripts
1... Sum of 100
Non-recursive:#!/ bin/bash read num count=1 sum=0 val="" ret="" while [ $count -le $num ] do if [ $count -eq $num ];then val=$count elseval=${count}'+' fi let sum+=count let count++ ret=${ret}${val} done echo ${ret}'='$sum Recursion: #!/ bin/bash read num function add(){ local val=$1 local count=0 local sum=0 local num=$val if [ $val -eq 1 ];then echo 1 return fi arr=$count1+$val let val-- let count=$( add $val ) let sum=$count+$num echo $sum } ret=$(add $num) echo $ret
Results:
[fbl@www test6_19]$ ./ add.sh
100
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100=5050
Find the minimum or maximum number in a file
#!/ bin/bash read namemax=0min=0count=0while read linedo if [ $count -eq 1 ];then min=$line fi (( count++ )) if [ $max -lt $line ];then max=$line fi if [ $min -gt $line ];then min=$line fi done