-1

I have got over 1000 folders, and each folder contains one .wav file and one .txt file (example of the folders). The text file contains the time interval, and I need to trim the .wav file into clips based on the time interval that each text file given (note that text files are in different folders). I have got the following script from enter link description here

#!/bin/bash
index=0
while read this; do
if [ $index -gt 0 ]; then
 sox sound.wav clip-$index.wav trim $start $this-$start
fi
((index+=1))
start=$this
done < times.txt

However, it is for single file and it can only be used for files in current directory. How can I make it work for subfolders and into a loop?

Bruce Wang
  • 33
  • 3
  • 1
    That `done` is the end of a loop that's never started. Have a go with https://www.shellcheck.net/ to find the most glaring errors. – Benjamin W. Jan 12 '18 at 23:05
  • Thanks Benjamin, the shellcheck is quite useful, but you are not really answering my question.. – Bruce Wang Jan 12 '18 at 23:15
  • 1
    `done` on its own is a syntax error. You have to pair it with a `while ...; do`. Have a look at [BashFAQ/001](https://mywiki.wooledge.org/BashFAQ/001). – Benjamin W. Jan 12 '18 at 23:47
  • Sorry, I think I missed one line in my original question, and I have just edited it. But still, how can I make it to work for subfolders..? – Bruce Wang Jan 13 '18 at 00:16

1 Answers1

0

Basically, you want to do this loop for all the sub-directories. That means that you need to loop over the sub-dirs:

#!/bin/bash
topdir=$(pwd)
find . -type d | while read dir ; do
    cd "$dir"
    if [  -f sound.wav -a -f times.txt ] ; then
        index=0
        while read this; do
            if [ $index -gt 0 ]; then
                sox sound.wav "clip-$index.wav" trim "$start" "$this-$start"
            fi
            ((index+=1))
            start="$this"
        done < times.txt
    fi
done

I assume that the loop in your question does what you want it to do, and I just added some quotes.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • Thanks a lot @Ljm Dullaart. I just changed `sound.wav` and `times.txt` into `*.wav` and `*.txt` , but it only worked for the first subfolder, and I got an error message says `line 6: [: too many arguments`. Any idea why is this happening? – Bruce Wang Jan 13 '18 at 13:17
  • Hi @Ljm, I have just figured it out. I actually just need to execute my trim command in all the subdirs. I just added `for dir in */; do (cd -- "$dir" && trim command here) done`. The original answer comes from [https://unix.stackexchange.com/questions/270477/change-directory-and-execute-command-automatically-then-change-directory-back-ou/270494#270494] by cuonglm. – Bruce Wang Jan 13 '18 at 17:21