From 41ffa47075020796d8cbed44c1e95500705e6037 2020-07-06 16:44:26 From: Branko Majic <branko@majic.rs> Date: 2020-07-06 16:44:26 Subject: [PATCH] Noticket: [factorio_manager.sh] Use globbing for reading list of available backup directories instead of ls with grep. --- diff --git a/games/factorio_manager.sh b/games/factorio_manager.sh index ed5b9e163976414b57bce4a893546875f08b988b..fb1397c08b4a629812a81243843238a2c7e7c26f 100755 --- a/games/factorio_manager.sh +++ b/games/factorio_manager.sh @@ -1513,41 +1513,40 @@ elif [[ $command == list-backups ]]; then # Validate that instance directory contains valid instance. validate_path_or_terminate "instance_directory" "$instance_directory" "$ERROR_ARGUMENTS" - # Extended regular expression for matching YYYY-MM-DD-hh:mm:ss format. - backup_destination_name_pattern="[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}_[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}" - # Read current game version. # shellcheck source=/dev/null source "$instance_config" current_game_version="$game_version" unset game_version - # Detect and show available backups to the user. - # shellcheck disable=SC2010 # Using ls with quoting style shell-escape will produce one filename per line. - if [[ ! -d $instance_backup_directory ]] || ! ls --quoting-style=shell-escape "$instance_backup_directory" | grep -q -E "^$backup_destination_name_pattern\$"; then + # Set-up list of backup directories. + shopt -s nullglob + # Glob expression for matching YYYY-MM-DD-hh:mm:ss format. + backup_directories=("$instance_backup_directory"/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]:[0-9][0-9]:[0-9][0-9]) + shopt -s nullglob + + # Show available backups to the user. + if (( ${#backup_directories[@]} == 0 )); then echo "No backups are available for instance $(colorecho -n green "$instance")." else echo "Available backups for instance $(colorecho -n green "$instance") (current version $(colorecho -n green "$current_game_version"))." echo - for backup_destination in "$instance_backup_directory"/*; do - - if [[ $backup_destination =~ $backup_destination_name_pattern ]]; then - backup_date=$(basename "$backup_destination") + for backup_directory in "${backup_directories[@]}"; do - # Read instance configuration for backup. - # shellcheck source=/dev/null - source "$backup_destination/instance.conf" - - if [[ -f "$backup_destination/.description" ]]; then - backup_description=$(<"$backup_destination/.description") - echo " - $backup_date - version $(colorecho -n green "$game_version") ($backup_description)" - else - echo " - $backup_date - version $(colorecho -n green "$game_version")" - fi + backup_date=$(basename "$backup_directory") + # Read instance configuration for backup. + # shellcheck source=/dev/null + source "$backup_directory/instance.conf" + if [[ -f "$backup_directory/.description" ]]; then + backup_description=$(<"$backup_directory/.description") + echo " - $backup_date - version $(colorecho -n green "$game_version") ($backup_description)" + else + echo " - $backup_date - version $(colorecho -n green "$game_version")" fi + done echo