Using read to read from and Env var

Using read to read from and Env var

Post by jwpurpl » Mon, 23 Nov 1998 04:00:00



set $(<myfile)

This works in ksh, in bash you might have to do

set ${cat myfile}

This will set the positional parameters of the shell ($1, $2, ...) and you
can proceed to do

Alias=$1
Unit1=$2
Unit2=$3


>L'm using Linux 2.1 with Bash and I'm trying to take a line from cut
>which will return several words. Each word needs to be put into a
>shell variable. Is there a way to do something like this:

>#LIST="${HOME}/.x10alias"
>#UNIT=`cat ${LIST}|grep ^${1}|cut -f2`

>UNIT="A B C" # This would what be cut would have returned (Example)

>echo ${Unit} | read ${Alias} ${Unit1} ${Unit2}

>I know I could use a shift but I am curious if I can use read for
>soemthing like this (the above fails of course).

>--


 
 
 

Using read to read from and Env var

Post by Neil Cher » Tue, 24 Nov 1998 04:00:00


L'm using Linux 2.1 with Bash and I'm trying to take a line from cut
which will return several words. Each word needs to be put into a
shell variable. Is there a way to do something like this:

#LIST="${HOME}/.x10alias"
#UNIT=`cat ${LIST}|grep ^${1}|cut -f2`

UNIT="A B C"  # This would what be cut would have returned (Example)

echo ${Unit} | read ${Alias} ${Unit1} ${Unit2}

I know I could use a shift but I am curious if I can use read for
soemthing like this (the above fails of course).

--


 
 
 

Using read to read from and Env var

Post by Neil Cher » Tue, 24 Nov 1998 04:00:00



>set $(<myfile)

>This works in ksh, in bash you might have to do

>set ${cat myfile}

>This will set the positional parameters of the shell ($1, $2, ...) and you
>can proceed to do

>Alias=$1
>Unit1=$2
>Unit2=$3

Not exactly what I was looking for but better :-) , Thank you.

--

 
 
 

Using read to read from and Env var

Post by Niall Smar » Wed, 25 Nov 1998 04:00:00


Quote:

> echo ${Unit} | read ${Alias} ${Unit1} ${Unit2}

> I know I could use a shift but I am curious if I can use read for
> soemthing like this (the above fails of course).

Hmm, why do you say "of course"?  It works on zsh 3.1.5, but not
on Solaris 2.5.1 /bin/sh or bash 2.00.0(1)-release.  I would have
expected it to work everywhere, maybe someone else can shed
some light on this.

Niall

 
 
 

Using read to read from and Env var

Post by Icarus Spar » Wed, 25 Nov 1998 04:00:00



Quote:

>> echo ${Unit} | read ${Alias} ${Unit1} ${Unit2}

>> I know I could use a shift but I am curious if I can use read for
>> soemthing like this (the above fails of course).

>Hmm, why do you say "of course"?  It works on zsh 3.1.5, but not
>on Solaris 2.5.1 /bin/sh or bash 2.00.0(1)-release.  I would have
>expected it to work everywhere, maybe someone else can shed
>some light on this.

This is question 28 from the BASH FAQ.

This is because when the shell sees a '|' it forks. So the 'read' is
done in a subshell, which instantly exits. A workaround is

echo ${Unit} | while read Alias Unit1 Unit2
do
        ... rest of script
        exit
done

Icarus

 
 
 

Using read to read from and Env var

Post by Neil Cher » Wed, 25 Nov 1998 04:00:00



>> echo ${Unit} | read ${Alias} ${Unit1} ${Unit2}

>> I know I could use a shift but I am curious if I can use read for
>> soemthing like this (the above fails of course).

>Hmm, why do you say "of course"?  It works on zsh 3.1.5, but not
>on Solaris 2.5.1 /bin/sh or bash 2.00.0(1)-release.  I would have
>expected it to work everywhere, maybe someone else can shed
>some light on this.

What I meant by "of course" was that I tried it and it failed. I have
used redirection into read in a while loop but I can not find it now
to see how i did it.  I think I redirected a file into the loop.

--

 
 
 

Using read to read from and Env var

Post by Niall Smar » Wed, 25 Nov 1998 04:00:00


Quote:

> >> echo ${Unit} | read ${Alias} ${Unit1} ${Unit2}

> >Hmm, why do you say "of course"?  It works on zsh 3.1.5, but not
> >on Solaris 2.5.1 /bin/sh or bash 2.00.0(1)-release.  I would have
> >expected it to work everywhere, maybe someone else can shed
> >some light on this.

> This is question 28 from the BASH FAQ.

> This is because when the shell sees a '|' it forks. So the 'read' is
> done in a subshell, which instantly exits. A workaround is

I had considered this, but then thought "nah, that would be silly" :)
Surely the whole point of builtins is that they shouldn't cause a
fork.  Interestingly zsh does The Right Thing.

Quote:> echo ${Unit} | while read Alias Unit1 Unit2
> do
>         ... rest of script
>         exit
> done

What makes you think the while won't cause a fork?  It actually does.

Perhaps:

eval `echo $foo | (set -a; read x y; env | egrep '^[xy]=')`

Or (ksh):

eval read Alias Unit1 Unit2 \< <(echo $unit)

Regards,

Niall

 
 
 

Using read to read from and Env var

Post by chris ulri » Mon, 30 Nov 1998 04:00:00




]>>
]>> echo ${Unit} | read ${Alias} ${Unit1} ${Unit2}
]>>
]>> I know I could use a shift but I am curious if I can use read for
]>> soemthing like this (the above fails of course).
]>
]>
]>Hmm, why do you say "of course"?  It works on zsh 3.1.5, but not
]>on Solaris 2.5.1 /bin/sh or bash 2.00.0(1)-release.  I would have
]>expected it to work everywhere, maybe someone else can shed
]>some light on this.
]
]This is question 28 from the BASH FAQ.
]
]This is because when the shell sees a '|' it forks. So the 'read' is
]done in a subshell, which instantly exits. A workaround is
]
]echo ${Unit} | while read Alias Unit1 Unit2
]do
]       ... rest of script
]       exit
]done
]
]Icarus

  Another option is to wrap everything in curly braces
echo $stuff | { read variable another still more
  stuff
  }

  Also, I don't think the shell actually forks, I think it is
trying to be compatible with the original /bin/sh which *did*
fork.  I think now, it just pretends to fork.

  Is there a posix specified behavior?  Given that both ksh
and bash *say* they are posix compliant, perhaps not, but this
is an important issue.
chris

 
 
 

1. Reading in ENV vars from a text file and exporting the value

hi, I have a text (text.ini) file of the following form:
AGE=19
ADDRESS=anywhere us
COMPUTER=windows 98
DRIVE=$SystemDrive  

$SystemDrive is an env var.
I'm reading in text.ini and exporting the values in there.
while read lines
  do
   export "$lines"
done < text.ini

the problem is that my $SystemDrive env is getting lost.
if I echo $DRIVE, the output is $SystemDrive and not C:

Any gurus out there know how I fix this?

2. questions about the sound mixer

3. How to read created env var from a spawned shell?

4. Grounding issues

5. reading env var in Solaris 7

6. Linear buffer in XFree86

7. Referencing an env var from an env var

8. Disable Multiple Mode

9. I need to read directly from hard disk device (really need to read /var partition)

10. while read VAR VAL; do setenv $VAR $VAL; done

11. grep on '$VAR ' (read: $VAR with a SPACE " " after it)

12. Problem reading from pipe ("|") using read()

13. LILO answers HERE read read read