Maxwell's Daemon
Maxwell's Daemon

Reputation: 631

how to use brackets in a script?

I create this silly script:

#!/bin/bash
#archivo=0
for i in *.esp.srt
do
  iconv -f=ISO8859-1 -t=UTF-8 "$i" > "$i.srt"

done

But I have to rename the files before in order to execute the script properly. If the file is called, for example:

"whatever" (Español (España)).srt

It doesn't work.

I already tried changing this line in my script: for i in *(Español (España)).srt

But obviously, I'm doing something wrong because it doesn't work.

Edit: my script works when the file is *.esp.srt. But it doesn't work if the file is * (Español (España)).srt

Edit2: Now it works:

#!/bin/bash
#archivo=0
for i in *" (Español (España)).srt"
do
  iconv -f=ISO8859-1 -t=UTF-8 "$i" > "${i%% "(Español (España)).srt"}.esp.srt"
  rm *" (Español (España)).srt"
done

Upvotes: 0

Views: 169

Answers (3)

Amory
Amory

Reputation: 806

Edit: my script works when the file is *.esp.srt. But it doesn't work if the file is * (Español (España)).srt

Uhh yeah. That's because this line

for i in *.esp.srt

Only looks for things that end in .esp.srt which "whatever" (Español (España)).srt does not. If you want to use

for i in *(Español (España)).srt

Then you'll need to account for the brackets, as you imply. Put "" around it.

Upvotes: 1

konsolebox
konsolebox

Reputation: 75488

Try something like this:

mkdir -p converted && \
for i in *.esp.srt *Esp*.srt
do
  iconv -f=ISO8859-1 -t=UTF-8 "$i" > "converted/$i"
done

Also, how do you really want your filenames converted? You should also know that with your code your outputs become something.srt.srt. If you like to replace your extension to something like .utf8.srt, do it this way (assuming your files end in .esp.srt):

for i in *.esp.srt
do
  iconv -f=ISO8859-1 -t=UTF-8 "$i" > "${i%%.esp.srt}.utf8.srt"
done

Similarly you could add another loop for files like "whatever" .(Español (España)).srt:

for i in *'.(Español (España)).srt'
do
  iconv -f=ISO8859-1 -t=UTF-8 "$i" > "${i%%'.(Español (España)).srt'}.utf8.srt"
done

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185106

You can try running

detox *

before.

See http://detox.sourceforge.net/

Upvotes: 1

Related Questions