0

The shellscript I am trying to write is to

  1. download a tarball and stdout to tar to uncompress it
  2. at the same time parse the http headers found at stderr
  3. 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

  1. pipe stdout to a command, also pipe stderr to another command
  2. parse the stderr and set the output as a variable
  3. 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?

Community
  • 1
  • 1
est
  • 11,429
  • 14
  • 70
  • 118

1 Answers1

1

damn it I have to trial & error to find the ugly answer myself:

rev=$(wget -Sq -O- $url 2> >( 
    grep "Content-Disposition:" | 
    tail -1 | 
    awk 'match($0, /filename=.+\-([a-zA-Z0-9]+)\./, f){ print f[1] }'
    ) > >(
    tar -C $www_dir --strip=1 -zmxf -
    ))
est
  • 11,429
  • 14
  • 70
  • 118