Find the answer to your Linux question:
Results 1 to 6 of 6
My BASH script populates the id3tag from pre-existing id3 tags and/or the filename. The script has id3lib dependencies (id3tag, id3info, and id3convert). I would like to make the script more ...
  1. #1
    Just Joined!
    Join Date
    Oct 2005
    Posts
    11

    Editable input and using read

    My BASH script populates the id3tag from pre-existing id3 tags and/or the filename. The script has id3lib dependencies (id3tag, id3info, and id3convert). I would like to make the script more interactive and allow the user to build the id3 tags if necessary. Currently, the script is fairly crude (no error checking and design for specific naming convention).

    Id3tag builds the tag information similar to the following line (this line is just an example).

    Code:
    id3tag --artist=$ARTIST --song=$SONG --album=$ALBUM --year=$YEAR --track=$NUM --genre=$GENRE
    Basically, my script builds an array with all the option to pass to the ID3tag.

    Code:
    id3tag ${TAGS[@]}
    I want to prompt the user with the id3tag parameters and allow them to edit them; and pass them to the script. The prompt would allow the user to populate the tags; edit the id3tag parameters; or skip tagging for this file. I don’t know how to present editable input to the user; pass the edited input to the script; and execute it. Here is my work-in-progress
    Code:
    echo	"id3tag ${TAGS[@]}"
    read -p "Do you want to run populate the ID3 tag (y,n,edit)?" ANSWER
    case $ANSWER in
    	[Yy]*) id3tag ${TAGS[@]};;
    	[Nn]*) echo 'Exiting'
    	continue;;
    esac
    I am also having trouble with the “read” command. The line below does not prompt the user for input. The script does not pause to read from the console. Both code blocks are found within a while loop.
    Code:
    if [ "$ALBUM" = "" ]; then
    	echo "ID3 tag does not contain ALBUM"
    	read -p "Please enter the Album" ALBUM
    	TAGS[i++]="--album=\"$ALBUM\""
    else
    	TAGS[i++]="--album=\"$ALBUM\""
    fi

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    That read statement works fine, Are you saying it shows Please enter the album but then just keeps going?

  3. #3
    Just Joined!
    Join Date
    Oct 2005
    Posts
    11
    That read statement works fine, Are you saying it shows Please enter the album but then just keeps going?
    Yes, the script does not pause at the “read” statement and does not allow the user to enter a value. The script does echo the “Please enter the album;” which shows that the "if" condition was met. The script does not stop at the

    “read -p "Do you want to run populate the ID3 tag (y,n,edit)?" ANSWER”

    A while loop contains both these read statements. Could this be an issue? My script does not return error otherwise.

  4. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    How are you running the program?

  5. #5
    Just Joined!
    Join Date
    Oct 2005
    Posts
    11
    I have gained some insight on my problem. It may relate to where the read command output goes when piped through multiple commands. The “read” command resides in a piped loop, thus my redirection was going somewhere else (unknown to me). I am still trying to wrap my head around stdin, stdout, and stderror in relationship to pipe commands. So I have not been able to resolve my “read” issues; and now I have to understand how to redirect my “find” and “while” commands. The goal is to redirect the keyboard input to the stdout (I guess). Then stderr may need redirection.

    I understand the fundamentals behind redirection.
    stdin equals 0
    stdout equals 1
    stderr equals 2
    2>&1 # send stderr to the same place as stdout
    3<&0 # send stdin to a file descriptor 3; little confused here

    The exec command adds to the confusion as well.

    My Bash script has code similar to below. This code is crude and may contain numerous mistakes. Presently, I learning the syntax and build upon a previously working script. The pseudo code here just illustrates my situation (and does not to showcase scripting knowledge).

    Code:
    find . -name \*.mp3 -type f -print | while read FILE
        do
            i=0
            FILE=${FILE##*/}
            TAG=`id3info "$FILE"`
            TITLE2=$(echo "$TAG" | awk -F ":" '/TIT2/ { print $2}' | sed -e 's/^ *//' -e 's/ *$//')
            ALBUM1=$(echo "$TAG" | awk -F ":" '/TALB/ { print $2}' | sed -e 's/^ *//' -e 's/ *$//')
            # Other code here
            if [ -z "$ALBUM" ]; then
                # DOES NOT PAUSE FOR INPUT
                echo "ID3 tag does not contain ALBUM"
                history -s "$ALBUM"
                printf "Press up arrow to edit current value (%s)\n" "$ALBUM"
                read -ep "Enter album title: " VAR
                ALBUM="$VAR"
                TAGS[i++]="--album=\"$ALBUM\""
              else
                TAGS[i++]="--album=\"$ALBUM\""
            fi
            # More code here
    
            echo"id3tag ${TAGS[@]}"
            # DOES NOT PAUSE FOR INPUT
            read -p "Do you want to run populate the ID3 tag (y,n,edit)?" ANSWER
            case $ANSWER in
                [Yy]*)        id3tag ${TAGS[@]}
                              ;;
                [edit]*)       history -s "id3tag ${TAGS[@]}"
                              printf "Press up arrow to edit current value (%s)\n"
                              read -ep "Edit the ID3tag parameter: " VAR
                              ;;
                [Nn]*)        echo 'Exiting'
                              continue
                              ;;
        esac
    
    done

  6. #6
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    That find | while isn't going to work as far as I can tell. You can get something functionally similar by doing

    Code:
    for FILE in *.mp3; do
    stuff goes here
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...