Will the output file be opened and closed several times?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Will the output file be opened and closed several times?
# 1  
Old 12-27-2007
Will the output file be opened and closed several times?

Hi, there,

I wrote a script like this:

#!/bin/bash
#put something into a LIST
for item in $LIST
do
cat $item >> /tmp/output
done

My question is that if I have 5 items in that LIST, should it be opened and closed every time when the ">>" works? So that file will be opened and closed 5 times, and it'll be a performance issue when it has a big number of items in that list. Am I right?

If so, anyway to improve it?

Thanks!
# 2  
Old 12-27-2007
you don't usually cat a variable, unless that variable contains the name of the file.
and no, don't worry about the >> .
# 3  
Old 12-27-2007
first of all
not cat $item >> /tmp/output
echo $item >> /tmp/output

you can use this to convert the list to lines and insert into file at once:
echo $LIST | awk '{for (i=1; i<=NF; ++i) {print $i}}' > /tmp/output
# 4  
Old 12-27-2007
Quote:
Originally Posted by mehmet_demirez
first of all
not cat $item >> /tmp/output
echo $item >> /tmp/output
we don't know where OP got his $LIST, so it may contain file names. So cat $item may be correct.
# 5  
Old 12-27-2007
I will assume that cat is correct... to open the file once, you could:

Code:
for item in $LIST ; do
      cat $item 
done >> /tmp/output

# 6  
Old 12-27-2007
Quote:
Originally Posted by Perderabo
I will assume that cat is correct... to open the file once, you could:

Code:
for item in $LIST ; do
      cat $item 
done >> /tmp/output

Oh, yes, it's a list of some process ids. I think you can treat them as file names, right?
Well, the real use case is that I have some users who run multiple firefox process independently on Mac machines and I need to know which profile those process are accessing. In another word, I need to know in this case, which profiles are "in use".

Firefox itself has a flag file called .parentlock in each profile folder and it suppose to be created once the profile is in use and deleted once the profile quits. But there's a bug with Apple's file system so the file won't be removed once it's created. That means I can't use the file as a flag.

So I decided to use some help of unix shell script, bash is installed by default so I'd like to use that. The idea is:
1) use 'ps -U' to get running processes for current user
2) use 'grep', 'sed' and 'awk' to find out all firefox processes and the profile name it attached, dump to a file

The script looks like:
Code:
#!/bin/bash

if [ -z $1 ]; then
        echo "No output file name."
        exit -1
fi

if [ -f $1 ]; then
        echo "Output file exists. Remove it."
#       exit -2
        rm -rf $1
fi

firefox_suspects=`ps -U \`whoami\`|grep firefox-bin|awk '{print $1}'`
for pid in $firefox_suspects;
do
        lsof -p $pid | grep '.parentlock' | awk '{print $10}' | sed 's!^.*/\([^/]*\)/\.parentlock$!\1!' >> $1
done

Well, now I know I can improve it by simply move the '>>' after 'done'. That's nice. But does it mean there *IS* performance issue in my old script and the outfile ($1) will be opened and closed as many times as the number of the items in that list?

Thanks for all the responses. Smilie

Wish everybody a Happy New Year!
# 7  
Old 12-27-2007
Quote:
Originally Posted by koifans
Well, now I know I can improve it by simply move the '>>' after 'done'. That's nice. But does it mean there *IS* performance issue in my old script and the outfile ($1) will be opened and closed as many times as the number of the items in that list?
Yes, you are opening the file in append mode many times and this will cause a minor performance issue.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Dup2 - for file descriptor opened by a different process

is it possible to duplicate file descriptors(opened by a different process) with the help of dup or dup2. the two process do not share parent child relationship as well. (2 Replies)
Discussion started by: replytoshishir
2 Replies

2. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

3. Shell Programming and Scripting

Help with cp command when destination file is opened

I am writing a shell script (runs on HP Unix) which copies files from a source directory to another destination daily. The destination directory always have the files with same name as in the source directory. And daily a new file will be created in the source. cp command works fine if the file... (1 Reply)
Discussion started by: arunkumar_ms
1 Replies

4. Shell Programming and Scripting

How to find who opened/modified a file last

Hi *, I'm wondering if it possible to know WHO opened or modified a file last? I know it's possible with some options of find and also ls to get when the file was modified last. I'm currently supervising a file and have to log all users name who opened or modified it. Thanx a lot for any... (3 Replies)
Discussion started by: Jabarod
3 Replies

5. Shell Programming and Scripting

Output section of file between two expressions multiple times

Attached is the exact ouput of a vmware VDR log file I am working with but what I am trying to achieve is as follows: I need to output sections of the file using the string "Normal backup" as the start and "Duration" as the end to seperate files so I can then manipulate them further to create... (2 Replies)
Discussion started by: jelloir
2 Replies

6. Shell Programming and Scripting

Find process through file opened

Hello all, I have a file that is growing and growing by the action of any process. How can I find what process is? Thank you (2 Replies)
Discussion started by: albertogarcia
2 Replies

7. UNIX for Advanced & Expert Users

My command is still running but someone closed my shell, can I get my output still?

I ran an fs_usage (based off dtrace facility) but someone closed the window I think.. But I still see the process running # 15023 I tried fg 15023 but it told me no job control.. Is there anyway I can see my output? The scan has been running an hour and the output is valuable to me. (3 Replies)
Discussion started by: glev2005
3 Replies

8. UNIX for Advanced & Expert Users

same file being opened by two users at a time

I want to avoid a situation where because two users simultaneously open a file and modify and save, leaving the original file in mess. Is there a way in UNIX to warn a user if that particular file is already being used by another user. Thanks in advance (3 Replies)
Discussion started by: paresh n doshi
3 Replies

9. Shell Programming and Scripting

How to recognize if a file still opened by any process?

Is there a way to tell for sure if a file currently is opened by any running process? I have a task to filter a text file which is produced by some long process. I have no way to communicate with that process, but I have access to a location, where that proces produce an output file. Need... (3 Replies)
Discussion started by: alex_5161
3 Replies

10. Programming

maximum number of times - a file can be opened

Hi All, We can find the maximum of open file descriptors in hold with respect to a process. As default size was 256 (with getrlimit) and the hard limt was 65536 I tried changing the limit to 1024(with setrlimit) successfully changed the limit but still I couldnt have as many open file... (3 Replies)
Discussion started by: matrixmadhan
3 Replies
Login or Register to Ask a Question