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?
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
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 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 )
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)
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)
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)
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)
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)