Create a formated text file

Hi,

I have written a BASH shell script (included below) which will allow me to monitor my blood pressure. The script computes the mean of 5 user input systolic, diastolic, and heart rate values.

I would like the script to then append these three values to their respective columns in a text file. How do I do this? I would like the text file to be formated so that it can be read into EXCEL. Thanks a lot.

Mike

#!/bin/bash


###########################################################################
###########################################################################
SYSTOLIC=(120 130 125 124 122)
DIASTOLIC=(90 80 75 89 73)
HEARTRATE=(60 70 80 85 75)
###########################################################################
###########################################################################


###########################################################################
###########################################################################
#Perform an initial check:

echo; echo; echo 'Performing initial check:'

#Check that SYSTOLIC has 5 elements:
if [ ${#SYSTOLIC[@]} -eq 5 ]; then
    echo 'SYSTOLIC has 5 elements:' ${SYSTOLIC[@]}
else
    echo; echo; echo 'ERROR: SYSTOLIC does not have 5 elements'
    exit 1
fi

#Check that DIASTOLIC has 5 elements:
if [ ${#DIASTOLIC[@]} -eq 5 ]; then
    echo 'DIASTOLIC has 5 elements:' ${DIASTOLIC[@]}
else
    echo; echo; echo 'ERROR: DIASTOLIC does not have 5 elements'
    exit 1
fi

#Check that HEARTRATE has 5 elements:
if [ ${#HEARTRATE[@]} -eq 5 ]; then
    echo 'HEARTRATE has 5 elements:' ${HEARTRATE[@]}
else
    echo; echo; echo 'ERROR: HEARTRATE does not have 5 elements'
    exit 1
fi

echo 'Initial check complete'
###########################################################################
###########################################################################


###########################################################################
###########################################################################
#Compute the mean blood pressure and heart rate values:

echo; echo; echo 'Computing mean blood pressure and heart rate values:'

let MEAN_SYSTOLIC=(${SYSTOLIC[0]} + ${SYSTOLIC[1]} + ${SYSTOLIC[2]} + ${SYSTOLIC[3]} + ${SYSTOLIC[4]})/5
let MEAN_DIASTOLIC=(${DIASTOLIC[0]} + ${DIASTOLIC[1]} + ${DIASTOLIC[2]} + ${DIASTOLIC[3]} + ${DIASTOLIC[4]})/5
let MEAN_HEARTRATE=(${HEARTRATE[0]} + ${HEARTRATE[1]} + ${HEARTRATE[2]} + ${HEARTRATE[3]} + ${HEARTRATE[4]})/5

echo 'Mean Systolic:' $MEAN_SYSTOLIC
echo 'Mean Diastolic:' $MEAN_DIASTOLIC
echo 'Mean Heartrate:' $MEAN_HEARTRATE
###########################################################################
###########################################################################

you could have done this script in much more easy way..
anyways you want to read it to excel so you have to creat a .CSV file..
first type this in the command line or you can take care inside the script while running it first time by checking the file exists or not...

echo "Mean Systolic,Mean Diastolic,Mean Heartrate" > formatedfile.csv

later inside script replace your last 3 echo statments by one echo statement

echo "$MEAN_SYSTOLIC,$MEAN_DIASTOLIC,$MEAN_HEARTRATE" >>formatedfile.csv

Hi vidyadhar85,

Thanks for the reply! How could I simplify the script?

Mike

you can before that i wanna clarify some things
are you gonna hard core your input values??as you did in the above script
i mean to array??
or you will provide them as argument to script??

yes, i will reinput them every time i run the script. I won't specify them as command line parameters.

it can be done some thing like this

SYSTOLIC="120 130 125 124 122"
DIASTOLIC="90 80 75 89 73"
HEARTRATE="60 70 80 85 75"
len=`echo $SYSTOLIC|awk 'BEGIN{RS=" "}END{print NR}'`
if [ $len -eq 5 ] ; then
MEAN_SYSTOLIC=`echo $SYSTOLIC|awk 'BEGIN{RS=" "}{sum += $0}END{print sum/5}'`
else
    echo 'ERROR: SYSTOLIC does not have 5 elements'
    exit 1
fi
len=`echo $DIASTOLIC|awk 'BEGIN{RS=" "}END{print NR}'`
if [ $len -eq 5 ] ; then
MEAN_DIASTOLIC=`echo $DIASTOLIC|awk 'BEGIN{RS=" "}{sum += $0}END{print sum/5}'`
else
    echo 'ERROR: DIASTOLIC does not have 5 elements'
    exit 1
fi
len=`echo $HEARTRATE|awk 'BEGIN{RS=" "}END{print NR}'`
if [ $len -eq 5 ] ; then
MEAN_HEARTRATE=`echo $HEARTRATE|awk 'BEGIN{RS=" "}{sum += $0}END{print sum/5}'`
else
    echo 'ERROR: HEARTRATE does not have 5 elements'
    exit 1
fi
echo "$MEAN_SYSTOLIC,$MEAN_DIASTOLIC,$MEAN_HEARTRATE" >> file.csv

but since you are entering the values manually i don't think you need to check for the five elements entered or not right??
if you avoid that it became 10 line code:)