1

I am attempting to write a script to list all the shebang lines in python files.

What I would like to do is

sudo bash -c 'for logf in $(find / -name "*.py"); do fgrep '#!/usr/bin' "$logf"; done'

This gives error bash: !/usr/bin': event not found. I understand why, although the meaning of the error is unclear.

The trouble is I can't figure out how to pass a string to bash which includes another string without command execution.

If I leave off the "#!" it works, but of course includes a number of other lines.

I have tried practically every combination of escape and string without success.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Milliways
  • 1,348
  • 5
  • 18
  • 35

1 Answers1

1

Thanks to the comments I have 2 versions which work. Note I have made a couple of changes (to only find shebang at beginning of line) and to allow white space after shebang.

sudo bash -c 'for logf in $(find / -name "*.py"); do grep '^\\#\\!/usr/bin' "$logf"; done'

For some reason when I tried to allow white space after shebang I couldn't get it to work.

Even better (and simpler)

sudo find / -name "*.py" -exec grep '^#! */usr/bin' {} \;
Milliways
  • 1,348
  • 5
  • 18
  • 35