WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Shell challenge  (Read 1435 times)

Offline ost

  • Newbie
  • *
  • Posts: 41
Shell challenge
« on: September 29, 2011, 05:53:31 AM »
This is a general shell question, but since Ive subscribed to this forum I may just ask here.. I hope you don't mind.
Im trying to grab a 2 line output by piping it to two read commands like this:
echo -e "1\n2"|(read l1;read l2)

Now, the l1 and l2 variable doesnt survive the end bracket (sub shell?). What's the easiest way to capture these and pass it to the main shell?

tests:
echo -e "1\n2"|(read l1;read l2;echo -e "$l1\n$l2");#works
echo -e "1\n2"|(read l1;read l2);echo -e "$l1\n$l2";#works not


Offline ost

  • Newbie
  • *
  • Posts: 41
Re: Shell challenge
« Reply #1 on: September 29, 2011, 06:12:01 AM »
I could add that this is supported in a shell that takes arrays:
line=(`echo -e "1\n2"`)

result:
line=([0]="1" [1]="2")

But microcore's shell doesnt seem to support that.

Offline SvOlli

  • Full Member
  • ***
  • Posts: 193
  • Linux Developer
Re: Shell challenge
« Reply #2 on: September 29, 2011, 07:02:20 AM »
Not nice, but working:
Code: [Select]
eval $(echo -e "1\n2"|(read l1;read l2;echo "l1=\"$l1\" l2=\"$l2\""))To come up with a more sophisticated solution, I need a bigger context.

Offline ost

  • Newbie
  • *
  • Posts: 41
Re: Shell challenge
« Reply #3 on: September 29, 2011, 08:13:09 AM »
Not nice, but working:

I find it nice and not nice at the same time ;) Thanks :)