ksh file handling


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh file handling
# 1  
ksh file handling

Specifically on RHEL 5.7.

When does the underlying ksh process open and close files? Every time they're accessed, or as little as possible?

Say you have some script like:
Code:
CreateFiles()
{
        grep "<VALUE1>" ${infile} >> ${outfile}
        grep "<VALUE2>" ${infile} >> ${outfile}
...
}

Assuming the files aren't explicitly opened or closed, will the shell do 2*open(2) (and 2*close(2) later on exit), or 4*open(2) and 4*close(2)?
# 2  
Code:
egrep -e '<VALUE1>|<VALUE2>' ${infile} >> ${outfile}

If you have many values, maybe you should put them in a pattern_file.txt and then use
Code:
fgrep pattern_file.txt ${infile}

or
Code:
grep -f pattern_file.txt ${infile}

# 3  
I know - it's just an example. The actual command used isn't relevant to the question I asked - I could equally have used any other command sequence that both reads and writes.
# 4  
strace it then
This User Gave Thanks to ctsgnb For This Post:
# 5  
Quote:
Originally Posted by ctsgnb
strace it then
Ah-hah. I knew I was forgetting an easy way to check. Smilie

It does open & close the files every time.

In case anyone is curious:
Code:
[user@host1: /tmp] cat x.sh
#!/bin/ksh

MyFiles ()
{
   grep "<TAG1>" < file1 >> file2
   grep "<TAG2>" < file1 >> file2
}

MyFiles;

[user@host1: /tmp] strace ./x.sh > /tmp/x.trace 2>&1
[user@host1: /tmp] cat x.trace

...

stat("/bin/grep", {st_mode=S_IFREG|0755, st_size=88896, ...}) = 0
lstat("/bin/grep", {st_mode=S_IFREG|0755, st_size=88896, ...}) = 0
open("file1", O_RDONLY)                 = 3
fcntl(0, F_DUPFD, 10)                   = 11
fcntl(11, F_SETFD, FD_CLOEXEC)          = 0
close(0)                                = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3f5edea0) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=60, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=60, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fcntl(3, F_DUPFD, 0)                    = 0
close(3)                                = 0
close(3)                                = -1 EBADF (Bad file descriptor)
open("file2", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
fcntl(1, F_DUPFD, 10)                   = 12
fcntl(12, F_SETFD, FD_CLOEXEC)          = 0
close(1)                                = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3f5edea0) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=38, ...}) = 0
fcntl(3, F_DUPFD, 1)                    = 1
close(3)                                = 0
fstat(1, {st_mode=S_IFREG|0644, st_size=38, ...}) = 0
lseek(1, 0, SEEK_CUR)                   = 0
close(3)                                = -1 EBADF (Bad file descriptor)
rt_sigprocmask(SIG_BLOCK, [HUP INT QUIT PIPE], [], 8) = 0
vfork()                                 = 6750
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
close(1)                                = 0
fcntl(12, F_DUPFD, 1)                   = 1
close(12)                               = 0
close(0)                                = 0
fcntl(11, F_DUPFD, 0)                   = 0
close(11)                               = 0
rt_sigaction(SIGINT, {0x416ac0, [], SA_RESTORER|SA_INTERRUPT, 0x385be302d0}, {SIG_DFL, [], 0}, 8) = 0
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 6750
--- SIGCHLD (Child exited) @ 0 (0) ---
rt_sigreturn(0x11)                      = 6750
wait4(-1, 0x7fff3f5ee494, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
wait4(-1, 0x7fff3f5ee494, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
rt_sigaction(SIGCHLD, {0x423cf0, [], SA_RESTORER|SA_INTERRUPT, 0x385be302d0}, {0x423cf0, [], SA_RESTORER|SA_INTERRUPT, 0x385be302d0}, 8) = 0
ioctl(2, TIOCGPGRP, [0])                = -1 ENOTTY (Inappropriate ioctl for device)
open("file1", O_RDONLY)                 = 3
fcntl(0, F_DUPFD, 10)                   = 11
fcntl(11, F_SETFD, FD_CLOEXEC)          = 0
close(0)                                = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3f5edea0) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=60, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=60, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fcntl(3, F_DUPFD, 0)                    = 0
close(3)                                = 0
close(3)                                = -1 EBADF (Bad file descriptor)
open("file2", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
fcntl(1, F_DUPFD, 10)                   = 12
fcntl(12, F_SETFD, FD_CLOEXEC)          = 0
close(1)                                = 0
fcntl(12, F_SETFD, FD_CLOEXEC)          = 0
close(1)                                = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff3f5edea0) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=56, ...}) = 0
fcntl(3, F_DUPFD, 1)                    = 1
close(3)                                = 0
fstat(1, {st_mode=S_IFREG|0644, st_size=56, ...}) = 0
lseek(1, 0, SEEK_CUR)                   = 0
close(3)                                = -1 EBADF (Bad file descriptor)

...

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #813
Difficulty: Medium
The CSS RGBA alpha parameter defines the opacity as a number between 1.0 (fully transparent) and 0.0 (fully opaque).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File handling

Hi All, I need to extract the data from the text file. The data of the text file is shown below #L 0.000017 4.329939 0.000017 4.716267 r7.9 P 1 1;Net=IN32 The extracted data should be IN32. Could anyone help to script in c shell.? (4 Replies)
Discussion started by: gopishrine
4 Replies

2. UNIX for Dummies Questions & Answers

File handling

I have a file 1 298167 298168 1093209 1093210 1422663 I want to write a code where in I want to read contents of above file like first read is 1 second read is 298167 Substract second read from first and if this is greater than or less than 99,999. Similarly I want to traverse thru... (3 Replies)
Discussion started by: Guru148
3 Replies

3. UNIX for Advanced & Expert Users

File handling

my input for a script is another csv file but in that file say 7 lines are there ... how can i get line by line to that input for example : >cat link.csv www.yahoo.com,yahoo www.google.com,google www.unix.com,unix another file in that file i need to ping the above links ... (2 Replies)
Discussion started by: ponmuthu
2 Replies

4. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

5. Shell Programming and Scripting

Handling values with space while passing commandline argument from wrapper script in KSH

Hi there, I have a wapper script which passes the argument from command prompt to inner script.. It works fine as long as the argument containing single word. But when value contains multiple word with space, not working as expected. I tried my best, couldn't find the reason. Gurus, pls.... (2 Replies)
Discussion started by: kans
2 Replies

6. UNIX for Advanced & Expert Users

File Handling

Hi, I have a log file which runs into 3 to 5 GB. We store this typically for 6 months. When a new month starts we move the previous month into a 9 month back up log (file.9m) and delete the last month of the 9 month back up. Iam using awk to find the data and cat to join the files like... (3 Replies)
Discussion started by: baanprog
3 Replies

7. Programming

file handling

Hi all, I got a little issue here. Imagine that I have more than one process accessing one file. Is it possible to know which process(es) are accessing that file when I open the file?? Thanks for the help. Best regards, Ernesto (2 Replies)
Discussion started by: ninjanesto
2 Replies

8. Shell Programming and Scripting

file handling with ksh scripting

how can i write content of a variable to a file? how can i read standard output into a variable? (1 Reply)
Discussion started by: gfhgfnhhn
1 Replies

9. Programming

File Handling in C

Hi all, I have a problem in handling files through C. here is the problem im having: i will query the database (for instance consider employees table ) for empno,ename,job,salary fields.The query returns me some 100 of rows. now i need to place them in a file in row wise pattern as they... (3 Replies)
Discussion started by: trinath
3 Replies

10. Shell Programming and Scripting

KSH File Handling

Hi Im writing a small script right now which reads all of the files in a directory and displays there name. However I dont want to use the ls command. Is there any way of reading the names of the files and printing them to the screen? (2 Replies)
Discussion started by: madtim
2 Replies

Featured Tech Videos