Combining output of multiple commands and posting to a file

Hi, I have a requirement where I want to post the output of a git command in a particular format to a file using the sh command .

here is the git command: git rev-parse --short HEAD, which gives the output c5678u.

FINAL OUTPUT:
the final output posted to the file should look like, where return is also a text :

return "c5678u"

how can i do this using the sh command?

Welcome to the forums.

We encourage users to post their own attempts first.
Have you tried anything ?

Regards
Peasant.

1 Like

Actually i did try several times and I was partially able to make it work but I am stuck at the qoutes thing :

 sh 'echo return > file.txt; git rev-parse --short HEAD >> fiile.txt; echo >> file.txt'

but this produces the output like:
return
c5678u

but I want to produce it like return "c5678u".
I tried escaping the qoutes, but whenever I do that the command doesnot work anymore. Any ideas why?

I tried doing this:

echo -ne return "\"" > file.txt ; echo -ne git rev-parse --short HEAD >> file.txt;  echo -ne "\"" >> filetxt

This works through the command line but does not run through my sh command in my script. Any ideas on why this is not working?

how about:

printf  'return "%s"\n'  "$(git rev-parse --short HEAD)"

awesome @vgersh99 this was really helpful. thank you so much.
return is just a string in this case, I tried this : sh "printf return "%s" ${git rev-parse --short HEAD} > file.txt"
but this produces return " " ${git rev-parse --short HEAD} output in the file, rather than printing the value of the git command. Any idea of what I might be doing wrong?

I guess you are in a shell already so you do not need to start another shell; simply redirect to the file.

printf  'return "%s"\n'  "$(git rev-parse --short HEAD)" > file.txt

Here is how you can redirect a group of commands to a file:

{ echo -n return \"; git rev-parse --short HEAD; echo -n \"; } > filetxt
1 Like

@MadeInGermany hey I tried the echi command as well but since it is already running in the shell, it always shows up like this in the text file:
-n return ; git rev-parse --short HEAD; echo -n

First, please start using markdown-s to properly format your code/data samples.

Secondly, this is not what's suggested. Please pay attention to exactly what was suggested.

As indicated by @MadeInGermany you don't need to redirect anything to any file.

And you need to follow the suggested solution - pay attention to single/double quotes and the placement of ():

printf  'return "%s"\n'  "$(git rev-parse --short HEAD)"
3 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.