1

The below script works, but what purpose does the declare $line have? If I remove it, it doesn't work.

And what is the difference of {} \; and {} + in the find command?

awk '{print "old="$1" new="$2}' list.txt |\
while IFS= read line; do
    declare $line
    find /path -name '*.ext' -exec sed -i "s/\b$old\b/$new/" {} +
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Jasmine Lognnes
  • 6,597
  • 9
  • 38
  • 58
  • 6
    Which parts of the help blurb and man pages are you having trouble with? – Ignacio Vazquez-Abrams Feb 28 '15 at 14:15
  • 1
    1. You don't really need `awk` and `declare` for that anyway: `while read -r old new _; do find /path -name '*.ext' -exec sed -i "s/\b$old\b/$new/" {} +; done` would do the same. Regarding `declare`: `help declare`. 2. see `man find` for `-exec ... +`, or even the [POSIX ref](http://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html). 3. The script _seems_ to work, but will break if first field contains regex character, or if fields contain slashes, or in some other cases too. – gniourf_gniourf Feb 28 '15 at 14:18
  • @gniourf_gniourf Didn't knew about `help`. Thanks. What does your `_` do? I don't really understand what `{} ;` and `{} +` does. `{}` is the filename, but why is `;` and/or `+` needed? – Jasmine Lognnes Feb 28 '15 at 14:31
  • 1
    `_` is just a throw-away variable name (without it, `new` would be filled with the remaining part of the linẹ—I wanted the command to be equivalent to `awk`'s command, that only considers first and second field). – gniourf_gniourf Feb 28 '15 at 15:01
  • 2
    With `find`'s `-exec`, the `\;` or `+` signals the end of the command. In the `\;` version, for each found file, `find` replaces all occurrences of `{}` by the file name, and executes the command. In the `+` version, note that the `{}` must appear just before the `+`, and that there must be only one occurrence of `{}`; then `find` replaces `{}` by _all_ the found files and executes the command; this is usually more efficient, as the command will be launched only once, with multiple files. It's good to use `+` when it's possible to use it—sometimes it's not possible. – gniourf_gniourf Feb 28 '15 at 15:04
  • To add a comment -- the the `;` was needed even before `find` supported `-exec ... +` as an option. The reason, then, isn't just about selecting between the two behaviors, but about allowing other `find` options to be used _after_ an `exec`. Consider `find . -type f -exec check_invalid '{}' ';' -exec rm -f '{}' ';'` to remove any files that the `check_invalid` command flags; without a terminal character, one couldn't tell whether any future arguments were meant for `find` or for the prior thing being `-exec`'d. – Charles Duffy Mar 02 '15 at 02:32

2 Answers2

6

The declare is setting variables: Your awk command emits contents of the form old=foo new=bar. Running declare old=foo new=bar sets those two variables.

That said, this is a wrong and sloppy way to do this. Instead, use read to directly read the desired fields from the input file and assign to the variables (more on this in BashFAQ #1):

while read -u 3 -r old new _; do
    find /path -name '*.ext' -exec sed -i "s/\b$old\b/$new/" {} +
done 3<list.txt

To make this a bit safer, one can also escape literal content against being treated as regular expressions:

requote() { sed 's/[^^]/[&]/g; s/\^/\\^/g' <<< "$1"; };
substquote() { sed 's/[&/\]/\\&/g' <<< "$1"; }
while read -u 3 -r old new _; do
    find /path -name '*.ext' -exec \
        sed -i "s/\b$(requote "$old")\b/$(substquote "$new")/" {} +
done 3<list.txt

Note that I haven't changed the use of \b, an extension which many implementations of sed won't support. See BashFAQ #21 for alternative approaches to doing literal string substitutions.


For completeness (though this unrelated topic really should have been asked as a separate question -- and could have, in that case, been closed as duplicate, as it's been asked and answered before), allow a quotation from the find man page:

  -exec command {} +
         This  variant  of the -exec action runs the specified command on
         the selected files, but the command line is built  by  appending
         each  selected file name at the end; the total number of invoca‐
         tions of the command will  be  much  less  than  the  number  of
         matched  files.   The command line is built in much the same way
         that xargs builds its command lines.  Only one instance of  `{}'
         is  allowed  within the command.  The command is executed in the
         starting directory.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • I wouldn't have phrased it better! `+1`. – gniourf_gniourf Feb 28 '15 at 16:10
  • Why don't use you stdin? – Jasmine Lognnes Feb 28 '15 at 16:32
  • 2
    @JasmineLognnes, to have a more general answer -- so folks can use commands that eat stdin (like ssh) inside the loop, as opposed to just this one `find` (which, indeed, doesn't). – Charles Duffy Feb 28 '15 at 16:33
  • 1
    Note that you probably don't want to apply `requote()` to the _replacement_ string (you do need escaping there too, but _different_ escaping; you could use `substquote() { sed 's/[&/\]/\\&/g' <<< "$1"; }`). The ingenious `sed` command at the heart of `requote()` has since also been put to good use [here](http://stackoverflow.com/a/29613573/45375). – mklement0 Apr 14 '15 at 22:35
-1

The declare or typeset Builtins, which are exact synonyms, permit modifying the properties of variables. This is a very weak form of the typing available in certain programming languages. The declare command is specific to version 2 or later of Bash. The typeset command also works in Ksh scripts.

kerlinux
  • 79
  • 1
  • 4
  • 2
    What you say is more or less accurate, but is uncorroborated by a link to further explanations. You have not explained how what you describe applies to the script in the question, though. You have also not explained the second part of the question about the `{} +` notation. – Jonathan Leffler Feb 28 '15 at 16:02