0

I wanted to set the title and label of an mp3 file as the name of the file. For example, if audio.mp3 is the name of the file, audio should be set as the title and label tags. To process all the mp3 files starting with letter P, I wanted to pass P*mp3 as the command line parameter. My shell script is:

# mp3tag.sh
# Change title and label of mp3 files to file name
# Feb-25-2019

if [ $# -lt 1 ]; then
    echo file name missing. Wild cards OK
    exit 1
fi

for i in $1; do 
    name=`echo $i | sed 's/....$//'` # Remove dot and mp3 from filename
    mp3info -t "$name" -l "$name" "$i"
    if [ $? -eq 0 ]; then
        echo Changed tags for "$i"
    else
        echo Error in changing tags for "$i"
    fi
done

This script processed only the first file P1.mp3 in the directory, though I had files like P1.mp3, P2.mp3, .... Where have I gone wrong? Please help

Seshadri R
  • 1,192
  • 14
  • 24

1 Answers1

1

Wildcard filename expansion is performed by the shell. By the time your script runs, the shell has already expanded P*mp3 into the actual list of filenames, and therefore $1 is only the first filename.

Use $* instead of $1.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Actually, he should use `for i in "$@"; do` which is so common that it can be abbreviated as `for i; do`. See also https://stackoverflow.com/a/9995322/1281485 – Alfe Feb 25 '19 at 17:06
  • Very nifty and it's a good reminder of my tip of iceberg knowledge, though I am using shell script since 1994. I would have marked yours as the most appropriate answer, if you had moved this from **Comments** to **Answers**. – Seshadri R Feb 26 '19 at 02:49