0

I found this script online that I'm trying to edit, but as I'm testing it I can see that it will spit out a bunch of errors for all the files I have with spaces. This is the kind of error log I get on the terminal window:

Skipping 1-03 as ./mp3/basename "$input_file" .wav.mp3 exists.
Skipping The as ./mp3/basename "$input_file" .wav.mp3 exists.
Skipping power, as ./mp3/basename "$input_file" .wav.mp3 exists.

And this is the script:

#!/bin/bash

# Title:        wav_to_mp3.sh
# Purpose:      Converts all WAV files present in the folder to MP3
# Author:       Karthic Raghupathi, IVR Technology Group LLC
# Last Revised: 2014.01.28

# references
sox="/usr/local/bin/sox"
sox_options="-S"

# variables
source_folder="${1:-.}"
destination_folder="${source_folder}/mp3"
environment="${2:-DEVELOPMENT}"

# check to see if an environment flag was supplied
if [ $environment = "PRODUCTION" ] || [ $environment = "production" ]; then
    sox="/usr/bin/sox"
    environment="PRODUCTION"
fi

# print all params so user can see
clear
echo "Script operating using the following settings and parameters....."
echo ""
echo "which SoX: ${sox}"
echo "SoX options: ${sox_options}"
echo "Environment: ${environment}"
echo "Source: ${source_folder}"
echo "Destination: ${destination_folder}"
echo ""

read -e -p "Do you wish to proceed? (y/n) : " confirm

if [ $confirm = "N" ] || [ $confirm = "n" ]; then
    exit
fi

# create destination if it does not exist
if [ ! -d "${destination_folder}" ]; then
    mkdir -p "${destination_folder}"
fi

# loop through all files in folder and convert them to
for input_file in $(ls -1 $1 | grep .wav)
do
    name_part=`basename "$input_file" .wav`
    output_file="$name_part.mp3"

    # create mp3 if file does not exist
    if [ ! -f "$destination_folder/$output_file" ]; then
        $sox $sox_options "${source_folder}/$input_file" "$destination_folder/$output_file"
    else
        echo "Skipping ${input_file} as $destination_folder/$output_file exists."
    fi


done

I know I'm supposed to make it escape the space characters, but I can't figure out how. I tried changing some quotes here and there but I'm just breaking it.

BTW, if anyone would be so kind as to link a good tutorial for learning how to make bash scripts on Mac OS (or Unix), that would be much appreciated. I already know a bit of web programming so I'm not a complete n00b, but still, I'm having trouble creating very simple scripts and I would like to learn independently without constantly bugging the internet for help :)

Marw
  • 35
  • 1
  • 8

2 Answers2

1

This is wrong:

for input_file in $(ls -1 $1 | grep .wav)

See here why. Also, inside $1, try this to see that filenames with spaces give trouble:

for i in $(ls -1 | grep wav); do echo $i; done 

Try this instead:

for input_file in $1/*.wav
Mko
  • 148
  • 1
  • 6
-4

You can escape spaces by inserting a backslash character before the space.

Change:

This file name

To:

This\ file\ name

It might be an idea to write a function to do this for you, iterate through each character in a string and adding a \ caracter before any spaces. that way you don't need to worry about pre-formatting the file names and escaping each individual space - just run the file name through the function and capture the result.

Nunchy
  • 948
  • 5
  • 11
  • Ok thanks! I'll try to see how I can make a function for that, thanks for the quick reply :) – Marw Jul 23 '16 at 11:20
  • No worries - you might find this useful: http://tldp.org/LDP/abs/html/string-manipulation.html – Nunchy Jul 23 '16 at 11:22
  • 1
    Downvote: This is fixing the symptom, not the problem. http://mywiki.wooledge.org/ParsingLs – tripleee Jul 23 '16 at 13:17