Advertisement
Guest User

Elements Script by /u/xereeto

a guest
Oct 27th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.70 KB | None | 0 0
  1. #!/bin/bash
  2. #######################
  3. ## Element Script by ##
  4. ## /u/xereeto. for   ##
  5. ## best results put  ##
  6. ## this script in    ##
  7. ## your PATH & name  ##
  8. ## it "element".     ##
  9. #######################
  10. #prints usage message if element is not found
  11. if [ -z $1 ]; then
  12.     #yes I could have used basename, but I didn't because fuck the system
  13.     S0=${0##*/}
  14.     echo "Usage: "
  15.     echo \"$S0 <element name>\""
  16.     echo \"$S0 -n <atomic number>\""
  17.     echo \"$S0 -s <atomic symbol>\""
  18.     exit 1
  19. else
  20.     S1="${1,,}"
  21. fi
  22. #chassive element (the chemical kind) array. warning.
  23. element[1]="hydrogen:H"
  24. element[2]="helium:He"
  25. element[3]="lithium:Li"
  26. element[4]="beryllium:Be"
  27. element[5]="boron:B"
  28. element[6]="carbon:C"
  29. element[7]="nitrogen:N"
  30. element[8]="oxygen:O"
  31. element[9]="fluorine:F"
  32. element[10]="neon:Ne"
  33. element[11]="sodium:Na"
  34. element[12]="magnesium:Mg"
  35. element[13]="aluminium:Al"
  36. element[14]="silicon:Si"
  37. element[15]="phosphorus:P"
  38. element[16]="sulfur:S"
  39. element[17]="chlorine:Cl"
  40. element[18]="argon:Ar"
  41. element[19]="potassium:K"
  42. element[20]="calcium:Ca"
  43. element[21]="scandium:Sc"
  44. element[22]="titanium:Ti"
  45. element[23]="vanadium:V"
  46. element[24]="chromium:Cr"
  47. element[25]="manganese:Mn"
  48. element[26]="iron:Fe"
  49. element[27]="cobalt:Co"
  50. element[28]="nickel:Ni"
  51. element[29]="copper:Cu"
  52. element[30]="zinc:Zn"
  53. element[31]="gallium:Ga"
  54. element[32]="germanium:Ge"
  55. element[33]="arsenic:As"
  56. element[34]="selenium:Se"
  57. element[35]="bromine:Br"
  58. element[36]="krypton:Kr"
  59. element[37]="rubidium:Rb"
  60. element[38]="strontium:Sr"
  61. element[39]="yttrium:Y"
  62. element[40]="zirconium:Zr"
  63. element[41]="niobium:Nb"
  64. element[42]="molybdenum:Mo"
  65. element[43]="technetium:Tc"
  66. element[44]="ruthenium:Ru"
  67. element[45]="rhodium:Rh"
  68. element[46]="palladium:Pd"
  69. element[47]="silver:Ag"
  70. element[48]="cadmium:Cd"
  71. element[49]="indium:In"
  72. element[50]="tin:Sn"
  73. element[51]="antimony:Sb"
  74. element[52]="tellurium:Te"
  75. element[53]="iodine:I"
  76. element[54]="xenon:Xe"
  77. element[55]="caesium:Cs"
  78. element[56]="barium:Ba"
  79. element[57]="lanthanum:La"
  80. element[58]="cerium:Ce"
  81. element[59]="praseodymium:Pr"
  82. element[60]="neodymium:Nd"
  83. element[61]="promethium:Pm"
  84. element[62]="samarium:Sm"
  85. element[63]="europium:Eu"
  86. element[64]="gadolinium:Gd"
  87. element[65]="terbium:Tb"
  88. element[66]="dysprosium:Dy"
  89. element[67]="holmium:Ho"
  90. element[68]="erbium:Er"
  91. element[69]="thulium:Tm"
  92. element[70]="ytterbium:Yb"
  93. element[71]="lutetium:Lu"
  94. element[72]="hafnium:Hf"
  95. element[73]="tantalum:Ta"
  96. element[74]="tungsten:W"
  97. element[75]="rhenium:Re"
  98. element[76]="osmium:Os"
  99. element[77]="iridium:Ir"
  100. element[78]="platinum:Pt"
  101. element[79]="gold:Au"
  102. element[80]="mercury:Hg"
  103. element[81]="thallium:Tl"
  104. element[82]="lead:Pb"
  105. element[83]="bismuth:Bi"
  106. element[84]="polonium:Po"
  107. element[85]="astatine:At"
  108. element[86]="radon:Rn"
  109. element[87]="francium:Fr"
  110. element[88]="radium:Ra"
  111. element[89]="actinium:Ac"
  112. element[90]="thorium:Th"
  113. element[91]="protactinium:Pa"
  114. element[92]="uranium:U"
  115. element[93]="neptunium:Np"
  116. element[94]="plutonium:Pu"
  117. element[95]="americium:Am"
  118. element[96]="curium:Cm"
  119. element[97]="berkelium:Bk"
  120. element[98]="californium:Cf"
  121. element[99]="einsteinium:Es"
  122. element[100]="fermium:Fm"
  123. element[101]="mendelevium:Md"
  124. element[102]="nobelium:No"
  125. element[103]="lawrencium:Lr"
  126. element[104]="rutherfordium:Rf"
  127. element[105]="dubnium:Db"
  128. element[106]="seaborgium:Sg"
  129. element[107]="bohrium:Bh"
  130. element[108]="hassium:Hs"
  131. element[109]="meitnerium:Mt"
  132. element[110]="darmstadtium:Ds"
  133. element[111]="roentgenium:Rg"
  134. element[112]="copernicium:Cn"
  135. element[113]="ununtrium:Uut"
  136. element[114]="flerovium:Fl"
  137. element[115]="ununpentium:Uup"
  138. element[116]="livermorium:Lv"
  139. element[117]="ununseptium:Uus"
  140. element[118]="ununoctium:Uuo"
  141. #end of chassive array.
  142. #processes command line options
  143. if [ "$1" == "-n" ]; then
  144.     if ! [[ "$2" =~ ^[0-9]+$ ]] ; then
  145.         #if the user is an arsehole and enters a non-number
  146.         echo -e "\e[1;31mInvalid Atomic Number - NaN\e[00m"
  147.         exit 1
  148.     fi
  149.     if [ "$2" -lt "1" ]; then
  150.         #if the user thinks there is an element with 0 protons
  151.         echo -e "\e[1;31mInvalid Atomic Number - too low!\e[00m"
  152.         exit 1
  153.     elif [ "$2" -gt "118" ]; then
  154.         #if the user thinks more elements have been discarvard
  155.                 echo -e "\e[1;31mInvalid Atomic Number - too high!\e[00m"
  156.                 exit 1
  157.     fi
  158.     #because of my formatting in the array, it is easy to extract the element name
  159.     #to pass to the server later.
  160.     S1=${element[$2]%:*}
  161. elif [ "$1" == "-s" ]; then
  162.     #converts to sentence case, in case the user decides that HE==He
  163.     S2=`echo "$2" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'`
  164.     #stores the entire element array in a space-delimited variable
  165.     elarr=`echo "${element[@]}"`
  166.     #converts spaces to newlines so the list can be grepped, then greps for the
  167.     #symbol the user provided
  168.     elarr=`echo "${elarr// /$'\n'}" | grep -w "$S2"` #yes, I use backticks instead of $(). Old habits...
  169.     if [ -z "$elarr" ]; then
  170.         #if the user made a typo, mistake or just straight-up invented an element
  171.         echo -e "\e[1;31mError!\e[00m Element not found!"
  172.         exit 1
  173.     fi
  174.     #truncates the symbol so the name can be sent to the server
  175.     S1=${elarr%:*} #i thought I'd comment on naming my var S1. I was trying to directly
  176.                #modify $1, then I realized I couldn't, so instead of a long var name, S1 did fine.
  177. fi
  178. #now to actually download the element's info page.
  179. bk=$(curl -L -silent www.webelements.com/$S1)
  180. #replace bullet point tags with newlines.
  181. bk=${bk//<li>/$'\n'}
  182. #now to strip HTML tags
  183. bk=$(echo "$bk" | sed "s/<[^>]\+>//g")
  184. #from here it's just grepping for info about the element.
  185. el_name=$(echo "$bk" | grep "Name: ")
  186. el_name=${el_name:7} #where did 7 come from? answer: I counted the number of characters before the actual value
  187.              #on the web page, and that amount was 7.
  188. if [ -z "$el_name" ]; then
  189.     #if the text "Name: " is not found on the page, that means the server 404'd.
  190.     echo -e "\e[1;31mERROR: \e[00melement \"$S1\" not found."
  191.     exit 1
  192. fi
  193. el_sym=$(echo "$bk" | grep "Symbol: ")
  194. el_sym=${el_sym:9}
  195. el_num=$(echo "$bk" | grep "Atomic number: ")
  196. el_num=${el_num:16}
  197. el_mass=$(echo "$bk" | grep "Atomic weight: ")
  198. el_mass=${el_mass:16}
  199. if [ "$el_num" == "3" ]; then
  200.     #for some reason Lithium has [ ]s round its atomic mass, which is shit because only one of them
  201.     #will show up since the mass has a decimal point. To compensate I remove the left [.
  202.     el_mass=" ${el_mass:2}"
  203. fi
  204. #find where the decimal point is
  205. el_mass_index=$(expr index "$el_mass" '.')
  206. if [ $el_mass_index == 0 ]; then
  207.     #yes, I could have used if [ ! ... ]. but I didn't.
  208.     echo > /dev/null
  209. else
  210.     #none of that rounding bullshit here. Just straight up truncating.
  211.     let el_mass_index=$(expr index "$el_mass" '.')+2
  212.     el_mass=${el_mass:0:($el_mass_index)}
  213. fi
  214. #fixes a small problem with radioactive elements
  215. el_mass="${el_mass/[/ [}"
  216. el_state=$(echo "$bk" | grep "Standard state: ")
  217. el_state=${el_state:16}
  218. el_group=$(echo "$bk" | grep "Group in periodic table: ")
  219. el_group=${el_group:26}
  220. #some elements don't have a group.
  221. if [ -z "$el_group" ]; then
  222.     el_group="N/A"
  223. fi
  224. el_period=$(echo "$bk" | grep "Period in periodic table: ")
  225. el_period=${el_period:27}
  226. el_color=$(echo "$bk" | grep "Colour: ")
  227. el_color=${el_color:9}
  228. el_class=$(echo "$bk" | grep "Classification: ")
  229. el_class=${el_class:16}
  230. #formats the info in a nice neat table.
  231. echo -e "\e[1;31m"
  232. echo "$el_name"
  233. echo -e "\e[00m"
  234. (echo -e "\e[1;36mName%\e[00m $el_name"
  235. echo -e "\e[1;36mSymbol %\e[00m $el_sym"
  236. echo -e "\e[1;36mAtomic Number %\e[00m $el_num"
  237. echo -e "\e[1;36mAtomic Mass%\e[00m$el_mass"
  238. echo -e "\e[1;36mStandard State%\e[00m$el_state"
  239. echo -e "\e[1;36mGroup in Periodic Table %\e[00m $el_group"
  240. echo -e "\e[1;36mPeriod in Periodic Table%\e[00m$el_period"
  241. echo -e "\e[1;36mColor %\e[00m $el_color"
  242. echo -e "\e[1;36mClassification %\e[00m $el_class")| column -t -s "%"
  243. echo #done!!
  244. ################################
  245. ##                ##
  246. ## Hope you like this script! ##
  247. ##     Made for reddit by     ##
  248. ##         /u/xereeto!        ##
  249. ##             `  ,#          ##
  250. ##            . ,#. +         ##
  251. ##            @   @,.         ##
  252. ##            '               ##
  253. ##        `#@++@#             ##
  254. ##       +;@        @;+       ##
  255. ##    @ +          # @        ##
  256. ##    .@  `;;  ;;`  #,        ##
  257. ##     #   ;,  ,;   @         ##
  258. ##     @            @         ##
  259. ##     `.  ;    ;  ,          ##
  260. ##      .;  `''`  '`          ##
  261. ##        +#,``:@'            ##
  262. ##       :+      ',           ##
  263. ##       ++      '#           ##
  264. ##       ,+      ;:           ##
  265. ##       ';      ,#           ##
  266. ##       '`       :           ##
  267. ##        .:    '`            ##
  268. ##        ,@    @,            ##
  269. ##       #  ;  +  +           ##
  270. ##       @@@@@@@@@@           ##
  271. ##                            ##
  272. ################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement