It is currently Tue, 07 Dec 2021 03:12:46 GMT



 
Author Message
 sh: read vars from file
In article <45bccf$...@ae-sun01.alden.com>,
exec 3<&0
exec <$infile;read vars1;read vars2;read vars3;read vars4;cat >/tmp/temp.$$
exec <&3

--
"Goodness me, could this be industrial disease?"  Dire Straits



 Fri, 27 Mar 1998 03:00:00 GMT   
 sh: read vars from file
Hi.

I have files which contain 4 lines to be read into variables
(used for e-mail) and then the rest to be processed as a mail message.
(Part of large Bourne shell script.)

The following works but I consider it highly inelegant  :-)  :
...
vars1="`awk 'NR==1' $infile`"
vars2="`awk 'NR==2' $infile`"
vars3="`awk 'NR==3' $infile`"
vars4="`awk 'NR==4' $infile`"
awk 'NR>4' $infile > /tmp/temp.$$
...

I have a feeling there's a way to use file descriptor 3 and read
the file only once, but my attempts to do so have failed.
Any ideas?

Thanks in advance,
Stan.
--
HELP!  I'M BEING HELD PRISONER IN A .SIGNATURE FACTORY!
    Stan Ryckman   s...@alden.com



 Fri, 27 Mar 1998 03:00:00 GMT   
 sh: read vars from file
In article <45bccf$...@ae-sun01.alden.com>,
: I have files which contain 4 lines to be read into variables
: (used for e-mail) and then the rest to be processed as a mail message.
: (Part of large Bourne shell script.)
:
: The following works but I consider it highly inelegant  :-)  :
: ....
: vars1="`awk 'NR==1' $infile`"
: vars2="`awk 'NR==2' $infile`"
: vars3="`awk 'NR==3' $infile`"
: vars4="`awk 'NR==4' $infile`"
: awk 'NR>4' $infile > /tmp/temp.$$
: ....
:
: I have a feeling there's a way to use file descriptor 3 and read
: the file only once, but my attempts to do so have failed.
: Any ideas?

Don't even bother with file descripter 3.

{ read vars1
  read vars2
  read vars3
  read vars4
  cat > /tmp/temp.$$ } < $infile

--Dave (and if you worked hard enough at it, you could probably get rid
        of your temp file as well)
--
http://armf18.dow.on.doe.ca:6700/~dbrown/ Dave Brown, rogue news admin

"Please stop posting the same post in the linked effect all over the internet."
                               --some AOL user on crossposting



 Fri, 27 Mar 1998 03:00:00 GMT   
 sh: read vars from file

 > In article <45bccf$...@ae-sun01.alden.com>,
 > Stan Ryckman <s...@ae-sun01.alden.com> wrote:
 > : I have files which contain 4 lines to be read into variables
 > : (used for e-mail) and then the rest to be processed as a mail message.
 > : (Part of large Bourne shell script.)
 > :
 > : The following works but I consider it highly inelegant  :-)  :
 > : ....
 > : vars1="`awk 'NR==1' $infile`"
 > : vars2="`awk 'NR==2' $infile`"
 > : vars3="`awk 'NR==3' $infile`"
 > : vars4="`awk 'NR==4' $infile`"
 > : awk 'NR>4' $infile > /tmp/temp.$$
 > : ....
 > :
 > : I have a feeling there's a way to use file descriptor 3 and read
 > : the file only once, but my attempts to do so have failed.
 > : Any ideas?

 > Don't even bother with file descripter 3.

 > { read vars1
 >   read vars2
 >   read vars3
 >   read vars4
 >   cat > /tmp/temp.$$ } < $infile

This works with ksh, but not with standard Bourne Shell.

The following works with both shells:

    # Save old stdin file descriptor 0 to fd 3, redirect fd 0 from $infile
    exec 3<&0 0<$infile
    read vars1
    read vars2
    read vars3
    read vars4
    exec 0<&3 3<&-        # Restore stdin, close fd 3

    # Print lines starting with line 5 to temp. file
    sed -n '5,$p' < $infile > /tmp/temp.$$

Heiner
--
   ------------------------------------------------------------------
  /  Heiner Steven, Nuernberg          hei...@hsysnbg.nbg.sub.org  /
 /   World, Europe, Germany, Nuernberg               h...@bintec.de /
------------------------------------------------------------------
:q!ZZ^X^Cquit^Mexit^J^D^Cbye^Mlogout^M^?^\xxxxalles kacke



 Mon, 30 Mar 1998 03:00:00 GMT   
 
   [ 4 post ] 

Similar Threads

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

2. Reading and passing vars from a file.

3. environment vars in sh script

4. Length of sh or ksh environmental vars

5. Maintaining white space in vars (/bin/sh)

6. sh vars in awk scripts??

7. using /bin/sh vars in awk

8. SUMMARY: using sh vars in awk calls.

9. reading complete line from file in sh

10. sh - reading/closing/opening files


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software