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