Execute all files at once

Hi everyone, i'm newbie to unix!
If I have many files named by temp1.txt, temp2.text, temp3.txt,..... I want to introduce some key words at the top of each of files. The script (named bimbo) I tried is shown.

#!/bin/csh
set FILE=$1
echo $FILE
set OUT=$1
echo '%tage='$FILE  >> x_$FILE    
sed -e  '2i\%aveg=330'   x_$FILE  > x1  
sed -e  '3i\temp=3000MB'  x1 > x2
sed -e '4i\%rain=8'  x2  > x3
sed -e '5i\@skyhigh' x3  > x4
sed -e '6i\ '   x4> x5      # insert blank line
echo 'topic:'$FILE.txt >>  x5    # file names
sed -e '8i\ ' x5> x6
sed -e '9i\bm nnm'   x6 > x7
cat x7 $FILE.txt >  $OUT.out

when I execute: ./bimbo temp1 it works but not for multiple files at once /bimbo *.txt,? so how do i make to excetue all files at once? thanks

This might work:

ls temp*.txt | xargs -n 1 bimbo

The functionality could also be built into the bimbo script, so you could run bimbo temp*.txt, but the above way will probably work.

Of course, I hope you are saving copy of original files to verify bimbo script working correctly.

If there are numerous temp*.txt files so that it exceed the ARG_MAX you could get an "argument list too long" error.
better use the "for" SHELL built-in

for i in temp*.txt
do bimbo $i
done

Are you sure about that? You may be right. But I thought part of the point of xargs was to avoid such problems. It seems to work, no error message, on a test I did. Any counter-example?

$ getconf ARG_MAX
2621440
$ seq 2621441 | xargs -n 1 echo | tail -2 # takes a while to run!
2621440
2621441

The ARG_MAX (or may be LINE_MAX ) issue is with ls temp*.txt and not with xargs .

1 Like

Thanks.

If you want to give a test (may takes a while but still) :

seq 2621441 | while read n
do touch tst_$n
done

then try :

ls tst_* | tail

then try :

for i in tst_*
do echo $i
done | tail

Yes, I'm sure it would take a long while :slight_smile:
So I did not run the example.

But I think I get your point. I think the point is
that in the case where a huge number of files:

"for i in tst_" works OK (tst_ not expanded).
"ls tst_" fails (tst_ expanded, exceeds ARG_MAX).

I previously assumed "for i in tst_" was expanded. It
seemed the shell could see and expand tst_
. But
apparently expansion does not happen. When I use
"set -x", it displays "for i in 'tst_*'" with the single
quotes preventing expansion.

The other thing I noticed is that my "seq 2621441"
example, while it worked, was overkill and misguided.
ARG_MAX is the length of the argument list, not the
number of args. "seq 2621441" produces way more
than ARG_MAX characters.

Excactly ! (Point 2.9 in The Open Group Base Specifications Issue 6)

1 Like