Prevent script from following symbolic links

Asked

Viewed 134 times

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" ?

  • 1

    I don’t understand much bash, despite being aware of like-Unix systems, this link helps: http://unix.stackexchange.com/a/141485 ?

  • @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!

  • 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.

3 answers

1

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

find $HOMEDIR -maxdepth 1 ! -type l

The -maxdepth 1 searches only at a directory tree level (similar to ls) the ! -type l search only files that are not symbolic links

More about the ls at this link: http://rberaldo.com.br/curso-shell-script-comandos-basicos-linux/

  • Thank you for the answer, but as I mentioned in the comments in the question, I had already tested it but the result is far from expected, the find give me back the full path and not just the user to build the email address, so the solution will need to be more elaborate. If you have the opportunity I thank you, in the meantime.

1


You can use ls -l and remove the lines it contains "->".

Whirling ls -lA, you will 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 only get the filenames from 58° 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 you don’t have ">" with grep -v '>' :

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

.ash_history                                                                    
hello.c   

In short: change your ls -A for 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 treatment for this.

0

Does 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.