tags:

views:

70

answers:

3
+1  Q: 

pipelines vs for

Starting to play with bash on Linux, I am trying to perform a clearcase operation on all files resulting from an other clearcase operation. In other words I want to check in all the checked out files.

The command to list checked out files is: cleartool lsco -short -rec. The command to check in a file is: cleartool ci -nc filename.

I am able to do that using the following for loop:

for f in 'cleartool  lsco -short -rec.'; do cleartool ci -nc $f; done

I am wondering if there is an other way to do that using pipelines? Something like:

cleartool lsco -short -rec . | cleartool ci -nc

The problem here is that cleartool doesn't read stdin but expects a parameter, correct?

+3  A: 

Yes, correct.

In general, you can convert "list of arguments one per line, as coming through a pipe" to "list of arguments all in the same line, as in a command invocation" using xargs:

Richard also points out a way to do this more directly using shell substitution.

$ cleartool lsco -short -rec . | xargs cleartool ci -nc
unwind
+1  A: 
cleartool ci -nc `cleartool lsco -short -rec .`
Richard Pennington
This solution works until you do not exceed maximum number of command-line arguments which is 65536 by default in linux.
corvus
The funny thing is that I have something very similar as a shell alias at work.
Richard Pennington
A: 

I have these two functions defined in my .bashrc:

$ cico () { ct lsco -me -cview -short ${@} | enquote | xargs cleartool ci -nc; }
$ cicor () { ct lsco -me -r -cview -short ${@} | enquote | xargs cleartool ci -nc; }

Enquote is:

$ enquote () { /usr/bin/sed 's/^/"/;s/$/"/' ; }

You'll want the -me and -cview if you start working on a project with other people, because, by default, lsco will show everyone's checked out files and the files you have checked out to different views.

In general, the main command you're looking for is xargs.

Dave