1#!/bin/bash 2set -e 3if [ -z "$1" ]; then 4 source_list=/dev/stdin 5 dest_list=/dev/stdout 6else 7 source_list="$1" 8 dest_list="$1" 9fi 10# Load the file 11readarray A < "$source_list" 12# Sort 13IFS=$'\n' 14# Stash away comments 15C=( $(grep -E '^#' <<< "${A[*]}" || :) ) 16A=( $(grep -v -E '^#' <<< "${A[*]}" || :) ) 17# Sort entries 18A=( $(LC_COLLATE=C sort -f <<< "${A[*]}") ) 19A=( $(uniq <<< "${A[*]}") ) 20# Concatenate comments and entries 21A=( ${C[*]} ${A[*]} ) 22unset IFS 23# Dump array back into the file 24if [ ${#A[@]} -ne 0 ]; then 25 printf '%s\n' "${A[@]}" > "$dest_list" 26fi 27