Trouble removing dead links as root

Trouble removing dead links as root

Post by yan_can_c.. » Sat, 14 Apr 2001 23:43:25



I tried the script posted to comp.unix.aix on Solaris 2.5.1 (running
ksh).  And, it works fine as long as I am not logged in as root.  When I
do log in as root, an error in the 'if' statement terminates the script.

When I am logged in as anyone else, the errors related to the 'if' are
either "test argument expected" or "bad test operator".  But, the script
will still run to completion.  In order to solve this, I have tried to
turn a lower user into a super user and also tried to locate the root's
initialization scripts to see if there is something akin to a 'trap' that
is causing this trouble.  Neither method has met with any success as the
new super user still did not have full rights to traverse the whole tree;
and, I could not locate the correct script (.kshrc?) for the root.  I
also tried to rewrite the 'if' with a 'test', but this falied as well.

Is there any logical reason why root is booted out of the script when an
error occurs while all other users do not have this problem?  If so, how
do I fix it?  Or, is there a better way to do the 'if'?

Quote:> #This script will display an error message about files that it's
> #unsure of, but will NOT take any action.  Also, the script will
> #remove empty directories and dead links via a prompted remove.
> #And, the session results are appended to /tmp/dead_link.out.
> #Written by:  Jeremy Huber on April 12, 2001
> echo "-------------------------------------" >> /tmp/dead_link.out
> for i in `find . -type l`
> do
>    j=`find $i -type l -exec ls -Ll \{} \; | awk '{ print $9 }'`
>    if [ ! $j ] ; then
>       echo "Dead link / null dir found: " $i
>       echo "Dead link / null dir found: " $i >> /tmp/dead_link.out
>       rm -i $i  #remove -i to do a non-prompted rm (auto delete).
>    fi
> done

 
 
 

Trouble removing dead links as root

Post by Barry Margoli » Sun, 15 Apr 2001 00:06:50




>I tried the script posted to comp.unix.aix on Solaris 2.5.1 (running
>ksh).  And, it works fine as long as I am not logged in as root.  When I
>do log in as root, an error in the 'if' statement terminates the script.

>When I am logged in as anyone else, the errors related to the 'if' are
>either "test argument expected" or "bad test operator".  But, the script

You need to quote $j to get an empty string to be processed properly.
Without the quoting, if $j is empty, you just get:

if [ ! ]

instead of:

if [ ! "" ]

You should also put quotes around $i, in case any of the filenames contain
spaces.  Otherwise, if there's a link named "foo bar", the command

rm -i $i

will try to remove "foo" and "bar", not "foo bar".

Quote:>Is there any logical reason why root is booted out of the script when an
>error occurs while all other users do not have this problem?  If so, how
>do I fix it?  Or, is there a better way to do the 'if'?

Probably root's shell is sh, not ksh, which may explain the different
behavior.  If you want your script to be run with a specific shell, you
have to put a #!/bin/ksh line at the beginning.

BTW, why are you running find twice?  Why don't you replace the second find
with just:

ls -lL "$i" | awk '{print $9}'

Quote:>> #This script will display an error message about files that it's
>> #unsure of, but will NOT take any action.  Also, the script will
>> #remove empty directories and dead links via a prompted remove.
>> #And, the session results are appended to /tmp/dead_link.out.
>> #Written by:  Jeremy Huber on April 12, 2001
>> echo "-------------------------------------" >> /tmp/dead_link.out
>> for i in `find . -type l`
>> do
>>    j=`find $i -type l -exec ls -Ll \{} \; | awk '{ print $9 }'`
>>    if [ ! $j ] ; then
>>       echo "Dead link / null dir found: " $i
>>       echo "Dead link / null dir found: " $i >> /tmp/dead_link.out
>>       rm -i $i  #remove -i to do a non-prompted rm (auto delete).
>>    fi
>> done

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Trouble removing dead links as root

Post by yan_can_c.. » Sun, 15 Apr 2001 02:13:13


Thanks for the input!  The script now works great.
 
 
 

Trouble removing dead links as root

Post by Mathew Kirsc » Tue, 17 Apr 2001 23:48:58



> I tried the script posted to comp.unix.aix on Solaris 2.5.1 (running
> ksh).  And, it works fine as long as I am not logged in as root.  When I
> do log in as root, an error in the 'if' statement terminates the script.

> When I am logged in as anyone else, the errors related to the 'if' are
> either "test argument expected" or "bad test operator".  But, the script
> will still run to completion.  In order to solve this, I have tried to
> turn a lower user into a super user and also tried to locate the root's
> initialization scripts to see if there is something akin to a 'trap' that
> is causing this trouble.  Neither method has met with any success as the
> new super user still did not have full rights to traverse the whole tree;
> and, I could not locate the correct script (.kshrc?) for the root.  I
> also tried to rewrite the 'if' with a 'test', but this falied as well.

If you haven't changed root's shell, root uses /sbin/sh, a statically linked
version of the Bourne shell. The rest of your users appear to use the Korn
shell. Different shells behave differently. Evidently, the Bourne shell exits
on the first error, and the Korn shell doesn't exit on non-fatal errors.

Now, best practice is to fix the script instead of fuddling with the account.
You've already done that, I just thought that a little more background might
be helpful in the future.

Quote:> Is there any logical reason why root is booted out of the script when an
> error occurs while all other users do not have this problem?  If so, how
> do I fix it?  Or, is there a better way to do the 'if'?