[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Why 'exec' has to be used in with fd<>filename? And the
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Why 'exec' has to be used in with fd<>filename? And the difference between <> and >>? |
Date: |
Wed, 12 Feb 2014 08:36:04 -0500 |
User-agent: |
Mutt/1.4.2.3i |
On Tue, Feb 11, 2014 at 05:28:59PM -0600, Peng Yu wrote:
> exec fd<>fileName
> In the above document, I don't see 'exec' should be used. Where is
> this documented? Thanks.
As Chet said, exec is used when you want the shell to open or close
a file descriptor. Think of it as bash's builtin version of open()
and close() with a silly name inherited from the Bourne shell.
> Also, what is the benefit of using exec fd<>filename.
Pretty much the only time I would ever use <> in a script is with a
socket connection. For example:
exec 3<>/dev/tcp/"$hostname"/80 # open
printf >&3 '%s\n' \
'HEAD / HTTP/1.1' \
"Host: $hostname" \
'Connection: close' \
''
cat <&3 # without 'Connection: close' this hangs
exec 3>&- # close
For regular files, bidirectional I/O is very, very rare in shell scripts.