I was writing a shell script to generate a string in a particular format so that it can be used as a input in one of the XML I am working with.
Given a input file in the format <attribute field>,<data_type>,<size>
instanceid,varchar,256
sysdate,date
status,number
notes,varchar,4000
created_on,date
I want to store in a variable the "check sum" like md5( INSTANCEID || STATUS || NOTES). That is I want all the attribute fields except the field having date as it's type Or'd.
The script I had written is this
IFS=$'\n'
file=$(cat source.txt)
line_number=$(cat source.txt | wc -l)
checksum="md5( "
for line in $file
do
let line_number=line_number-1
data_field=$(echo $line | cut -f1 -d','| tr "a-z" "A-Z")
data_type=$(echo $line | cut -f2 -d',' | tr "a-z" "A-Z")
if [ $data_type != "DATE" ] && [ $line_number -gt 0 ]
then checksum+="$data_field || "
elif [ $data_type != "DATE" ] && [ $line_number -eq 0 ]
then checksum+=" $data_field "
fi
done
checksum+=")"
echo $checksum
This script works fine with all the input scenarios except when the last line has a attribute with date as it's type.
In which case the variable has a value likemd5( INSTANCEID || STATUS || NOTES || )
I tried to check if the last line was a date using tail command, but this again would fail if the last few lines had it's type as date.
How can I do away with the || which appears in the end?