6

This example was in a Linux book:

$ cat sort-wc
#!/bin/bash
# Sort files according to their line count
for f
do
    echo `wc -l <"$f» lines in $f
done | sort -n
$ ./sort-wc /etc/passwd /ect/fstab /etc/motd

What I don't get is why there is only a single backtick, a single double quote and what the >> does. Isn't >> for writing to a file?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

1 Answers1

27

This is from page 121 of "Introduction to Linux for Users and Administrators" and that's a typographical error in the text. The script is also avaliable in other texts from tuxcademy, with the same typographical error.

The single » character is not the same as the double >> and it serves no purpose in a shell script. My guess is that the typesetting system used for formatting the text of the book got confused by "` for some reason and formatted it as a guillemet (angle-quote), or it's just a plain typo (the «...» quotes are used for quoting ordinary text elsewhere in the document).

The script should read

#!/bin/bash
# Sort files according to their line count
for f
do
    echo `wc -l <"$f"` lines in $f
done | sort -n

... but would be better written

#!/bin/sh
# Sort files according to their line count

for f; do
    printf '%d lines in %s\n' "$(wc -l <"$f")" "$f"
done | sort -n

The backticks are an older form of $( ... ), and printf is better to use for outputting variable data. Also, variable expansions and command substitutions should be quoted, and the script uses no bash features so it could just as well be executed by /bin/sh.

Related:

Kusalananda
  • 333,661
  • Wow thank you, yes that's exactly where this example is from. I'll need to comb over your better second resolution because that confusing me more, I'm an extreme beginner at shell scripting :) – user284179 Apr 03 '18 at 18:53
  • @user284179 Once you figure out how printf works (see man 1 printf, it takes a format string followed by other arguments to substitute into the placeholders in the format string), it will be clear. – Kusalananda Apr 03 '18 at 19:13
  • Your guess about the typesetting error is very plausible. The quote used to be an active character in German LaTeX packages to be able to type ä as “a. With the german package, the combination in question should produce German opening quotation marks, but maybe the author used a variant that gives Swiss quotation marks. – Carsten S Apr 03 '18 at 21:43
  • I think I've seen /bin/sh implementations which support backticks but not $(...), so beware of portability issues. – u1686_grawity Apr 03 '18 at 21:57
  • Fun fact: https://github.com/nyucel/linuxessentials contains a TEX copy of this book — in, apparently, Turkish — posted by Necdet Yucel.  As I mentioned, the text is apparently in Turkish (as identified by Google Translate), except the Preface and the examples are in English. https://github.com/nyucel/linuxessentials/blob/master/bolum9.tex is Chapter 9, which contains the example this question is about — but it gets it right: echo \wc -l <"$f"` lines in $f` (see lines 435-444). – Scott - Слава Україні Apr 04 '18 at 03:39
  • 2
    @grawity If you have a system with a non-POSIX /bin/sh, then it's so old that you should not be doing development work on it. If it's a production system, it should seriously be considered for replacing as it would potentially be insecure if connected to the internet. – Kusalananda Apr 04 '18 at 05:50
  • guillemets are used in a number of languages for quoting, but not english or german. As the answer says, this is probably an example of slightly sloppy typesetting (using automatic smart quotes + not properly declaring a code environment probably). – Cubic Apr 04 '18 at 10:48