0

I'm getting an issue with this code below, set -x is telling me that the variables ARE being assigned, but trying to echo them outside of this loop does not seem to be working?

          export "ex_$x"=$(git rev-parse HEAD | cut -c1-10)

      done
    ((used++))

    echo $ex_render
    echo $ex_storage

    exit # =/

    php -f "${cdir}/../public/bootstrap.php" -- "${line}" "${ex_render}" "${ex_storage}"
ehime
  • 8,025
  • 14
  • 51
  • 110
  • 2
    Can you post the rest of your script? It's possible that you while loop is [accidentally creating a subshell](http://stackoverflow.com/questions/13726764/bash-script-while-loop-subshell-dilemma), at which point the body of the loop is running in a different environment with a different set of variables. – Jeff Bowman Oct 29 '13 at 16:49
  • Use an array, not dynamically generated variable names. – chepner Oct 29 '13 at 17:05
  • @chepner good advice iff he has bash 4. – kojiro Oct 29 '13 at 17:07
  • It's tagged as such, but it's not even clear he needs an associative array, or that the names of the variables matter (they appear to be passed as arguments to the PHP script, not inherited via the environment). – chepner Oct 29 '13 at 17:08
  • @JeffBowman it was, I have a full dump on pastebin at http://pastebin.com/LunQcxgq – ehime Oct 29 '13 at 17:19
  • @chepner, i did not know if I could PASS an assoc arrray to php, that would be an ideal situation if I could but I'm not certain what goes into that. – ehime Oct 29 '13 at 17:20

1 Answers1

2

It looks like your code got truncated, but it sounds like the classic pipe-to-read problem.

$ echo hi | read x
$ echo $x
$ # Nothing!
$ read x <<< hi
$ echo $x
hi

Basically, a pipe creates an implicit subshell. To avoid it, either avoid the pipe:

while read foo; do things; done < <(process substitution)

Or explicitly create the subshell so you can control the scope:

inputcommand | ( while read foo; do things; done;
  # variables still assigned as long as you're in the subshell
)
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • That's correct I AM piping in, let me test this really quick to make sure it works before accepting – ehime Oct 29 '13 at 16:54
  • worked perfectly, +1 and accepted. Thank you. Note: I used your second with the pipe since I am doing `echo -e "render\nstorage" | ( while read x; do export ex_$x='foo'; done;` – ehime Oct 29 '13 at 17:02
  • @ehime It's still not really necessary. You could just do `<<< $'render\nstorage' while read …` and have no pipe. – kojiro Oct 29 '13 at 17:09
  • 1
    This looks like an unnecessary use of a loop. `ex_render='foo'; ex_storage='foo'` is clearer and less error prone. – chepner Oct 29 '13 at 17:10
  • The loop does a lot of other things, this is just the portion that was a problem, for everyones sanity I shortened it to the only points that matter. – ehime Oct 29 '13 at 17:13
  • @kojiro using `while read x <<< $'render\nstorage'; do` just gives me an infinite loop, let me link all my code in this block and you can see whats going on, maybe you can shorten my case statement since there's only one switch difference while you're at it? – ehime Oct 29 '13 at 17:15
  • 1
    @ehime you just put the herestring in the wrong place. You can put it before the `while` keyword or after the `done` one. – kojiro Oct 29 '13 at 17:20
  • @kojiro +1 again, you are correct, I switched to that instead, updating my pastebin – ehime Oct 29 '13 at 17:23