more efficient script

master
Leo Coogan 2 months ago
parent 73fe913eae
commit 190639ab69
Signed by: lcoogan
GPG Key ID: 54DBD17B0D75ABB0

@ -1,3 +1,7 @@
#!/bin/sh #!/bin/sh
# Convert audio files recursively to opus # Convert audio files recursively to opus
find ./ -type f \( -iname \*.flac -o -iname \*.mp3 -o -iname \*.ogg -o -iname \*.m4a -o -iname \*.wav \) -exec sh -c 'i="{}"; ffmpeg -y -i "$i" "${i%.*}.opus" && rm -f "$i"' \;
#IFS=$'\n'; for i in $(find -name '*.c'); do echo $i; done
#find ./ -type f \( -iname '*.flac' -o -iname '*.mp3' -o -iname '*.ogg' -o -iname '*.m4a' -o -iname '*.wav' \) -exec sh -c 'i="{}"; ffmpeg -y -i "$i" "${i%.*}.opus" && rm -f "$i"' \;
IFS=$'\n'; for i in $(find . -iname '*.mp3' -iname '*.wav' -iname '*.ogg' -iname '*.m4a' -iname '*.flac*'); do ffmpeg -y -i $i ${i:r}.opus; done

@ -0,0 +1,36 @@
00:29:01 braewoods ╡ IFS=$'\n'; for i in $(find -name '*.c'); do echo $i; done
00:29:05 ⤷ ╡ This seems to work.
00:30:04 ⤷ ╡ By using newlines, you can get the full path just fine.
00:30:18 ⤷ ╡ Unless the file name happens to contain newlines (very unlikely), it should be fine.
00:31:30 ⤷ ╡ Never underestimate the ability of weird file names to confuse shell.
00:31:47 ⤷ ╡ Usually it is spaces, but it can be quotes, etc, too.
00:32:20 fmlatghor ╡ there's some other languages / ISO so I'm not surprised
00:32:25 braewoods ╡ globs don't care, as long as it matches the pattern it'll work even if the file name has other weird characters.
00:33:02 ⤷ ╡ So that's why this never comes up when you do something like: ls *.c
00:33:26 ⤷ ╡ But it does when you try to interpret the text in shell.
00:34:30 ⤷ ╡ But frankly, it might be easier to just use a language where this can't happen.
00:34:49 ⤷ ╡ There's a lot of weird hacks you have to use in shell.
00:35:21 ⤷ ╡ xargs is a nice program but doesn't work well with all utilities.
00:35:39 ⤷ ╡ If the utility isn't designed to process batch files, it pretty much is out of the question.
00:36:01 ⤷ ╡ If you have to do special file name processing, find's exec is also a bad fit.
00:36:02 fmlatghor ╡ okei I'll figure out what I need to replace in the rogiinal scirpt with the env var you gave me
00:36:38 braewoods ╡ It's simple, the idea is to just do the shell stuff in your shell rather than a subshell that find executes.
00:36:58 ⤷ ╡ If you need to micromanage everything from shell, you may as well just iterate over it manually.
00:37:28 ⤷ ╡ Alternatively, you could do something like:
00:37:32 ⤷ ╡ find | my_function
00:37:42 ⤷ ╡ and let the function iterate over the output.
00:38:09 ⤷ ╡ Of course you can only use shell functions in a pipeline from your script.
00:39:25 ⤷ ╡ The main reason shell functions exist imho is to manage more complex setups.
00:39:36 ⤷ ╡ Due to how shells work, environment is mostly global and shared.
00:39:45 ⤷ ╡ You can only do localized variables in a function.
00:39:48 ⤷ ╡ So...
00:40:21 ⤷ ╡ You need to write functions to keep variables from getting overwritten by accident.
00:40:36 ⤷ ╡ If you have a complex enough script to warrant that.
00:41:30 ⤷ ╡ This ain't in POSIX, but most shells implement a builtin called local that will make a given variable name local to the function.
00:42:17 ⤷ ╡ its main use is to allow you to reuse the same variable names without clashing.
00:43:42 ⤷ ╡ sometimes a complex shell script is preferable to a heavy duty utility.
00:52:38 fmlatghor ╡ okei I'll figure it out tomorrow, I'm too tired and I need to finish one more thing for school
00:59:44 ⤷ ╡ oh I see I couldn't follow the logic because I was drunk and tired
00:59:57 ⤷ ╡ at least I think I understand it
01:00:16 ⤷ ╡ > find: illegal option -- n
01:01:27 * ╡ fmlatghor shrugs
Loading…
Cancel
Save