[Tutor] Struggling with os.exec

lonetwin lonetwin@yahoo.com
Tue May 27 04:03:04 2003


Hi there,

On Friday 23 May 2003 09:44 pm, Charlie Clark wrote:
> Sure, I'm trying to write the following script in Python:
>
> function goThere() {
> echo $1
>     if [ -d "$1" ] ; then
>             cd "$1"
>             return $?
>         fi
>     return 1
> }

def goThere(dir_name):
    print 'Changing to %s from %s' % (dir_name, os.getcwd())
    if os.path.isdir(dir_name):
        return os.chdir(dir_name)
    return 1

> function countArgs() {
> 	return $#
> }

def countArgs(some_args):
     return len(some_args)

> function j() {
> choices=$(query "name=\"$1\"" | tr '\n' ' ')
> eval "countArgs $choices"
> case $? in
>     0) return 1;;
>     1) eval "goThere $choices" ; return $?;;
>     *)   PS3='Choice: '
>          eval "\
>          select curChoice in ${choices};\
>          do\
>             goThere \"\$curChoice\";\
>             if [ \$? -eq 0 ];\
>             then\
>                 break;\
>             fi;\
>         done"
> esac
> }

Now here I don't really understand what 'query' is in the line:
> choices=$(query "name=\"$1\"" | tr '\n' ' ')
but I'm assuming that $choices would be a string that has names of directories 
within a directory hierarchary.
eg: if the dir. structure is 
top
 |- first
 |   |- second
 |        |- third

choices would be ['first', 'second', 'third' ]

so the above function j becomes:

def j():
    choices = ['first', 'second', 'third' ] # or ['first'] or []
    if not choices:
        return 1
    elif countArgs(choices) == 1:
        goThere(choices[0])
    else:
        for curChoice in choices:
            print os.getcwd()
            if goThere(curChoice) == 1:  # Sorry this part may be wrong bcos I
                break                    # don't really know what $choices is

Now, does this work as expected ?? If it doesn't what is expected ??

Regards
Steve
-- 
Never be afraid to tell the world who you are.
                                     - Anonymous