Code:
#!/bin/bash
(( UID )) && { echo 'This script requires root privileges, use sudo'; exit 1; }
col=$'\e[1;36m'
rst=$'\e[0m'
while :
do
qmail=()
read -p "Enter name to Search and Destroy: " user
while read -rd $'\0' f
do
qmail+=( "$f" )
done < <( grep -ZHilF "$user" .qmail* )
(( ${#qmail[@]} )) || { printf 'Specified name not found\n\n'; continue; }
(( ${#qmail[@]} == 1 )) && als='alias' || als='aliases'
printf '%s%s%s is found in %d qmail %s:\n' "$col" "$user" "$rst" ${#qmail[@]} "$als"
printf '%s\n' "${qmail[@]}"
(( ${#qmail[@]} == 1 )) && als='this list' || als='these lists'
until [[ ${confirm,,} = [yn] ]]
do
printf 'Delete %s%s%s from %s? (y/n) ' "$col" "$user" "$rst" "$als"
read confirm
done
if [[ ${confirm,,} = y ]]
then
sed -i "/$user/d" .qmail*
printf 'User %s%s%s deleted\n' "$col" "$user" "$rst"
fi
confirm=
printf '\n'
done
should fit the bill, though test it first in some junk directory on dummy data. Detection and deletion with straightforward grep/sed are kind of stupid and not foolproof enough for my taste, so you shouldn't enter things like 'a' or 'john' that would match boatloads of records.
Bookmarks