|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
You still have to input something to read at the <, otherwise how would you get an output at > ?
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
Yes that's the valid question and that will actually throw syntactic error .
|
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
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 0producing: 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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|