Score:0

STDOUT not performing as expected in script

um flag

Having just been helped to get my script functioning on directories with spaces in the name (question 1447738 - thank you, again), I thought I'd like to pass its output to a text file.

Here's the script:

find . -type d -print0 | while read -r -d $'\0' dir; do
cd "$dir"
  for file in *.ogg *.wav *.mp3
  do
   duration=$(ffprobe "$file" 2>&1 | awk '/Duration/ { print $2 }')
   echo -e "$duration\t$file"
  done | sort -n
cd - > /dev/null
done

I thought I should be able to change the "echo" line to this to output to a text file:

echo -e "$duration\t$file" > length-check.log

Despite the fact that without the sdtout > the results display correctly, with the > all the log file contains is this:

*.ogg

*.wav

*.mp3

I know I could get the output by launching the script thus:

./length-check.sh > length-check.log

But I'd like to know where I've gone wrong within the script.

So, again, my lack of knowledge of scripting is holding me back. I'd be really grateful if someone could show me how to write this line correctly.

Many thanks, again.

Terrance avatar
id flag
Try instead of `> length-check.log` to `| tee -a length-check.log`
hr flag
Are you setting the nullglob option in your script (`shopt -s nullglob`)? If not, bash will set the file variable to literal `*.ogg` etc. in the case that there are no matching files in `"$dir"`
JamesB avatar
um flag
Thank you both for your reply. Much appreciated. The original post shows the whole script, minus the `#!/bin/bash` line.
JamesB avatar
um flag
@Terrance I get the same result with `| tee` as I do with `>` or `>`.
JamesB avatar
um flag
@steeldriver If I insert `shopt -s nullglob` no log file is created. I assume it should be placed at the start.
hr flag
If you get only `*.ogg *.wav *.mp3` in the log when nullglob is not set, and no log when it is set, that suggests there simply are no matching files in the directories that you are iterating over (or at least in the *last* found directory, if you use `>` instead of `>>`)
Terrance avatar
id flag
I tested your `echo -e "$duration\t$file"` line with the `tee -a`, which is append and not overwrite, and it worked fine. Your issue actually lies in the `find . -type d -print0 | while read -r -d $'\0' dir` as that clumps all directories as one name. I am not 100% sure but I was toying with `find . -type d -printf '%P\n' | while read -r -d "$0" dir` seemed to work better, but I don't have directories like yours to test with.
Score:2
es flag

You are overwriting your output file with every iteration of your loop. What you probably want is to append to it; use >> instead of >.

JamesB avatar
um flag
Hi there, and thank you for your message. No, I used both > and >>. Same result. There's something else wrong with my script.
Score:0
um flag

Thanks everyone for your help. It is hugely valued. This one may remain a mystery, as all suggestions don't result in a logfile output.

The only way I could achieve a logfile is to launch the file thus:

./length-check.sh > length-check.log

After that, loading the results into a spreadsheet and sorting for play length all worked.

Thank you all again, and Happy New Year!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.