Showing results for 
Search instead for 
Do you mean 

Scripting Help

Regular Advisor

Scripting Help

I need a script for following problem !

I need to find all files with .afi extention.
After that i must cut the extention .afi und run a command with the files without extention.

13 REPLIES
Regular Advisor

Re: Scripting Help

Script:

!/bin/sh
set -x
for f in *.afi
do
afena $f ~/tmp
done

I must cut the .afi extention that afena command works !

Regards,

rene
Honored Contributor Honored Contributor

Re: Scripting Help

!/bin/sh
set -x
for f in `ls *.afi | sed 's/\.afi$//'`
do
afena $f ~/tmp
done

the sed will strip trailing ".afi" from each file. This will also work for files with multiple "." eg file.1.afi etc
Respected Contributor Respected Contributor

Re: Scripting Help

Hi Rene,

for i in `ls *.afi|awk -F. '{print $1}'`
do
afena $i
done

This should work

Regards

Franky
Don't worry be happy
Honored Contributor Honored Contributor

Re: Scripting Help

Hi,

Just for your information, because previous posts works, it's sometimes useful to know about shell's capabilities. For example :

phelix> A=foo.abcd
phelix> echo $(basename $A .abcd)
foo
phelix> echo ${foo%%.abcd}
foo

Regards,

Jean-Louis
It works for me (© Bill McNAMARA ...)
Regular Advisor

Re: Scripting Help


On the console the commands works !

Command: for f in 'ls *.afi | sed 's/\.afi$//''

But in the Script it didn´t work ?

Message:
+ afena ls t1.afi t2.afi | sed s/.afi$//

?????????????

Regards,

Re
Regular Advisor

Re: Scripting Help


On the console the commands works !

Command: for f in 'ls *.afi | sed 's/\.afi$//''

But in the Script it didn´t work ?

Message:
+ afena ls t1.afi t2.afi | sed s/.afi$//

?????????????

Regards,

Re
Respected Contributor Respected Contributor

Re: Scripting Help

Hi,

why don't you just use the "awk" solution?
Even works in the script

Regards

Franky
Don't worry be happy
Regular Advisor

Re: Scripting Help

I notized that there where special characters around the command !

Thanks for your Help !

I only have one question !

Descripe to me the several characters above:

", ',`´

Regards

Re
Honored Contributor Honored Contributor

Re: Scripting Help

You have changed the quoting on the "not working version".

It should read: -

for f in `ls *.afi | sed 's/\.afi$//'`

however you are now using: -

for f in 'ls *.afi | sed 's/\.afi$//''

Not the difference in quotes. The very first and last quotes should be backquote ` (top-left of your keyboard).

The awk solution suggested above will also work, however it will not take into account filenames with more than one "." in.
Honored Contributor Honored Contributor

Re: Scripting Help

Change the script to :

#!/bin/sh

set -x

for f in $(ls -C1 *.afi | sed "s/\.afi$//")
do
echo $f
done

Regards,
Jean-Luc
fiat lux
Regular Advisor

Re: Scripting Help

I now understand the syntax.
Thanks for your help !

Rene
Honored Contributor Honored Contributor

Re: Scripting Help

You can do it easily with find command as,

-- test.ksh --
afena $1 ~/tmp
--------------

find -name "*.afi" -exec ksh test.ksh {} \;

Is afena a shell script function ?

You can also do as,

for FILE in $(ls *.afi|perl -pe 's/.afi$//)
do
echo $FILE
done
Easy to suggest when don't know about the problem!
Highlighted
Honored Contributor Honored Contributor

Re: Scripting Help

Hi,
See if this works:

# find ./ -name *.afi | cut -d "." -f 1 | xargs

Regards,
You need to know a lot to actually know how little you know