Forum Moderators: bakedjake

Message Too Old, No Replies

Using a bash script to alter multiple files

Trouble with variables

         

encyclo

7:20 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to convert a bunch of files from the ISO-8859-1 encoding to UTF-8. For this I'm using the
iconv
command to automatically generate UTF-8 versions of each file. However, I want to automate this rather than doing it file by file, but my pitiful bash skills are not up to getting this working.

Here's my first attempt:

#/bin/bash
for i in *.php;
do iconv -f iso-8859-1 -t utf-8 %i -o %i_utf-8;
do mv %i_utf-8 %i;
done

But I get the error:

iconv: cannot open input file `%i': No such file or directory

Could anyone tell me where I'm going wrong? I've also got Python 2.4.1 or Perl 5.8 or PHP 4.3.10 if bash isn't the most appropriate tool.

Many thanks for any insight!

jamie

1:07 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi encyclo,

i am sure someone will come up with a better solution...

you need to feed "

for x in ...
" a list of all php files. create the list

LIST=`ls *.php` 
for $i in $LIST
do
...
...
done

hth

encyclo

6:42 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your reply jamie.

I'm getting a different error now, but I think there's signs of progress:

/home/example/bin/convert.sh: line 6: `$i': not a valid identifier

My script now reads like this:

#/bin/bash
LIST=`ls *.php`
for $i in $LIST ;
do iconv -f iso-8859-1 -t utf-8 %i -o %i_utf-8;
mv %i_utf-8 %i;
done

Am I using

%i
incorrectly or do I need to define it differently?

jamie

6:43 am on Apr 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi encyclo,

it should be $i