to create Soft linked file if not present

September 14, 2011 at 02:43:23
Specs: Windows XP
Hi,

As i am beginner to shell scripting, guide me to write a code comparing a flat file containing linked files, link with soflinks available under / directory and if the file doesnt exist i have to create that. this should be done with a script

I have used " find / -type l -exec ls -l {} \; >> listing_main" command to create the flat file which have all the list of linked files. these file are taken regular backup in regular interval and in period of crash his will be retained .. i have to check wheather all softlinks are retrieved back properly.

Thanks,
New_beginner


See More: to create Soft linked file if not present

Report •


#1
September 14, 2011 at 11:32:34
I am assuming that you are looking for broken links. Since you are a beginner, if in my working directory and directory tst exists, this command creates link mylink

ln -s ./tst/myfile mylink

performing a long listing shows:

lrwxrwxrwx 1 nails people 12 Sep 14 10:37 mylink -> ./tst/myfile

This command delivers the target:

ls -l mylink |awk ' { print $NF } '

If you remove ./tst/myfile, you have a broken link.

This Korn script finds broken links using your listing_main file:

#!/bin/ksh

# UNTESTED!
while read mylink
do
   thetarget=$(ls -l $mylink |awk ' { print $NF } ')
   ls "$thetarget" > /dev/null 2>&1
   if [[ $? -ne 0 ]]
   then # exit code is non-zero if the target is missing
      echo "for $mylink: target is missing: $thetarget"
   fi
done <  listing_main
# end script

Finally, Run this script from the directory where the listing_main file was created as as the user that created the listing_main file or root.

Let me know if you have any questions.


Report •

#2
September 15, 2011 at 04:13:38
many thanks , I was enlighten about awk script, but I want to check weather symbolic links present under /tmp directory and it sub directory are well present after putting the Backup(into production) taken one week back, and if the link files are not been linked correctly once after backup , i want to check this and create those link through a script. using "find /tmp type -l exec ls -l {} \:" command i have taken all the linked files under /tmp directry before taking Backup.. so using this flat file i have to compare and create. Please help

Report •

#3
September 15, 2011 at 09:21:19
Sorry, I cannot help as I don't understand what you are trying to do.

Report •

Related Solutions


Ask Question