rapairing broken symlinks

Try that:

erlangen:~ # cat /root/bin/findlinks.sh  
#!/bin/bash 
if  -z $1 ]]; then 
        echo Usage: findlinks.sh folderpath 
        exit 
fi 

find $1 -type l|while read n ; do 
        t=$(readlink "$n") 
        r=$(realpath "$n") 
        if  ! -e "$t" ]] &&  ! -e "$r" ]] ; then  
                        echo link name "$n": target "$t" does not exist. 
                if  "$t" == ../* ]] ; then 
                        echo ln -sf "$r" "$n" 
                else 
                        echo ln -sf /"$t" "$n" 
                fi 
        fi  
done 
  
erlangen:~ #

Some test:

erlangen:~ # ln -sf usr/bin/ls /home/link
erlangen:~ # findlinks.sh /home
realpath: /home/link: No such file or directory
link name /home/link: target usr/bin/ls does not exist.
ln -sf /usr/bin/ls /home/link
erlangen:~ # 

In that script I did i should have quoted the variables like this, right?


[FONT=monospace]#!/bin/bash 

**if** **** -L "$1" ]; 
**then** 

  **echo**  File 
  **echo** "$1" 

  RELATIVEPATH=$(readlink  "$1") 
  **echo** symlink relative path 
  **echo** "$RELATIVEPATH" 

  ABSOLUTEPATH="/${RELATIVEPATH}" 
  **echo** symlink absolute path 
  **echo** "$ABSOLUTEPATH" 


  **if** **** "${RELATIVEPATH:0:1}" **!**= "/" ] && **** -e "$ABSOLUTEPATH" ] ; 
  **then** 
    **echo** repairing symlink 
    ln -sf $ABSOLUTEPATH $1 
    
    **echo** "File:  $1   Broken link: $RELATIVEPATH  Replaced with   $ABSOLUTEPATH "  
    **echo** "File:  $1   Broken link: $RELATIVEPATH  Replaced with   $ABSOLUTEPATH " >> repairbrokenlinks.log 

  **fi** 
   
**else** 
  **echo** "Not a symlink:  $1" 

**fi**
 

[/FONT]