使用bash脚本存储git命令的输出并通过电子邮件发送

时间:2019-07-07 18:02:40

标签: linux bash git email

这是我的小脚本...它的全部作用是循环访问某些文件夹,检查是否有修改的文件,将更改提交到git,但是同时存储要通过电子邮件作为邮件发送的git消息的输出:

#!/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

脚本的输出看起来像这样:

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

看起来git命令输出的所有换行符似乎都消失了。我应该如何修改脚本,以使输出看起来像这样:

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

1 个答案:

答案 0 :(得分:0)

发生问题是因为%{}删除了结尾的换行符。您需要做的就是改变

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

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

This链接提供了一些很好的附加信息。