Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-13-2012
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts
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
Sponsored Links
    #2  
Old 03-13-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,645
Thanks: 164
Thanked 639 Times in 616 Posts
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
Sponsored Links
    #3  
Old 03-14-2012
hedkandi's Avatar
Registered User
 
Join Date: Aug 2008
Location: Doha, Qatar
Posts: 187
Thanks: 27
Thanked 9 Times in 8 Posts
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
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts
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
Sponsored Links
    #5  
Old 03-14-2012
hedkandi's Avatar
Registered User
 
Join Date: Aug 2008
Location: Doha, Qatar
Posts: 187
Thanks: 27
Thanked 9 Times in 8 Posts
You still have to input something to read at the <, otherwise how would you get an output at > ?
Sponsored Links
    #6  
Old 03-14-2012
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts
Yes that's the valid question and that will actually throw syntactic error .
Sponsored Links
    #7  
Old 03-14-2012
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,482
Thanks: 15
Thanked 134 Times in 122 Posts
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 04:50 PM..
The Following User Says Thank You to drl For This Useful Post:
pandeesh (03-14-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Purpose of inv raju4u UNIX for Advanced & Expert Users 1 03-01-2011 07:33 AM
Purpose of /etc/cron.d proactiveaditya UNIX for Dummies Questions & Answers 3 12-12-2009 05:42 AM
What is the purpose of 2 >&1 in crontab? senmak UNIX for Dummies Questions & Answers 4 11-15-2009 02:05 AM
Purpose of dsi log student2010 Filesystems, Disks and Memory 0 05-24-2009 06:21 AM
Purpose of 2>&1 in the command mmunir Shell Programming and Scripting 1 05-27-2008 04:00 AM



All times are GMT -4. The time now is 08:22 PM.