2

I have a large list of files:

i18n/
    editor.utf8
    editor_fr.utf8
    editor_ko.utf8
    editor_ge.utf8
    ...etc. (dozens of these)

I would like to run a utility, let's call it fizzbuzz, that takes as its program arguments an "input file" and an "output file". An example of invoking it would be:

fizzbuzz editor.utf8 editor.properties

or:

fizzbuzz editor_fr.utf8 editor_fr.properties

Hence, for each invocation, I want the input file to be a different file under i18n/, and I want the output file to be a file of the same name, but with a different file extension (.properties). If the output/properties file already exists, it should get totally overwritten.

How could I script things so that I can run fizzbuzz on each .utf8 file in i18n/, and for each of those files, specify an output with the same name, just a .properties file?

My best attempt thus far:

for FILE in i18n
do
    fizzbuzz FILE FILE.properties
done

Just creates a lone FILE.properties file.

  • 2
    What have you done on your own so far, where are you stuck? This sounds like a simple job for find in combination with exec, there are lots of questions on similar topics already. – Panki Jul 08 '19 at 13:29
  • Thanks @Panki (+1) -- please see my edit, I included what I have tried so far, as well as the undesired outcome! – hotmeatballsoup Jul 08 '19 at 13:33
  • 2
    Take a look at this: https://unix.stackexchange.com/questions/12902/how-to-run-find-exec Also, read up on how variables are used in a shell ($ is key here) – Panki Jul 08 '19 at 13:45

1 Answers1

2

If all of the files are in one level down within i18n, you don't need the find method.

Change the for command to use:

for FILE in i18n/*

Also the variable must be preceded with $ when referenced.

My recommendation is to use a safe command the first time you do a for loop, or it can make a lot of damage. For example, just list the value it sees for FILE, and show the commands it would run:

for FILE in i18n/*
do
    echo "fizzbuzz $FILE $FILE.properties"
done

When you are satisfied it is what you want, then take away the echo.

If you want it to walk down multiple directories, this answer is not going to work well, but I'm going by the original question which looks like it could be a flat directory.

labradort
  • 281
  • Thanks @labradort (+1) -- however there may be non-utf8 files in that directory (properties files from previous runs, JPGs, etc.). Is there any way to "filter" so that the loop only executes on .utf8 files? Something equivalent to for FILE in i18n/* THAT ENDS WITH .utf8, etc.? – hotmeatballsoup Jul 08 '19 at 14:56
  • 2
    Use for FILE in i18n/*.utf8 ..., and always quote variables! ==> So, instead of $FILE use "$FILE"! – pLumo Jul 08 '19 at 15:09
  • Thanks @pLumo (+1) -- just curious, why always quote them? – hotmeatballsoup Jul 08 '19 at 15:14
  • 1
    because filenames or in general variables may contain whitespace. And if they have, and you don't quote them, your script will break. If you're 100% sure that this doesn't apply to you, you should still quote them as a matter of habit... – pLumo Jul 08 '19 at 15:16
  • Thanks again @pLumo (+1), so when I run for FILE in i18n/*.utf8; do echo "fizzbuzz $FILE $FILE.properties"; done the output is fizzbuzz i18n/field_fr.utf8 i18n/field_fr.utf8.properties. This is so close to what I'm looking for, except I need the output file argument to be i18n/field_fr.properties, not i18n/field_fr.utf8.properties. Any ideas how I can remove the .utf8 file extension from the output file arg? – hotmeatballsoup Jul 08 '19 at 15:19
  • 3
    sure. Use "${FILE%.*}.properties". See here. – pLumo Jul 08 '19 at 15:22