Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

Scripting Help

Rene_17
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
Rene_17
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
Simon Hargrave
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
Franky_1
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
Highlighted
Jean-Louis Phelix
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 ...)
Rene_17
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
Rene_17
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
Franky_1
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
Rene_17
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
Simon Hargrave
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.
Jean-Luc Oudart
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
Rene_17
Regular Advisor

Re: Scripting Help

I now understand the syntax.
Thanks for your help !

Rene
Muthukumar_5
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!
Bharat Katkar
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