sh script problem with capturing return code

Fbsd8 fbsd8 at a1poweruser.com
Wed Jan 9 15:12:22 UTC 2013


Lowell Gilbert wrote:
> Fbsd8 <fbsd8 at a1poweruser.com> writes:
> 
>> Polytropon wrote:
>>> On Tue, 08 Jan 2013 16:30:49 -0500, Fbsd8 wrote:
>>>> I can not get the return code from mtree to control
>>>> the displaying of a error message.
>>>>
>>>> The mtree at the end of the script does function correctly
>>>> because I can tell from the printed output.
>>>>
>>>> When mtree prints comments saying "extra" that means the directory
>>>> being read does not match the specification file. return code
>>>> should be Not equal to zero.
>>>>
>>>> And when they do match IE: no mtree comments printed, that should
>>>> be a return code of zero.
>>>>
>>>> I tried 2 different ways to capture the return code to no joy.
>>>> What I am doing wrong?
>>>>
>>>> #! /bin/sh
>>>> flavor="/a/mtree.std"
>>>> echo "flavor = ${flavor}"
>>>> /bin/cat  << EOF |
>>>> /set type=dir uname=root gname=wheel
>>>> .
>>>> etc     ignore
>>>> ..
>>>> root    ignore
>>>> ..
>>>> usr
>>>> home    ignore
>>>> ..
>>>> local
>>>> etc     ignore
>>>> ..
>>>> ..
>>>> ..
>>>> ..
>>>> EOF
>>>>
>>>>   mtree -d -u -p "${flavor}"  || \
>>>>     echo "Error invalid directories in flavor ${flavor}."
>>>>
>>>> #mtree -d -p "${flavor}"
>>>> #[ $? -eq 0 ] || \
>>>> #  echo "Error invalid directories in flavor ${flavor}."
>>>> echo "return = $?"
>>> It seems that returning 0 is correct in a case as you mentioned.
>>> The manual at "man mtree" states:
>>>
>>> EXIT STATUS
>>>      The mtree utility exits 0 on success, and >0 if an error occurs.
>>>
>>> Question: What _is_ an error here?
>>>
>>> If you use the source Luke at /usr/src/usr.sbin/mtree/mtree.c,
>>> you could find out what combination of options plus circumstances
>>> found at runtime could trigger an exit status != 0.
>>>
>>> The main() functions finishes with exit(status); where status
>>> is either set by functions mtree_specspec() or mtree_verifyspec(),
>>> or manually to 0 when -U is provided and MISMATCHEXIT (is 2) is
>>> encountered.
>>>
>>> Again from the manual:
>>>
>>>      -u    Same as -U except a status of 2 is returned if the file hierarchy
>>>            did not match the specification.
>>>
>>>      -U    Modify the owner, group, permissions, and modification time of
>>>            existing files to match the specification and create any missing
>>>            directories or symbolic links.  User, group and permissions must
>>>            all be specified for missing directories to be created.  Corrected
>>>            mismatches are not considered errors.
>>>
>>> However, you're not using -U, but -u, so the last sentence of
>>> the description above should be relevant: No error per se,
>>> even though the status code should be 2.
>>>
>>>
>>>
>>>
>> Well I just tested with -U -u together no joy.
>> Tested with -U no joy.
>>
>> My read of the above is -u should cause a return code of 2 when the
>> file hierarchy does not match the specification. I don't want -U
>> because I am not modifying any content of the directory tree mtree is
>> looking at.
>>
>> I just don't get the point your trying to make.
>>
>> Oh the other hand are you saying the script code is correct to capture
>> the return code but using wrong options with mtree ?
> 
> It works fine for me:
> 
>     [5023] (lowell-desk) temp> touch foo
>     [5024] (lowell-desk) temp> mtree -c > ../out
>     [5025] (lowell-desk) temp> if (mtree < ../out ) ; then echo yes ; else echo no ; fi
>     yes
>     [5026] (lowell-desk) temp> touch foo
>     [5027] (lowell-desk) temp> if (mtree < ../out ) ; then echo yes ; else echo no ; fi
>     foo changed
>             modification time expected Wed Jan  9 02:42:31 2013 found Wed Jan  9 02:42:47 2013
>     no
>     [5028] (lowell-desk) temp> echo $?
>     0
>     [5029] (lowell-desk) temp> mtree < ../out
>     foo changed
>             modification time expected Wed Jan  9 02:42:31 2013 found Wed Jan  9 02:42:47 2013
>     [5030] (lowell-desk) temp> echo $?
>     2
>     [5031] (lowell-desk) temp> touch temp
>     [5032] (lowell-desk) temp> mtree -u < ../out
>     . changed
>             modification time expected Wed Jan  9 02:42:37 2013 found Wed Jan  9 02:49:52 2013 modified
>     foo changed
>             modification time expected Wed Jan  9 02:42:31 2013 found Wed Jan  9 02:42:47 2013 modified
>     temp extra
>     [5033] (lowell-desk) temp> echo $?
>     2
> 
> This is exactly what I would expect, and what you said you weren't
> getting. You didn't show what you ran your script on, or what the
> results were, so I can't tell you what you're doing wrong -- but compare
> my example to yours and I'm sure you'll be able to figure it out.
> 
> Good luck.
> 
> 

Your example is testing for file changes. The mtree spec file I posted
and the mtree -d shows I am checking just at the directory level. The
"ignore" option on the spec file means "ignore any file hierarchy below
this file". That coupled with the -d option that says "ignore everything
except directory type files".

So what I want to get is; apply that spec file to the target directory
checking that the spec directories are present in the target and any 
sub-directories on those spec directories are ignored on the target, IE; 
is not an error condition. All files in spec directory tree and the 
target directory tree are ignored. Any directories in the target 
directory tree not in the spec file are errors and set the return code 
to non-zero.

The spec and target directory trees look like this.

   +mtree.spec
    | +etc
    | +root
    | +usr
    |   +home
    |   +local
    |     +etc


    |  +mtree.error   also has 2 links, home & jail
    |    +etc
    |      +ssh        this should be ignored
    |    +root
    |      +archives   this should be ignored
    |      +bin        this should be ignored
    |    +usr
    |      +home
    |        +bob      this should be ignored
    |      +local
    |        +etc
    |      +share      this is an error
    |        +skel


   mtree -d gives this output

home extra
jail extra
usr/share extra
But return code is zero when it should be not=zero.

Seems to me every thing is working like designed but the return code.

So the question remains, why is mtree giving a return of zero when it 
finds directories on the target that are not in the spec file?





More information about the freebsd-questions mailing list