-5

Can some explain me what the following does?

find $FIRMWARE_BASE_DIR \( -name "*.txt" \) -type f | 
  xargs -t -n1 sed -i '' -e '/^#/d;/^nocrc=/d;/macaddr=/d;/^$/d;s/[ \t]*$//'
larsks
  • 277,717
  • 41
  • 399
  • 399
Jeremyapple
  • 253
  • 2
  • 6
  • 20
  • 3
    Read the manual pages for `find`, `xargs`, and `sed`. Understanding this has nothing to do with a shell script. – lurker Jun 25 '18 at 00:33
  • Then learn regex, because they're everywhere, and they're quite useful (until you have two problems). – Dave Newton Jun 25 '18 at 00:42
  • Do it in parts. 1) the find. What is the value of $FIRMWARE_BASE_DIR? Then, look at what the `-name` options does. Same thing with `-type f`. The result of this find goes to xargs. 2) look at xargs, what are `-t` `-n1` options do? Then sed. The idea here is to deconstruct the command to its parts and build up from that. – Nic3500 Jun 25 '18 at 00:50

1 Answers1

0

This bit:

find $FIRMWARE_BASE_DIR \( -name "*.txt" \) -type f

finds all regular files ending with .txt under the directory stored in the FIRMWARE_BASE_DIR environment variable.

Note that the parenthesis are escaped so that they are passed to find instead of being interpreted by the shell but there is only one find predicate inside them so they have no function - if this command is machine generated perhaps it may contain more than one term sometimes? If not then they could be removed.

You can find the documentation at http://man7.org/linux/man-pages/man1/find.1.html

This bit:

xargs -t -n1 command

takes that list of files and runs whatever command is specified on each filename (printing the command to the screen beforehand)

You can find the documentation at http://man7.org/linux/man-pages/man1/xargs.1.html

This bit:

sed -i '' -e '/^#/d;/^nocrc=/d;/macaddr=/d;/^$/d;s/[ \t]*$//'

edits the input file in place without a backup:

  • deleting any line starting with # or nocrc=
  • deleting any line containing macaddr=
  • deleting empty lines
  • and removing trailing whitespace

You can find the documentation at http://man7.org/linux/man-pages/man1/sed.1.html

Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
  • 1
    I'd add that `\( \)` around the `-name` option doesn't do anything and could be skipped; and I wouldn't say "delete lines containing `^#` or `^nocrc=`", but "delete lines that *begin* with `#` or `nocrc=`. – Benjamin W. Jun 25 '18 at 02:03
  • @BenjaminW. Yikes, I wasn't focused on what was being deleted but of course you are completely right - that ^ means something important. – Jerry Jeremiah Jun 25 '18 at 02:31