Prevent the script from following symbolic links

6

With the script below, a search is made to all the existing properties in the indicated directory in order to obtain the email addresses associated with each hosting:

#!/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 usage:

./getMails /home/ > lista.txt

Result in 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 performing the search only with "regular" directories?

    
asked by anonymous 22.12.2014 / 13:10

3 answers

1

You can use ls -l and remove the rows containing "->".

Running ls -lA , you'll get something like this:

-rw-------    1 root     root           576 Jan 21 16:22 .ash_history           
lrwxrwxrwx    1 root     root             9 Jan 21 16:18 dos -> /root/dos       
-rw-r--r--    1 root     root           242 Jan 21 16:18 hello.c   

Then you can get only the filenames from the 58th character using cut -c 58- :

$ ls -lA | cut -c 58-

.ash_history                                                                    
dos -> /root/dos                                                                
hello.c

And from this result you can extract only what does not have ">" with grep -v '>' :

$ ls -lA | cut -c 58- |grep -v '>'  

.ash_history                                                                    
hello.c   

In short: change your ls -A by ls -lA | cut -c 58- |grep -v '>'

NOTE: This option will include a blank line along with the result, but your code already has a handle to it.

    
21.01.2015 / 17:38
1

Instead of doing ls in $ HOMEDIR, use the command find , like this:

find $HOMEDIR -maxdepth 1 ! -type l

-maxdepth 1 looks only at one level of the directory tree (similar to ls ) ! -type l looks for files other than symbolic links

More about ls at this link: link

    
29.12.2014 / 01:06
0

Do not process ls .

The test [ -L … ] identifies symbolic links.

for i in "${DOMAIN[@]}"; do
  for f in "$HOMEDIR$x/mail/$i"/* "$HOMEDIR$x/mail/$i"/.*; do
    n="${f%%*/}"  # o nome sem o diretório
    case "$n" in
      .|..|cur|new|tmp) continue;; # arquivos não interessantes
    esac
    if [ -L "$n" ]; then continue; fi  # ligação simbólica
    echo  "$n"@"$i"
  done
done
    
22.12.2015 / 20:57