#!/bin/bash WORK_DIR=`mktemp -d` RED=`tput setaf 1` GREEN=`tput setaf 10` YELLOW=`tput setaf 11` PURPLE=`tput setaf 13` RESET=`tput sgr0` fancy_echo() { local fmt="$1"; shift # shellcheck disable=SC2059 printf "\n$fmt\n" "$@" } # check if tmp dir was created if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then echo "Could not create temp dir" exit 1 fi # deletes the temp directory function cleanup { rm -rf "$WORK_DIR" fancy_echo "${YELLOW}Deleted temp working directory $WORK_DIR${RESET}" } # register the cleanup function to be called on the EXIT signal trap cleanup EXIT if [[ -f "$HOME/.ssh/authorized_keys" ]]; then cp -u "$HOME/.ssh/authorized_keys" "$HOME/.ssh/authorized_keys.bak" cp -u "$HOME/.ssh/authorized_keys" "$WORK_DIR/authorized_keys.tmp" else touch "$WORK_DIR/authorized_keys.tmp" fi for file in ./*.pub do cat "$file" | tee -a "$WORK_DIR/authorized_keys.tmp" done # cp "$WORK_DIR/authorized_keys.tmp" "$WORK_DIR/authorized_keys.wip" sort -u "$WORK_DIR/authorized_keys.tmp" > "$WORK_DIR/authorized_keys.wip" # cat "$WORK_DIR/authorized_keys.wip" cp "$WORK_DIR/authorized_keys.wip" "$HOME/.ssh/authorized_keys"