Purpose of <>


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Purpose of <>
# 8  
Old 03-14-2012
Quote:
Originally Posted by pandeesh
Code:
Some commands <> filename

If that were even valid, what would it accomplish? The instant you open a file with >, you truncate it, leaving nothing to read.

I have used similar syntax used in bash to open device files:

Code:
exec 5<>/dev/ttyUSB1

...to open a serial port for instance. Then I could echo asdf >&5 and read <&5 without reopening and re-closing the serial port every call. But I believe that's BASH-only syntax, and only useful in very specific circumstances.

Last edited by Corona688; 03-14-2012 at 01:13 PM..
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 03-14-2012
Hi.

The <> re-direction operator is also available in ksh and zsh.

The serial port example makes it useful ... cheers, drl

---------- Post updated at 12:48 ---------- Previous update was at 12:27 ----------

Hi.

Associating a file descriptor with an existing file does not destroy it:
Code:
#!/usr/bin/env bash

# @(#) s3	Demonstrate <> re-direction operator.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C

echo first time > f
pl " Initial content of file f:"
cat f

pl " Associate descriptor 4 with file f, show file:"
exec 4<>f
cat f
ls -lgG f
read -u 4 line
pe " variable line read from FD 4 as \"$line\""

pl " Write to f:"
echo hi >f
ls -lgG f

pl " Read from f:"
cat <f

exit 0

producing:
Code:
% ./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39

-----
 Initial content of file f:
first time

-----
 Associate descriptor 4 with file f, show file:
first time
-rw-r--r-- 1 11 Mar 14 12:46 f
 variable line read from FD 4 as "first time"

-----
 Write to f:
-rw-r--r-- 1 3 Mar 14 12:46 f

-----
 Read from f:

hi

( I corrected my earlier post. )

Best wishes ... cheers, drl

( edit 1: addition )
This User Gave Thanks to drl For This Post:
# 10  
Old 03-14-2012
Good example, thanks, but it does NOT work as:
Code:
<> filename

only with a file descriptor number and the exec keyword. it is analogous to C:
Code:
fopen("somefile, "r+");

or "a+".

-- opening a file for update.
# 11  
Old 03-14-2012
Hi.

The construct <>filename seems to work with ksh:
Code:
#!/usr/bin/env ksh
#!/usr/bin/env zsh
#!/usr/bin/env bash

# @(#) s4	Demonstrate <> re-direction operator.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C

echo first time > f
pl " Initial content of file f:"
cat f

pl " Associate descriptor with file f, show file:"
# exec 4<>f
<>f
cat f
ls -lgG f
read -u 0 line
pe " variable line read from FD as \"$line\""

pl " Write to f:"
echo hi >f
ls -lgG f

pl " Read from f:"
cat <f

exit 0

producing:
Code:
./s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.32-5-686, i686
Distribution        : Debian GNU/Linux 6.0.3 (squeeze) 
ksh 93s+

-----
 Initial content of file f:
first time

-----
 Associate descriptor with file f, show file:
first time
-rw-r----- 1 11 Mar 14 16:00 f
xx
 variable line read from FD as "xx"

-----
 Write to f:
-rw-r----- 1 3 Mar 14 16:00 f

-----
 Read from f:
hi

I needed to enter something from the keyboard for the read -u. So the intent seems OK, but perhaps a problem with bash ... cheers, dr;
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

What is the Purpose of /dev/ipldevice?

Hi, Anyone, please explain the purpose of /dev/ipldevice in AIX .. it would be a problem if there is no /dev/ipldevice while booting. Regards, Siva (1 Reply)
Discussion started by: ksgnathan
1 Replies

2. Solaris

What is the purpose of Bind on Solaris 10?

I'm new to Solaris and Linux and I was wondering if someone could explain to me in simple terms what the process Bind is on Solaris 10? Thanks, in advance. (6 Replies)
Discussion started by: jastanle84
6 Replies

3. UNIX for Dummies Questions & Answers

exact purpose of links

please explain what is the exact purpose of hard link and soft link which is best one thanks in advance regards, surendra thota (3 Replies)
Discussion started by: tsurendra
3 Replies

4. UNIX for Advanced & Expert Users

Purpose of inv

Hi All Can anybody tell me what is the purpose of inv in the below command. ftp -inv $RFTPSERVER /temp/te.txt << EOF and << its stands for what.. Thanks (1 Reply)
Discussion started by: raju4u
1 Replies

5. UNIX for Dummies Questions & Answers

Purpose of /etc/cron.d

What is the purpose of /etc/cron.d? (3 Replies)
Discussion started by: proactiveaditya
3 Replies

6. UNIX for Dummies Questions & Answers

What is the purpose of 2 >&1 in crontab?

while we editing the cron at the end of the cron what is the purpose of giving 2 >&1 (4 Replies)
Discussion started by: senmak
4 Replies

7. Filesystems, Disks and Memory

Purpose of dsi log

Hi Please explain what is dsi log? Does it stores details related to File systems? (0 Replies)
Discussion started by: student2010
0 Replies

8. Shell Programming and Scripting

Purpose of 2>&1 in the command

Can any body kindly tell me what is the purpose of 2>&1 in the following commands. nohup ./append_import.sh 1 > import1.out 2>&1 < /dev/null & nohup ./append_import.sh 2 > import2.out 2>&1 < /dev/null & (1 Reply)
Discussion started by: mmunir
1 Replies

9. UNIX for Dummies Questions & Answers

whats the purpose of the following script?

whats the purpose of the following script? who could run it? To what is the script refering that exceeds 75%? The mailbox? What does sed 's/%//' do? (1 Reply)
Discussion started by: vrn
1 Replies

10. UNIX for Dummies Questions & Answers

Purpose of lint in UNIX

Can Any One let me know abut the use on "lint" in UNIX...... (1 Reply)
Discussion started by: kumar_saurabh
1 Replies
Login or Register to Ask a Question