Search code examples
unixclearcaseksh

ClearCase command won't run in script


I am trying to automate ClearCase check-ins via a ksh script. There is a strange issue where the following command will not run while it's part of the automation script, but runs fine if I paste it into the command line.

Snippet of script:

for dir in `cat $DIRS`
do
    RUNCMD="cleartool find <<vob_directory>>/$dir -type f -exec 'cleartool co -nc \$CLEARCASE_PN'"
    print $RUNCMD
    $RUNCMD
done
exit 1

produces the following command

cleartool find <<vob_directory>> -type f -exec 'cleartool co -nc $CLEARCASE_PN'

Here's the error

cleartool: Error: Extra arguments: "co"
Usage: find { pname ... [-depth | -nrecurse | -directory]
        | [pname ...] -all [-visible | -nvisible]
        | -avobs [-visible | -nvisible]
        }
        [-name 'pattern']
        [-cview]
        [-user login-name]
        [-group group-name]
        [-type {f|d|l}...]
        [-follow]
        [-kind object-kind]
        [-nxname]
        [-element query]
        [-branch query]
        [-version query]
        {-print | -exec command-invocation | -ok command-invocation} ...

What am I doing wrong here?


Solution

  • In ksh though (my first tests were in bash), the eval solution you recommend is more usual (bash eval is a bit different).

    You can see it in this clearcase multisite script.
    If you need to store the result in a variable for processing, you can follow this question:

    for dir in `cat $DIRS`
    do
        RUNCMD="cleartool find <<vob_directory>>/$dir -type f -exec 'cleartool co -nc \$CLEARCASE_PN'"
        print $RUNCMD
        eval "res=\$( $RUNCMD )"
        print $res
    done
    exit 1
    

    Original answer:

    You might want to add some quotes in order to prevent the shell to expand your command too soon, as in this example:

    RUNCMD="cleartool find <<vob_directory>>/$dir -type f -exec "'cleartool co -nc \$CLEARCASE_PN'""
    

    or even chaging the single quote by a double quote (as in this thread)

    RUNCMD="cleartool find <<vob_directory>>/$dir -type f -exec "cleartool co -nc \"\$CLEARCASE_PN\""
    

    (note the '\"' around \$CLEARCASE_PN in order to take into account path names with space in them)