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:

CreateFiles()
{
        grep "<VALUE1>" ${infile} >> ${outfile}
        grep "<VALUE2>" ${infile} >> ${outfile}
...
}

Assuming the files aren't explicitly opened or closed, will the shell do 2open(2) (and 2close(2) later on exit), or 4open(2) and 4close(2)?

egrep -e '<VALUE1>|<VALUE2>' ${infile} >> ${outfile}


If you have many values, maybe you should put them in a pattern_file.txt and then use

fgrep pattern_file.txt ${infile}

or

grep -f pattern_file.txt ${infile}

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.

strace it then

1 Like

Ah-hah. I knew I was forgetting an easy way to check. :o

It does open & close the files every time.

In case anyone is curious:

[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)

...