Not able to understand use exec command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not able to understand use exec command
# 1  
Old 01-28-2013
Not able to understand use exec command

Hi

i am in learning phase of unix.
i was going through exec in a unix book. below is the command
Code:
exec n>file
exec n>>file

however when i used the exec command like below , where ex is the file name
Code:
exec 2>>ex

and then do
Code:
 ls -lrt

then again when i do the
Code:
ls -lrt

to see the size of the file ex but what i see is it again appending the o/p of the
Code:
 ls -lrt

to the file ex.

from this what i understand is after running the exec command the control is not coming out the exec command.

below is the sequence which happened to me .
Code:
 $ exec 1>>ex
$ ls -lrt
$ ls -lrt
$ ls -lrt

it is not printing the o/p on the screen when i run the ls -lrt command second time.

i am just puzzled.

regards,
scriptor

Last edited by scriptor; 01-28-2013 at 08:47 AM.. Reason: typo mistake
# 2  
Old 01-28-2013
That is expected.

Code:
$ exec 1>>ex

You are redirecting (and appending) STDOUT to file ex - hence STDOUT is no longer "connected" to your terminal.
# 3  
Old 01-28-2013
HI tokiwinter,

thx for your prompt response. however i am still not able to understand the working of exec command.
the below command simple overwrite the data
Code:
exec n>file

but this is not happening, instead it is keep on accepting the command and also appending the data also it seems that exec command is not leaving the control. please see below

Code:
$ exec 1>x1
$ ls -lrt
$ ls -lrt
$

this is what written in a book which i am referring. pasting the content below
Code:
By default, the shell provides you with three standard file descriptors for every command. With it, you can
also associate any file with file descriptors using the exec command.
Associating a file with a file description is useful when you need to redirect output or input to a file many
times but you don't want to repeat the filename several times.
To open a file for writing, use one of the following forms:
exec n>file
exec n>>file
Here n is an integer, and file is the name of the file you want to open for writing. The first form overwrites
the specified file if it exists. The second form appends to the specified file. For example, the following
command 
$ exec 4>fd4.out
associates the file fd4.out with the file descriptor 4.
To open a file for reading, you use the following form:
exec n<file
Here n is an integer, and file is the name of the file you want to open for reading


Last edited by scriptor; 01-28-2013 at 09:28 AM.. Reason: adding some additional information
# 4  
Old 01-28-2013
Hi,

This is all expected behaviour.

Also

Code:
$ exec 1>ex

Will overwrite, not append. If you just want to redirect the output of a single run of ls to a file, run

Code:
$ ls -ltr 1>ex

As STDOUT is the default, you don't even need the 1 here.

If you are trying to understand the functionality/purpose of the exec command I'd recommend reading your shell's man page.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Help to understand the command

Hi Gurus, I am new for Unix scripting. below command in one existing script. I am not able to fully understand. please help in below command, I am not able to understand what's {P=1} do thanks in advance awk 'NF==1{$3=$1;$1=L}P&&NF>=3{print $1,$3;L=$1}/^___/{P=1}' FILE (3 Replies)
Discussion started by: green_k
3 Replies

3. UNIX for Dummies Questions & Answers

Not able to understand the output of w command

I have taken putty session of a server from two separate machines namely HOST1(3 sessions) and HOST2(1 Session) . However w command says there are 5 users Confused over the output any clue will be appreciated. # w 09:29:36 up 34 days, 15:48, 5 users, load average: 0.62, 4.33, 8.16 USER ... (3 Replies)
Discussion started by: pinga123
3 Replies

4. Shell Programming and Scripting

Need to understand the output of last command

root@desktop:~# last reboot | head -1 reboot system boot 2.6.31-17-generi Tue Jan 26 12:05 - 13:52 (01:46) What does the last two fields(12:05 - 13:52 (01:46)) of the output mean? (2 Replies)
Discussion started by: proactiveaditya
2 Replies

5. Shell Programming and Scripting

Sed command - help me understand

Hello, can someone please explain me what the following commands do.. i know the output but i would like to understand the break down of what they do step by step. 1) sed -n "/ $(date +\%R -d "-1 min")/,$"p req.txt| wc -l 2) awk '/19:00/,/22:00/' app.log |grep "mytesturl"|grep... (2 Replies)
Discussion started by: niks
2 Replies

6. Programming

help me understand exec() family of calls...

how does exec() do it? on successful call of exec() family of functions, they never return... how to i emulate that. assume the standard example, execl("/bin/ls","ls",NULL); now this would never return. i m trying to emulate exec()'s never to return feature... #include<unistd.h>... (4 Replies)
Discussion started by: c_d
4 Replies

7. HP-UX

Need help to understand chatr command

Hi, I am trying to find the max memory utilization of a binary file using chatr command. i gave chatr <file name>. But no where i could see the max memory usage of the file. How do you i check the memory usage of this binary file. Upto my knowledge it should be 1.75GB in HP-UX. But the does... (1 Reply)
Discussion started by: kprasanna_79
1 Replies

8. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies

9. UNIX for Dummies Questions & Answers

Just a quickie to help me understand this command:

Hi. Im working in AIX. Oracle 9.2. In one of the backup scripts, I see this: sysback -f'/dev/rmt0' '-T chrp' '-k mp' -w '/tmpback' 'oradata_vg oraindx_vg ora misc_vg' The line here, as I know, specifies the device name, the temp file, and the volume groups. My query regards theses two: '-T... (2 Replies)
Discussion started by: Incremental
2 Replies

10. UNIX for Dummies Questions & Answers

Can't understand sar command

HI Experts, Can anyone pls help me to understand this.. >sar 20:05:00 1 2 1 96 20:10:00 2 2 10 87 20:15:00 1 2 19 78 20:20:00 1 2 14 83 20:25:00 1 2 16 81 20:30:00 1 ... (1 Reply)
Discussion started by: shaan_dmp
1 Replies
Login or Register to Ask a Question