The shellscript I am trying to write is to
- download a tarball and stdout to
tar
to uncompress it - at the same time parse the http headers found at stderr
- set the parsed result at #2 to a variable.
For #1:
$ wget -Sq -O- "https://api.github.com/repos/est/est/tarball/master" | tar -zmxf -
I enabled the -S
option, which shows server response to stderr, I want to parse the header a bit for the string commit hash , and set it to a variable called $rev
$ rev=$(wget -Sq -O /dev/null "https://api.github.com/repos/est/est/tarball/master" 2>&1 | grep "Content-Disposition:" | tail -1 | awk 'match($0, /filename=.+\-([a-zA-Z0-9]+)\./, f){ print f[1] }')
$ echo $rev
dacd56e
So basically, I want a shellscript that could
- pipe stdout to a command, also pipe stderr to another command
- parse the stderr and set the output as a variable
- do not create temporary files
For #1 https://stackoverflow.com/a/9217228/41948
For #2 I found the answer is the read
command, but unfortunately the variable can be only found at a subshell.
So how do I pass the variable found at read
command subshell to the parent shell?
Or how do I write a shellscript like this?