do an operation for each files

do an operation for each files

Post by GrelEn » Sat, 21 Feb 2004 05:12:02



hello,

i'm really new to shell script, and need to do this in bash.

i'm looking fo a script which will acept something like "mypath/*.txt" in
argument, which will call another shell and redirect the output to a
modified name like mypath/*.modified.txt.

i've try we this but it still do not work

#!/bin/sh

call=a_script.sh
modified="modified."

dir=`dirname $1`
pattern=`basename $1`
files=`find $dir -name "$pattern"`
for file in $files
do
    dir=`dirname $1`
    ext=`basename $1|awk -F. 'NF>1{print $NF}'`
    file=`basename $1 $ext`
    a_script.sh < $file >$dir/$file$modified$ext
done

thanks for any help !

 
 
 

do an operation for each files

Post by Harr » Sat, 21 Feb 2004 06:01:48


Quote:GrelEns wrote...

>hello,

>i'm really new to shell script, and need to do this in bash.

>i'm looking fo a script which will acept something like "mypath/*.txt" in
>argument, which will call another shell and redirect the output to a
>modified name like mypath/*.modified.txt.

>i've try we this but it still do not work

>#!/bin/sh

>call=a_script.sh
>modified="modified."

>dir=`dirname $1`
>pattern=`basename $1`
>files=`find $dir -name "$pattern"`
>for file in $files
>do
>    dir=`dirname $1`
>    ext=`basename $1|awk -F. 'NF>1{print $NF}'`
>    file=`basename $1 $ext`
>    a_script.sh < $file >$dir/$file$modified$ext

   /path/to/a_script.sh  < /path/to/$file > $dir/$file$modified$ext

- Show quoted text -

Quote:>done

>thanks for any help !


 
 
 

do an operation for each files

Post by GrelEn » Sat, 21 Feb 2004 06:28:12


so here is a (trivial) modified version :

#!/bin/sh

call=cat
modified="modified."

dir=`dirname $1`
pattern=`basename $1`
files=`find $dir -name "$pattern"`
for file in $files
do
    dir=`dirname $file`
    ext=`basename $file|awk -F. 'NF>1{print $NF}'`
    root=`basename $file $ext`
    echo "$call $file >$dir/$root$modified$ext"
    $call $file >$dir/$root$modified$ext
done

but i still can not understand why my for loop only iterated once ! i must
me missing something in the `find` part, as in $1 i had many matching files.

 
 
 

do an operation for each files

Post by GrelEn » Sat, 21 Feb 2004 06:58:23


ok so i now understand why i only get one single file, in fact if a call
this script py :

./itsname.sh a/path/*.txt

i was expected it to

Quote:> dir=`dirname $1`
$dir be : /a/path
> pattern=`basename $1`

$pattern to be : *.txt

but it seems like linux is doing some kind of expansion to the first
occurence of the pattern *.txt and
so $pattern is : first-file-whose-extension-was-txt

how could i proceed to avoid this expansion ?

 
 
 

do an operation for each files

Post by Harr » Sat, 21 Feb 2004 07:20:19


Quote:GrelEns wrote...

>ok so i now understand why i only get one single file, in fact if a call
>this script py :

>./itsname.sh a/path/*.txt

>i was expected it to

>> dir=`dirname $1`
>$dir be : /a/path
>> pattern=`basename $1`
>$pattern to be : *.txt

>but it seems like linux is doing some kind of expansion to the first
>occurence of the pattern *.txt and
>so $pattern is : first-file-whose-extension-was-txt

As Design,

When you do "ls *.txt" on a shell, it will show a.txt, b.txt, etc :)

Quote:>how could i proceed to avoid this expansion ?

Instead of expecting a shell not to expand *, you may want to change
the way you program your script :)
 
 
 

do an operation for each files

Post by Barry Margoli » Sat, 21 Feb 2004 07:22:36




> so here is a (trivial) modified version :

> #!/bin/sh

> call=cat
> modified="modified."

> dir=`dirname $1`
> pattern=`basename $1`
> files=`find $dir -name "$pattern"`
> for file in $files
> do
>     dir=`dirname $file`
>     ext=`basename $file|awk -F. 'NF>1{print $NF}'`
>     root=`basename $file $ext`
>     echo "$call $file >$dir/$root$modified$ext"
>     $call $file >$dir/$root$modified$ext
> done

> but i still can not understand why my for loop only iterated once ! i must
> me missing something in the `find` part, as in $1 i had many matching files.

Since you don't quote $1 in the variable assignments, the shell expands
the wildcards.  So it becomes:

dir=`dirname mypath/file1.txt mypath/file2.txt mypath/file3.txt`
pattern=`basename mypath/file1.txt mypath/file2.txt mypath/file3.txt`

dirname and basename only operate on the first filename, so the result
is:

dir=mypath
pattern=file1.txt

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
 
 

do an operation for each files

Post by Barry Margoli » Sat, 21 Feb 2004 07:38:54




> ok so i now understand why i only get one single file, in fact if a call
> this script py :

> ./itsname.sh a/path/*.txt

> i was expected it to

> > dir=`dirname $1`
> $dir be : /a/path
> > pattern=`basename $1`
> $pattern to be : *.txt

> but it seems like linux is doing some kind of expansion to the first
> occurence of the pattern *.txt and
> so $pattern is : first-file-whose-extension-was-txt

> how could i proceed to avoid this expansion ?

The shell automatically expands wildcards for all commands.  If you
don't want this to happen, you must quote the argument.  But why don't
you just embrace this?  Instead of expanding the argument in your
script, just use the expanded parameters:


do
  ...
done

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
 
 

do an operation for each files

Post by Sai Chivuku » Tue, 24 Feb 2004 16:31:29


Quote:> Since you don't quote $1 in the variable assignments, the shell expands
> the wildcards.  So it becomes:

Doesn't the shell expand the wildcards BEFORE calling his script?
What difference would quoting $1 make here anyway?

-Sai.

 
 
 

do an operation for each files

Post by Chris F.A. Johnso » Tue, 24 Feb 2004 16:57:13



>> Since you don't quote $1 in the variable assignments, the shell expands
>> the wildcards.  So it becomes:

> Doesn't the shell expand the wildcards BEFORE calling his script?

   Yes; That's what he's saying.

Quote:> What difference would quoting $1 make here anyway?

   When it's quoted, the wildcards are NOT expanded.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

do an operation for each files

Post by Barry Margoli » Wed, 25 Feb 2004 01:32:58




Quote:> > Since you don't quote $1 in the variable assignments, the shell expands
> > the wildcards.  So it becomes:

> Doesn't the shell expand the wildcards BEFORE calling his script?
> What difference would quoting $1 make here anyway?

I assumed he was quoting the argument when he invoked the script.  But
he also needs to quote $1 inside the script.

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
 
 

do an operation for each files

Post by Sai Chivuku » Wed, 25 Feb 2004 04:47:29


Quote:> > What difference would quoting $1 make here anyway?

>    When it's quoted, the wildcards are NOT expanded.

Ah, yeah! No filename substitution inside double quotes...sure...