1 |
JD Gray wrote: |
2 |
|
3 |
> I'm running the below script on my gentoo servers to email me whenever |
4 |
> there are GLSA's affecting me. It works like a charm, but I have one |
5 |
> beef with it. Newlines are not preserved, so I get a lovely Wall Of |
6 |
> Text (tm) when ever it sends me the GLSA. I'm guessing this is because |
7 |
> of the way bash handles variables. Anyone have any insight on how to |
8 |
> correct this? |
9 |
> |
10 |
> #!/bin/bash |
11 |
> |
12 |
> /usr/bin/glsa-check -t all &> /dev/null |
13 |
> CHECK_RESULT="`/usr/bin/glsa-check -t all 2>&1`" |
14 |
> |
15 |
> if [ "$CHECK_RESULT" != "" ]; then |
16 |
> echo $CHECK_RESULT | /bin/mail -s "Frog glsa-check" kahdgarxi@×××××.com; |
17 |
> fi |
18 |
> |
19 |
Simply quote the variable you are echo'ing: echo "$CHECK_RESULT" |
20 |
What's happening here is called "word-splitting"; bash is sending each part of |
21 |
the string as a separate parameter to echo. See: |
22 |
http://www.bash-hackers.org/wiki/doku.php/syntax/words for more. |
23 |
|
24 |
The best explanation I've seen for why this happens (the one that helps |
25 |
beginners ime) is: |
26 |
"At its base, a shell is simply a macro processor that executes commands. The |
27 |
term macro processor means functionality where text and symbols are expanded |
28 |
to create larger expressions." |
29 |
..from the bash reference manual: |
30 |
http://tiswww.tis.case.edu/~chet/bash/bashref.html which I don't recommend as |
31 |
a beginning read :) but is definitely worth bookmarking. |
32 |
|
33 |
http://www.grymoire.com/Unix/Quote.html shows how to deal with quoting in |
34 |
various situations; make sure you understand the difference between strong |
35 |
(single) quotes and weak (double) quotes. For instance, "Frog glsa-check" |
36 |
doesn't need double-quotes and single-quotes are a bit faster to parse and |
37 |
evaluate (why lose /any/ speed when you don't need to?) Strong quotes can |
38 |
also be really useful for stuff echo'ed to the terminal, eg a help message |
39 |
where you need to include some script the user might look at or use. |
40 |
update[1] does this for the various help options. |
41 |
|
42 |
I've also heard that mutt is a very useful utility if you need to send mail |
43 |
from shellscripts; I haven't worked with it but aiui it makes it easy to add |
44 |
attachments and so on. |
45 |
|
46 |
BTW you can simply use: if [[ $foo ]] to do the check; see ''help test'' in |
47 |
your terminal; -n is the default, and no word-splitting happens inside [[ so |
48 |
quotes are not needed, which is one of the things that makes it much more |
49 |
convenient. |
50 |
|
51 |
http://wooledge.org/mywiki/BashGuide is the best tutorial I've seen on the |
52 |
web; it's written and maintained by people from #bash on irc.freenode.net |
53 |
which is an invaluable resource, if a little rude sometimes ;) |
54 |
The bash hackers wiki mentioned, is also a good place to learn: |
55 |
http://www.bash-hackers.org/wiki/ |
56 |
|
57 |
The FAQ: http://wooledge.org/mywiki/BashFaq is legendary, and this: |
58 |
http://wooledge.org/mywiki/BashPitfalls is worth reviewing, especially once |
59 |
you've got the basics. |
60 |
|
61 |
Minor style point: varnames in caps are usually reserved for environment vars, |
62 |
or constants; within ebuilds they're also used for standard config things, or |
63 |
variables that cross functions or phases. |
64 |
|
65 |
Oh, one other thing: backticks are deprecated, and $(..) is much preferred, |
66 |
eg: foo=$(cmd 'some param' "$blah" 2>"$errFile") since it's easier to nest |
67 |
and maintain. This is true for POSIX sh as well as BASH; see: |
68 |
http://www.opengroup.org/onlinepubs/009695399/utilities/contents.html |
69 |
..for the definitive reference on portability. Sticking to the parameters |
70 |
given there for commands (aka utilities), will do wonders for the portability |
71 |
of your BASH scripts. |
72 |
|
73 |
HTH, |
74 |
steveL (I _love_ BASH, can you tell? ;) |
75 |
|
76 |
[1] http://forums.gentoo.org/viewtopic-t-546828.html | You might like |
77 |
update -A which does the glsa-check for you. I'd love feedback from -server |
78 |
users; update -x is an automated mode which logs to the syslog, designed for |
79 |
use under cron. (It's called server in the code.) |