2

I want to run git stripspace on all my source code files, on each build (or precommit or just manually), to remove extraneous whitespace, and later maybe to format the code in a standard way,

So I want to do something like

git stripspace < project/code.m > project/code.m

so I thought I should be able to use find -exec or xargs but by the looks of find does not like 'git stripspace' (maybe because of the space in between or the < or >) and I don't know how to pass the same file name, same argument, twice in a row with the proper redirections (< >) .

Ali
  • 6,943
  • 4
    DO NOT DO THIS. Because of the way the shell interprets the command sequences, it'll empty project/code.m first to prepare the output pipe from the > before it tries to read it. – Shadur-don't-feed-the-AI Jul 26 '13 at 04:48
  • I see the concept that would be applicable from the duplicate suggested but it doesn't directly address the question here though. I think the chosen answer is very straight-forward and can be a good reference. – Julie Pelletier Aug 29 '16 at 18:15

2 Answers2

4

You're clobbering the file before you read from it. You need to use a temporary file or a utility like sponge from moreutils.

You can't use find -exec or xargs in this case. You would need to pass a shell a single argument containing both the commands and the file name. xargs and find expect the file name marker (which they replace with the file name) to be a standalone argument. You can use a loop instead.

With a temporary file:

find | while read file ; do
    git stripspace < "$file" > tempfile
    mv -f tempfile "$file"
done

With sponge:

find | while read file ; do
    git stripspace < "$file" | sponge "$file"
done
0

You can use Vim in Ex mode:

ex -sc '%!git stripspace' -cx project/code.m
  1. % select all lines

  2. ! run command

  3. x save and close

Zombo
  • 1
  • 5
  • 44
  • 63