1

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?

user1766760
  • 631
  • 2
  • 8
  • 26

2 Answers2

2

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)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I think you're onto something about the command being expanded (or *not* expanded) correctly. I tried several "styles" of quotes arrangements, but ultimately none really worked. Appreciate you lending a set of eyes on this. – user1766760 Apr 17 '13 at 21:53
  • thanks for the links - I'm marking your reply as answer since it'll be more useful. And I don't know about the differences between `ksh` and `bash` myself. – user1766760 Apr 19 '13 at 23:19
1

I can't speak to why this works, but a co-worker tipped me off about eval. Turns out this works.

for dir in `cat $DIRS`
do
    RUNCMD="cleartool find <<vob_directory>>/$dir -type f -exec 'cleartool co -nc \$CLEARCASE_PN'"
    print $RUNCMD
    eval $RUNCMD
done
exit 1
user1766760
  • 631
  • 2
  • 8
  • 26
  • Excellent. +1. I have referenced it in my question, with an additional reference to other ksh eval questions. – VonC Apr 18 '13 at 05:29