1

Here is my code: I want to create a temporary variable in a temporary directory. I create a function called read-series which reads integers until ctrl-d and then appends them to .tmp. Then it passes to even-odd which sums the product of evens and sum of odds. Reduce is then called to output the value. More or less. I'm new to Bash so please be clear on answers.

#!/bin/bash

TMPDIR=${HOME}/tmpdir

function readSeries () {
    while read -p "Enter an Integer: " number ; do
        echo $number
    done
    return 0;
} >> $$.tmp

function even-odd () {
    # unsure of how to reference TMPDIR
    while read $TMPDIR ; do
        evenp=$(($1 % 2))
        if [ $evenp -eq 0 ] ; then    # if 0 number is even
            return 0
        else                          # if 1 number is odd
            return 1
        fi
    done
}

function reduce () {
    # function to take sum of odds and product of evens
    # from lab 5 prompt
    even-odd $input
    cat $TMPDIR/$$.tmp | reduce
}

read-series

cat $TMPDIR/$$.tmp | reduce
1
  • you'll have to be clear in your code if you want clear answers. Where is read-series defined for instance (that is on the 2nd to last line of your script), and calling reduce inside the reduce function will lead to problems. Learning to understand the output of adding set -x near the top of your code will be a big help for debugging your own code. Good luck. Commented Oct 1, 2013 at 3:24

1 Answer 1

2

I think that will do for you

#!/bin/bash

TMPDIR=${HOME}/tmpdir

function readSeries () {
    while read -p "Enter an Integer: " number ; do
        #
        # Using Regular Expression to ensure that value is Integer
        # If value is not integer then we return out from function
        # and won't promt again to enter
        #
        if ! [[ "$number" =~ ^[0-9]+$ ]] ; 
            then return 0;
        fi
        echo $number
    done
    return 0;
} >> $$.tmp

#function evenOdd () {
    # don't need that
#}

function reduce () {
    # function to take sum of odds and product of evens
    sumOfOdds=0;
    productOfEvens=0;

    # 
    # When a shell function is on the receiving end of a pipe, 
    # standard input is read by the first command executed inside the function. 
    # USE `read` to pull that data into function 
    # Syntax :  read variable_you_want_to_name  
    #
    while read data; do
        echo "    line by line data from tmp file "$data;
        rem=$(($data % 2))
        if [ $rem -eq 0 ] ; then    # if 0; number is even
            productOfEvens=$(($productOfEvens * $data));
        else                          # if 1; number is odd
            sumOfOdds=$(($sumOfOdds + $data));
        fi
    done

    echo " Sum of Odds    :  "$sumOfOdds;
    echo " ProductOfEvens :  "$productOfEvens;
}

readSeries

#cat $TMPDIR/$$.tmp
cat $TMPDIR/$$.tmp | reduce

OR if want more specific answer here on SO you have to be clear in your code as @shellter point out.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.