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.

Milliways
  • 1,378
  • 1
    you could do this simply with the find command: find / -name "*.py" -exec fgrep '#!/usr/bin' {} \; -print – Ketan Feb 20 '16 at 03:45
  • Escaping the # didn't help? – Jeff Schaller Feb 20 '16 at 03:47
  • Do you want to make this command work (with minimal changes), or nest strings in general? – Michael Homer Feb 20 '16 at 04:04
  • @MichaelHomer At this stage I just want to make it work. I have had similar problems before, so would like to understand if there is a solution. I retried the plain command suggested by @Ketan with sudo, and this works. As I am only using a single command there is no need for the bash command. – Milliways Feb 20 '16 at 04:14
  • 1
    fgrep '\#\!/usr/bin' worked for me. – jai_s Feb 20 '16 at 04:14
  • @jai_s Thanks '\#\!/usr/bin' works. I obviously didn't have enough escapes. – Milliways Feb 20 '16 at 04:41

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,378