Advertisement
POdkovyrkinDaniil

Untitled

Dec 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. !#/bin/bash
  2.  
  3. if [ "$1" == "-h" ] || [ "$1" == "--help" ]
  4. then
  5. echo "$0 converts values from file"
  6. exit
  7. fi
  8.  
  9. while read string
  10. do
  11. value=$( echo $string | cut -d= -f2 )
  12. name=$( echo $string | cut -d= -f1 )
  13. number=${value//[^0-9]/}
  14. type=${value//[0-9]/}
  15. factor="*1"
  16. #echo "value = $value"
  17. #echo "number = $number"
  18.  
  19. if [[ "$name" =~ 'time'|'longtime' ]] && ! [[ "$type" =~ 'h'|'d'|'s'|'min' ]]
  20. then
  21. echo $type is not correct unit $name
  22. continue
  23. fi
  24.  
  25. if [ "$name" == "distance" ] && ! [[ "$type" =~ 'mm'|'sm'|'dm'|'m'|'km' ]]
  26. then
  27. echo $type is not correct unit for $name
  28. continue
  29. fi
  30.  
  31. if [ "$name" == "weight" ] && ! [[ "$type" =~ 'mg'|'g'|'kg'|'t' ]]
  32. then
  33. echo $type is not correct unit for $name
  34.  
  35. fi
  36.  
  37. if [[ $type == "g" ]]
  38. then
  39. factor="/1000"
  40. fi
  41. if [[ $type =~ "min" ]]
  42. then
  43. factor="*60"
  44. fi
  45. if [[ $type =~ "mm" ]]
  46. then
  47. factor="/1000"
  48. fi
  49. if [[ $type =~ "sm" ]]
  50. then
  51. factor="/100"
  52. fi
  53. if [[ $type =~ "km" ]]
  54. then
  55. factor="*1000"
  56. fi
  57. if [[ $type =~ "mg" ]]
  58. then
  59. factor="/1000000"
  60. fi
  61. if [[ $type =~ "t" ]]
  62. then
  63. factor="*1000"
  64. fi
  65. if [[ $type =~ "h" ]]
  66. then
  67. factor="*3600"
  68. fi
  69. if [[ $type =~ "d" ]]
  70. then
  71. factor="*86400"
  72. fi
  73. if [[ $type =~ "dm" ]]
  74. then
  75. factor="/10"
  76. fi
  77. if [[ $type =~ "kg" ]]
  78. then
  79. factor="*1"
  80. fi
  81. echo "factor = $factor"
  82. result=$(echo "scale=10;$number${factor}" |bc | awk '{printf("%.15g", $1)}' |sed 's/^\./0\./')
  83. echo "$name=$result"
  84. echo "result = $result"
  85. echo "-------------------------------------------------------------------- "
  86. done < $1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement