1

Is it possible to do an "git status" and output the result into an echo? Or send the output as email?

I guess the email-thing is no problem but I stuck doing an echo.

What I got

clear 
output="$(git status)"
echo output

But ... yeah, it won't work and I searched certain examples but they lead always ino a git status if,. and i need the output :( Is there way simple way to get this done

And, how to handle if this should be called on a remote system:

ssh ${SSHUSER}@${LIVE_HOST} << EOF
...
EOF
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
TheTom
  • 934
  • 2
  • 14
  • 40

3 Answers3

3

The echo is useless; all you need is

git status

If you genuinely need to store the output in a variable as well as print it, try

output=$(git status)
echo "$output"

To run it remotely, you don't need a here document at all;

ssh "${SSHUSER}@${LIVE_HOST}" git status

and again, if you need to store that in a variable, that would be

output=$(ssh "${SSHUSER}@${LIVE_HOST}" git status)
echo "$output"

If you really want to store the command in a here document, that's possible too:

ssh "${SSHUSER}@${LIVE_HOST}" <<\:
git status
:

or in Bash you could use a "here string":

ssh "${SSHUSER}@${LIVE_HOST}" <<<'git status'

If you want to send the result as email, a common arrangement is

git status | mail -s "Status report from xyz" you@example.com

but the mail command is poorly standardized, so you may have to consult a manual page or Google for a bit.

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

An echo "$output" would work better

echo ouput would just print output.

I just tried:

$ o=$(git status 2>&1); echo "$o"
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
tripleee
  • 175,061
  • 34
  • 275
  • 318
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Ah yes, ok i see my fault. So, and If this should happen on a remote server, do I have to set it up like theese? ssh ${SSHUSER}@${LIVE_HOST} << EOF git status EOF ? – TheTom May 31 '21 at 10:04
  • @TheTom only 8f your remote repository is on $HOME, which it shouldn't be. `git -C /path/to/rep status` would work. – VonC May 31 '21 at 11:07
0

It does not working because you missed the variables syntax.

Rewrite your code as follow:

clear 
output="$(git status)"
echo "$output"
tripleee
  • 175,061
  • 34
  • 275
  • 318
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • Yes, I mentioned that – VonC May 31 '21 at 08:42
  • And, reading from https://stackoverflow.com/questions/613572/capturing-multiple-line-output-into-a-bash-variable/613580, you should do `echo "$output"` to get the newlines correctly displayed. – VonC May 31 '21 at 08:43
  • Clearing the screen as part of your script is doing your users a disservice; I would take that out. – tripleee May 31 '21 at 10:23