Chapter 4: Shell Scripting
Activity 10: PGN Game Extractor Enhancement
The solutions are provided in five script files, pgn_extract_act1.sh
to pgn_extract_act5.sh
. Each script contains the changes made in the previous problem steps, so pgn_extract_act5.sh
has all the issues that were mentioned previously addressed. The following are the possible solutions:
- The following code can work:
function count_moves() { IFS='.' local moves_arr=( $moves ) local second_last=${moves_arr[@]: -2 : 1} IFS=' ' local second_last_arr=( $second_last ) num_moves=${second_last_arr[@]: -1 : 1} }
- Detecting blank lines can be done by testing for an empty string, such as the following:
[[ -z $line ]] && return 0
- Instead of expressions such as
count=$(( count + 1 ))
, we can write(( count ++ ))
. We can also change the[[ ]]
based conditionals, which test numeric variables such as[[ $should_print...