Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. SUM() {
  2. echo "The sum is $(($1+$2+$3+$4+$5+$6+$7+$8+$9))"
  3. }
  4.  
  5. SUM 3 8
  6. bash: 3+8+++++++: syntax error: operand expected (error token is "+")
  7.  
  8. sum() {
  9. [[ -n $2 ]] && echo $(( $(tr ' ' '+' <<<"$@") ))
  10. }
  11.  
  12. $ sum 1 2 3
  13. 6
  14.  
  15. sum() {
  16. [[ -n $2 ]] && (IFS=+; echo $(( $* )))
  17. }
  18.  
  19. SUM() {
  20. echo "The sum is $(($1+$2+${3:-0}+${4:-0}+${5:-0}+${6:-0}+${7:-0}+${8:-0}+${9:-0}))" || false
  21. }
  22.  
  23. SUM() {
  24. echo "The sum is $(($1+$2+${3:=0}+${4:=0}+${5:=0}+${6:=0}+${7:=0}+${8:=0}+${9:=0}))" || false
  25. }
  26.  
  27. sum () {
  28. local total=0;
  29. while [ $# -gt 0 ]; do
  30. total=$(($total + $1))
  31. shift
  32. done
  33. echo $total
  34. }
  35.  
  36. sum () {
  37. test $1 && echo $(( $1 + $(shift; sum $@) )) || echo 0
  38. }
  39.  
  40. SUM () {
  41. [ $# -lt "2" ] && return 1
  42. for par in $@; do
  43. local sum=`expr $sum + $par`
  44. done
  45. echo $sum
  46. return 0
  47. }
  48.  
  49. SUM 3 4 5
  50. SUM 3 4 5 1 1 1 1 2 3 4 5
  51.  
  52. sum(){
  53. t=0;
  54. for i in "$@"; do t=$((t + i )); done
  55. echo $t;
  56. }
  57.  
  58. sum(){
  59. echo "$@" | perl -lane '$s+=$_ for @F; print $s'
  60. }
  61.  
  62. sum(){
  63. echo "$@" | awk '{for(i=1; i<=NF; i++){k+=$i} print k}'
  64. }
  65.  
  66. SUM() {
  67. echo "The sum is $(($1+$2+$[$3]+$[$4]+$[$5]+$[$6]+$[$7]+$[$8]+$[$9]))"
  68. }
  69.  
  70. $ SUM 4 6 5
  71. The sum is 15
  72.  
  73. SUM() {
  74. echo "The sum is $((${1:-0}+${2:-0}+${3:-0}+${4:-0}+${5:-0}+${6:-0}+${7:-0}+${8:-0}+${9:-0}))"
  75. }
  76.  
  77. ${parameter:-word}
  78. Use Default Values. If parameter is unset or null, the expansion
  79. of word is substituted. Otherwise, the value of parameter is
  80. substituted.
  81.  
  82. $ SUM
  83.  
  84. $ SUM 1 2
  85.  
  86. $ SUM 1 1 1 1 1 1 1 1 1
  87.  
  88. SUM() {
  89. echo -e ${@/%/\n} | awk '{s+=$1} END {print "The sum is " s}'
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement