0

Here is my little script... All it does is to iterate through some folders, check for modified files, commit changes to git, but at the same time store output of git messages to be sent via email as a raport:

#!/bin/bash

enviromentName=xyz
serverName=abc
serverSize=1GB

cd /home/$enviromentName/domains/
domainsArray=(*/)
changes=0
message="Usage (max ${serverSize}): $(du -h --max-depth=0 | head -n1 | awk '{print $1;}')\n"
currentDate=$(date +%Y%m%d)

cd /home/$enviromentName/
for i in "${domainsArray[@]}"
do
        cd /home/$enviromentName/domains/$i/public_html/
        gitstatus=$(git status)
        if echo $gitstatus | grep -v -q "nothing to commit, working directory clean"; then
                message+="\n"
                message+="  Git Changes made to ${i}:\n"
                message+="'$gitstatus'\n"
                message+="$(git add .)\n"
                message+="$(git commit -m ${currentDate})\n"
                message+="\n"
                changes=1
        fi
done

if [ "$changes" -eq "1" ]; then
        #echo "Changes done"
        echo -e ${message}|mail -s "${serverName} logs" marcin@citystudio.pl
fi

The output of the script looks this way:

Usage (max 1GB): 0.5G

Git Changes made to foldername/:
'On branch master Untracked files: (use "git add <file>..." to include in what will be committed) test.php nothing added to commit but untracked files present (use "git add" to track)'

[master d4d3916] 20190707 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 public_html/test.file

It looks like all line breaks of git commands output seems to be gone. How should I modify the script so the output looks like this:

Usage (max 1GB): 0.5G

  Git Changes made to foldername/:
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    test.php

no changes added to commit (use "git add" and/or "git commit -a")
[master 86f39b9] 20190707
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 public_html/test.php
Marcin Bobowski
  • 1,745
  • 2
  • 19
  • 35
  • 1
    You need to double-quote variable references; this is just one of the many things that can go wrong when you don't. See [When is double-quoting necessary?](https://stackoverflow.com/questions/55023461/when-should-i-double-quote-a-parameter-expansion) Also, you should check for errors every time you `cd`, or else a failure can make the next bit of the script run in the wrong place. [shellcheck.net](https://www.shellcheck.net) is good at pointing out common mistakes like these. – Gordon Davisson Jul 08 '19 at 06:15
  • 1
    Finally, avoid `echo -e`, since it doesn't work with all versions of `echo` (I once had an OS version upgrade break a bunch of my scripts because I used `echo -n`, which has the same problem). Instead, include literal newlines in the string with ANSI C quoting mode, e.g. `message+=$'\n'` – Gordon Davisson Jul 08 '19 at 06:20

1 Answers1

1

The problem occurs because %{} strips trailing newlines. All you should need to do is change

echo -e ${message}|mail -s "${serverName} logs" marcin@citystudio.pl

to

echo -e "${message}"|mail -s "${serverName} logs" marcin@citystudio.pl

This link provides some good additional information.

Ben W
  • 930
  • 6
  • 16
  • 1
    Right solution, wrong explanation. It's not "stripping" newlines, it's converting all whitespace (normally spaces, tabs, and newlines) into "word" delimiters, passing the resulting "words" to `echo` as arguments, and then `echo` splices its arguments back together with spaces. If any "words" contain anything that looks like a filename wildcard, it'll also try to expand those. – Gordon Davisson Jul 08 '19 at 06:11