Homework of week >07 (Submission deadline: 23:59, Oct. 21, 2025)
-
Write a shell script to calculate the molecular weight of the input chemical formula from
$1.
Your script must be stand-alone, that is, independent of any database file such as ~jsyu/atom.3.
Example input strings: C6H12O6 , C1H3Br1, CH3Br, C2H5O1H1 , C2H4BrOCl ......
Notes:
Pay attention to the logic to distinguish between H and He; B and Br; C and S and Cs and Sc, ....etc.
-
Bonus question:
- Re-write your homework 1 of the previous week using numeric and/or associative array.
- Use python or C language to call the regurlar expression library to calculate the molecular weight of the input chemical formula.
These are optional.
References that might be useful:
- How did you fill the associative array from the table of animal sounds according to the file
~jsyu/animal.txt at Ukko?
Ans:
unset -v a ; a=( $(sed -e 's/[ \t]\{1,\}/ /g' ~jsyu/animal.txt |head -1) )
unset -v b ; b=( $(sed -e 's/[ \t]\{1,\}/ /g' ~jsyu/animal.txt |tail -1) )
unset -v sound ; declare -A sound ; for ((i=0 ; i<${#a[@]} ; i++)) ; do sound+=( ["${a[$i]}"]="${b[$i]}" ) ; done
- Variables in
bash
- string manipulation in
bash