First Script Syntax Error

So, I'm writing my first shell script to try to speed up the process of creating lyric files for mocp and I don't know what the error is, but considering how short the code is, I'm betting it's something super-obvious.
line 7: syntax error near unexpected token `echo'
line 7: ` echo "${song%%*(.mp3)}"'

#!/bin/bash
#This will create blank lyric files, as used by mocp, for all mp3s in a direcory

files=(ls *.mp3)
for song in $files
 echo "${song%%*(.mp3)}"
 touch "${song%%*(.mp3)}"
done

Hi D351,
I think you simply forgot the "do". Try this:

#!/bin/bash
#This will create blank lyric files, as used by mocp, for all mp3s in a direcory

files=(ls *.mp3)
for song in $files
do
 echo "${song%%*(.mp3)}"
 touch "${song%%*(.mp3)}"
done

Additionally, do you want to touch the mp3 file or touch a lyric file?

1 Like

Thanks. I'm trying to touch the lyric file which needs to be the same name as the mp3 but without the .mp3 extension.

---------- Post updated at 12:25 PM ---------- Previous update was at 12:21 PM ----------

Oh, wow. I need to figure out how to handle spaces in the file names.

Some comments on your code snippet:

files=(ls *.mp3)                # this will define & assign an array: (ls a.mp3 b.mp3 c.mp3 ...). You'd need to use it like an array in the for loop, then. 
                                # you might prefer command substitution: files=$(ls *.mp3)
                                # or let the shell do the work for you: files=*.mp3
for song in $files              # this will expand to the first array element: ls. If you want the .mp3 files as well, use array expansion: ${files[@]} if you don't like above proposals
 echo "${song%%*(.mp3)}"        # Why the * and the parentheses? You want to remove nothing but ".mp3", and once only, so one "%" is sufficient
 touch "${song%%*(.mp3)}"       # "${song%.mp3}" will do the job for you. "%*.mp3"  would erase the entire file name, the parentheses can't be matched
                                # and thus stop the expansion.  
                                # You are right in double quoting the file names which will handle spaces in file names, then.
done
1 Like

I've been cobbling this together from different tutorials found via duckduckgo, so my usage of parenthesis and multiple symbols is sketchy, but based on your advice, I now have:

#!/bin/bash
#This will create blank lyric files, as used by mocp, for all mp3s in a directory

files=*.mp3
for song in ${files
[*]}
do
 echo "${song%.mp3}"
 touch "${song%.mp3}"
done

which appears to be working perfectly. Thank you so much.