Custom script output in unix Custom script output in unix unix unix

Custom script output in unix


You can just construct the output string on the fly, with something like:

#!/bin/bash((sum = 0))                      # I like spacing expressions :-)text=""                          # Initial textprefix=""                        #   and term prefix.for i in "$@" ; do               # Handle each term    ((sum = sum + i))            #   by accumulating    text="${text}${prefix}${i}"  #   and adding to expression.    prefix=" + "                 # Adjust term prefix to suit.  doneecho "${text} = ${sum}"          # Output expression and result.

This can be seen in action thusly:

pax> ./sum_for 5 6 725 + 6 + 72 = 83

And, if you wanted to make it relatively robust to bad input data, you could try:

#!/bin/bash((sum = 0))text=""prefix=""for i in "$@" ; do    if [[ $i =~ ^-?[0-9]+$ ]] ; then        ((sum = sum + i))        text="${text}${prefix}${i}"        prefix=" + "    else        echo "WARNING, ignoring non-integer '${i}'."    fidoneecho "${text} = ${sum}"

Showing a sample run of that with various invalid data items:

pax> ./some_for 5 6 72 x 6x x6 -3 +4 --4 ++7 -+3 +-4 "" + - 4WARNING, ignoring non-integer 'x'.WARNING, ignoring non-integer '6x'.WARNING, ignoring non-integer 'x6'.WARNING, ignoring non-integer '+4'.WARNING, ignoring non-integer '--4'.WARNING, ignoring non-integer '++7'.WARNING, ignoring non-integer '-+3'.WARNING, ignoring non-integer '+-4'.WARNING, ignoring non-integer ''.WARNING, ignoring non-integer '+'.WARNING, ignoring non-integer '-'.5 + 6 + 72 + -3 + 4 = 84

That disallows some values that you may wish to allow but I've opted for only numbers in their simplest form, like 4 instead of +4, or --4, or even ++++----+--+4 :-)

Changing what's permitted is a (relatively) simple matter of changing the regular expression filter.


You can pass all the shell arguments ,as a variable in awk and get the sum for all of them in its BEGIN section.

cat code.shvar="$@"awk -v value="$var" 'BEGIN{z=split(value,a," ");for(i=1;i<=z;i++){s+=a[i]};print s}'


Using printf you can do something like

#!/bin/bashsum=0for ((i=1; i <= $#; i++))do    ((sum += ${!i}))                                  # sum with indirection    printf '%*.*s%d' 0 $(( i==1 ? 0 : 3)) ' + ' ${!i} # use variable widthdoneprintf ' = %d\n' $sum