6
With the script below, a search is carried out for all the accommodations in the indicated directory in order to obtain the email addresses associated with each accommodation:
#!/bin/bash
# Scan All available email account addresses
# for homedir under the provided path
# 2014-12-19 Salustiano Silva
# Control :: Provided directory supplied ?
if [ -z "$1" ]; then
echo "É preciso passar como parâmetro o caminho completo para a diretoria 'home'!"
exit
fi
HOMEDIR="$1"
# Control :: Provided directory exists ?
if [ ! -d "$HOMEDIR" ]; then
echo "A diretoria $HOMEDIR não foi localizada, verifique os dados fornecidos!"
exit
fi
HOMEDIR="$1"
CPANELUSERS=`ls -1A /var/cpanel/users/`
count=1
for x in `echo -n "$CPANELUSERS"`;do
wiersz=`grep -i ^dns /var/cpanel/users/"$x" |cut -d= -f2`
DOMAIN[$count]=$wiersz
count=$[$count+1]
echo "Login: `echo "$x"`"
for i in `echo "${DOMAIN[@]}" | sed 's/ /\n/g'`;do
for n in ` ls -A "$HOMEDIR""$x"/mail/"$i"/ 2>/dev/null`;do
if [ "$n" == "cur" ];then echo "$n" > /dev/null
elif [ "$n" == "new" ];then echo "$n" > /dev/null
elif [ "$n" == "tmp" ];then echo "$n" > /dev/null
elif [ "$n" == "" ];then echo "$n" > /dev/null
else
echo "$n"@"$i"
fi
done
done
echo;echo;
done
Example of use:
./getMails /home/ > lista.txt
Output in the file lista.txt
:
Login: example
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Login: test
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Question
How can I prevent the script from following symbolic links to other locations by only searching with "regular directories" ?
I don’t understand much
bash
, despite being aware of like-Unix systems, this link helps: http://unix.stackexchange.com/a/141485 ?– Guilherme Nascimento
@Guilhermenascimento It should, though, I had already seen this and two others in the Unix & Linux but by the tests I performed, the result is rigorously the same. Thank you for the tip!
– Zuul
You would have to create an If testing whether the directory described in for is a symbology link or not. Remember in sisetmas like-*Nix all directories are a special type of file.
– Thiago Oliveira