Operating System - HP-UX
1837085 Members
2342 Online
110112 Solutions
New Discussion

Re: run script in sequence

 
SOLVED
Go to solution
haeman
Frequent Advisor

run script in sequence

I have a script to read the .txt file in /tmp , the script will read the file content then write something to the file ( write different thing to different file depend the file content )

cat /tmp/*.txt |grep .... ; do ...


suppose there should only have one .txt file in /tmp , it works fine if there is only one .txt file , but however , sometimes there are more than one .txt file in it , in this case the result is not correct ( becuase the script cat the content for all *.txt file by one time) , so I would like to ask is there any way that when run my script it first check the existence of *.txt files , to make sure the files are read in sequence ( so that they write the correct thing to file ) , that mean the script will process every file for one time only .

for example

if there is one .txt file in /tmp
$ls /tmp
aaa.txt

when run my_script
$my_script /tmp
then it run my_script aaa.txt , it works fine


But , if in case there are 3 .txt files in /tmp

$ls /tmp
bbb.txt
ccc.txt
ddd.txt

when run my_script
$my_script /tmp
it will cat bbb.txt ccc.bbb.txt ddd.txt to find some content , then write it to each file , so the result is not correct.

what I want is when run my_script
$my_script /tmp
it run my_script bbb.txt , my_script ccc.txt & my_script ddd.txt separately,

can advise what can i do ? thx
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: run script in sequence

>cat /tmp/*.txt |grep .... ; do ...

You do know that grep can take a list of files. And depending on whether you want the name of the file in the output, you can either use cat(1) or use grep -h to suppress.

>sometimes there are more than one .txt file in it, in this case the result is not correct (because the script cat the content for all *.txt file by one time)

You need to explain why it isn't correct or provide more of your script fragment.

>to make sure the files are read in sequence (so that they write the correct thing to file ),

Sequence is alphabetically. What is the correct thing to write?

>that mean the script will process every file for one time only.

You can also do that with a "for" loop.

>what I want is when run my_script
$ my_script /tmp

If you want to process each file separately you can do:
for file in $1/*.txt; do
grep ... $file ...
done
haeman
Frequent Advisor

Re: run script in sequence

Thx