Purpose of <>


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Purpose of <>
# 1  
Old 03-14-2012
Purpose of <>

Hi,

I have read from the book that , <> causes the file to be used as both input as well as output. Can anyone give me the scenario where <> will be useful?

Thanks
# 2  
Old 03-14-2012
No, it provides redirection for stdin and stdout for any set of two files, not using the same file as you describe.
take take a file with a single column of text and put a line number after the column
Code:
#/bin/bash
cnt=1
while read rec
do
   echo "$rec $cnt"
   cnt=$((  $cnt + 1  ))
done < inputfile > newoutputfile

you can also use < somefile >> someotheroldfile to append to the outputfile
# 3  
Old 03-14-2012
I use it with script to pipe all my wtmp into a text file which I then email to the customer for auditing

Code:
# ./wtmp-report.pl < /var/log/wtmp > wtmp.txt

# ls -lrt | grep *txt
-rw-r--r-- 1 root root  30735 Mar 14 14:54 wtmp.txt

# 4  
Old 03-14-2012
The examples what you gave are fine . Even I used the same. Previously .
What I wonder from the book is , is it possible to use like below

Code:
Some commands <> filename

But practically nowhere that is required
# 5  
Old 03-14-2012
You still have to input something to read at the <, otherwise how would you get an output at > ?
# 6  
Old 03-14-2012
Yes that's the valid question and that will actually throw syntactic error .
# 7  
Old 03-14-2012
Hi.

The purpose is as noted, opens for both input and output:
Code:
Opening File Descriptors for Reading and Writing
The redirection operator

[n]<>word

causes the file whose name is the expansion of word to be opened
for both reading and writing on file descriptor n, or on file
descriptor 0 if n is not specified.  If the file does not exist,
it is created. 

-- excerpt ffom man bash

I don't recall an instance when I needed such a construct, however, it is syntactically acceptable:
Code:
#!/usr/bin/env bash

# @(#) s1	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

rm -f f
pl " Create descriptor 4 and file f, show file:"
exec 4<>f
ls -lgG f

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

pl " Read from f:"
cat <f

exit 0

producing:
Code:
% ./s1

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

-----
 Create descriptor 4 and file f, show file:
-rw-r--r-- 1 0 Mar 14 12:43 f

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

-----
 Read from f:
hi

It may be useful for writing on stdin, but I don't see the point of that.

Perhaps someone will describe a useful situation ... cheers, drl

( edit 1: corrected for exec mis-typed as echo )
( edit 2: misspelling )

Last edited by drl; 03-14-2012 at 05:50 PM..
This User Gave Thanks to drl For This Post:
 
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