You are not logged in.

#1 2012-02-20 23:06:33

silenc3r
Member
From: Poland
Registered: 2009-08-29
Posts: 149

Problem with simple regex in bash script

Hello, I'm writing simple script to automate splitting and tagging audio files.

Here's the code:

#!/bin/bash

cuefile=$1
extension=${cuefile##*.}
basename=${cuefile%.*}

if [[ $# -eq 1 ]] && [[ $extension = "cue" ]]; then
    if [ -e "$basename.flac" ]; then
        type="flac"
    elif [ -e "$basename.ape" ]; then
        type="ape"
    elif [ -e "$basename.wv" ]; then
        type="wv"
    elif [ -e "$basename.wav" ]; then
        type="wav"
    fi
else
    echo "You can split only flac, ape, wv and wav files!"
fi

shntool split -f "$basename.cue" -o flac -t %p_-_%n_-_%t -m \ _ "$basename.$type"
performer=`grep -m 1 'PERFORMER' "$cuefile" | sed 's/PERFORMER //g;s/"//g;s/\ /_/g'`
cuetag.sh "$cuefile" "$performer*.flac"

Everything works fine except the last line. I got error message like this:

~/code/bash/cuesplit Infatuation\ With\ Malevolence.cue 
warning: number of files does not match number of tracks
*.flac: ERROR: reading metadata, status = "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE"

The FLAC file could not be opened.  Most likely the file does not exist
or is not readable.

The strange thing is that if I run "cuetag.sh some_filename.cue artist*.flac" from prompt line it works.
I suspect that problem lies in proper use of regular expression, but I can't find out how to do it right.

Thanks in advance for any help.

Offline

#2 2012-02-20 23:31:48

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Problem with simple regex in bash script

Without seeing some of the input that causes that output, I'm not sure I can be of great help - but I do know that a grep+pipe+sed can almost always be simplified to an awk line.  Simplifying this way might help reign in the unwanted behavior too.

Edit: example

performer=`awk '/^PERFORMER/ { print $2_$3_$4_$5 }' $cuefile`

Of course you'd have to edit the number of parameters given to print, or if it varies, use a for loop from 2 to $NF.

Last edited by Trilby (2012-02-21 00:34:52)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-02-21 00:11:16

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Problem with simple regex in bash script

silenc3r wrote:

Hello, I'm writing simple script to automate splitting and tagging audio files.

Here's the code:

cuetag.sh "$cuefile" "$performer*.flac"

You're quoting a glob, meaning it will never expand. You shouldn't be confident that it'll expand, either. You can do something like this:

shopt -s nullglob
files=("$performer"*.flac)
if (( ${#files[*]} )); then
  cuetag.sh "$cuefile" "${files[@]}"
else
  echo "no files found!"
fi

Offline

#4 2012-02-21 00:21:05

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Problem with simple regex in bash script

[karol@black foo1]$ cat a1
#!/bin/bash

cuefile=$1
performer="foo bar baz"

echo "$cuefile" "$performer*.flac"
[karol@black foo1]$ ./a1 test
test foo bar baz*.flac

As falconindy said, '*' will be treated literally because of the quotes - not what you want.

Offline

#5 2012-02-21 19:24:04

silenc3r
Member
From: Poland
Registered: 2009-08-29
Posts: 149

Re: Problem with simple regex in bash script

falconindy wrote:

You're quoting a glob, meaning it will never expand. You shouldn't be confident that it'll expand, either. You can do something like this:

shopt -s nullglob
files=("$performer"*.flac)
if (( ${#files[*]} )); then
  cuetag.sh "$cuefile" "${files[@]}"
else
  echo "no files found!"
fi

Unfortunately this doesn't work either. I guess the easiest solution will be to parse a cue file to get the exact names of the files to proceed. It will also solve the problem with compilations. I don't have time now, but I will try to do it tomorrow.

Trilby wrote:

[...] but I do know that a grep+pipe+sed can almost always be simplified to an awk line. Simplifying this way might help reign in the unwanted behavior too.

Thanks for the tip.

Offline

#6 2012-02-25 10:08:11

aesiris
Member
Registered: 2012-02-25
Posts: 97

Re: Problem with simple regex in bash script

I would use find+xargs

find ./ -name "$performer*.flac" -print0 | xargs -0 cuetag.sh "$cuefile"

Offline

Board footer

Powered by FluxBB