Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- !#/bin/bash
- if [ "$1" == "-h" ] || [ "$1" == "--help" ]
- then
- echo "$0 converts values from file"
- exit
- fi
- while read string
- do
- value=$( echo $string | cut -d= -f2 )
- name=$( echo $string | cut -d= -f1 )
- number=${value//[^0-9]/}
- type=${value//[0-9]/}
- factor="*1"
- #echo "value = $value"
- #echo "number = $number"
- if [[ "$name" =~ 'time'|'longtime' ]] && ! [[ "$type" =~ 'h'|'d'|'s'|'min' ]]
- then
- echo $type is not correct unit $name
- continue
- fi
- if [ "$name" == "distance" ] && ! [[ "$type" =~ 'mm'|'sm'|'dm'|'m'|'km' ]]
- then
- echo $type is not correct unit for $name
- continue
- fi
- if [ "$name" == "weight" ] && ! [[ "$type" =~ 'mg'|'g'|'kg'|'t' ]]
- then
- echo $type is not correct unit for $name
- fi
- if [[ $type == "g" ]]
- then
- factor="/1000"
- fi
- if [[ $type =~ "min" ]]
- then
- factor="*60"
- fi
- if [[ $type =~ "mm" ]]
- then
- factor="/1000"
- fi
- if [[ $type =~ "sm" ]]
- then
- factor="/100"
- fi
- if [[ $type =~ "km" ]]
- then
- factor="*1000"
- fi
- if [[ $type =~ "mg" ]]
- then
- factor="/1000000"
- fi
- if [[ $type =~ "t" ]]
- then
- factor="*1000"
- fi
- if [[ $type =~ "h" ]]
- then
- factor="*3600"
- fi
- if [[ $type =~ "d" ]]
- then
- factor="*86400"
- fi
- if [[ $type =~ "dm" ]]
- then
- factor="/10"
- fi
- if [[ $type =~ "kg" ]]
- then
- factor="*1"
- fi
- echo "factor = $factor"
- result=$(echo "scale=10;$number${factor}" |bc | awk '{printf("%.15g", $1)}' |sed 's/^\./0\./')
- echo "$name=$result"
- echo "result = $result"
- echo "-------------------------------------------------------------------- "
- done < $1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement