Advertisement
POdkovyrkinDaniil

Untitled

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