LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH Script - What am I doing wrong in this test? - BASH Script (https://www.linuxquestions.org/questions/programming-9/bash-script-what-am-i-doing-wrong-in-this-test-bash-script-4175603408/)

BW-userx 04-07-2017 02:09 PM

BASH Script - What am I doing wrong in this test? - BASH Script
 
example page I had to go and check it to make sure it was not a me mistake.
http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html

Code:

if [ -f /var/log/messages ]
  then
    echo "/var/log/messages exists."
fi


just check to see if file is present if true returns 0 then acts on it. my code.

Code:

script_dir="~/scripts"
Code:

[[ -f "${script_dir}"/"${NewFile}" ]] && resampledFileLoc="${script_dir}"/"${NewFile}"
reslults set -x
Code:

next line is to lame flac file
+ [[ flac == \f\l\a\c ]]
+ flac -cd '/media/data/Deluge_Done/1979 - Public Image Ltd. - Metal Box [Virgin Vinyl 24-96 FLAC][2009][Kel Bazar]/01 - Albatross.flac'
+ lame -b 320 - '01 - Albatross.mp3'
flac 1.3.1, Copyright (C) 2000-2009  Josh Coalson, 2011-2014  Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
welcome to redistribute it under certain conditions.  Type `flac' for details.
01 - Albatross.flac: done       
LAME 3.99.5 64bits (http://lame.sf.net)
Resampling:  input 96 kHz  output 48 kHz
Using polyphase lowpass filter, transition band: 20323 Hz - 20903 Hz
Encoding <stdin> to 01 - Albatross.mp3
Encoding as 48 kHz j-stereo MPEG-1 Layer III (4.8x) 320 kbps qval=3

now comes the check to insure that a file was re-sampled
Code:


+ [[ -f ~/scripts/01 - Albatross.mp3 ]]

#
#  It tested true but did not set the variable The next line proves it.
#
+ printf '\n\n\ resampledFileLoc --> \n\n\n\n'
\ resampledFileLoc -->

#
# nothing was inside of it
#
+ [[ ! -z resampledFileLoc ]]
+ [[ -d '' ]]
+ [[ ! -z resampledFileLoc ]]
+ [[ -d '' ]]

I even changed it to

Code:


if [[ -f "${script_dir}"/"${NewFile}" ]] ; then
resampledFileLoc="${script_dir}"/"${NewFile}"
fi

and I still get the same results.

entire partial code block ( did you see what I did? I said entire partial code block that contradicts itself ;) you didn't catch that did you? )
Code:


#Set newFile name for converted flac to mp3
[[ "${ext}" == 'flac' ]] && NewFile="$title"."mp3"

#check for extension type

 printf "next line is to lame flac file\n\n\n\n\n"
 
[[ "${ext}" == 'flac' ]] && flac -cd "$FILENAME" | lame -b 320 - "$NewFile"

# check to be sure file was created

[[ -f "${script_dir}"/"${NewFile}" ]] && resampledFileLoc="${script_dir}"/"${NewFile}"


printf "\n\n\ resampledFileLoc --> $resampledFileLoc\n\n\n\n"

#check to be sure it is there again and check to be sure directories to move it to are there before moving 
[[ ! -z "resampledFileLoc" && -d  "${ArtistAlbum}" ]] && ( mv -v "resampledFileLoc" "${ArtistAlbum}" ; unset ArtistAlbum )

what am I doing wrong now?

NevemTeve 04-07-2017 02:23 PM

Please create a complete script that shows the problem and is minimal (everything in it is essential to produce the problem),

BW-userx 04-07-2017 02:29 PM

Quote:

Originally Posted by NevemTeve (Post 5693990)
Please create a complete script that shows the problem and is minimal (everything in it is essential to produce the problem),

now I got a copy right it Just in case .. J/k

Code:


#!/bin/bash

#Take meta data and use it to rename mp3 files taken off iPhone
# then make directories by band name and album.
#Then copy it into same said directories
#accordingly
#Apr. 06, 2017

working_dir="/media/data/Deluge_Done"
copy_to="/media/data/Sorted_iPhone-Music"
move_to="/media/data/LostTitle_iPhone_Music"
script_dir="~/scripts"


set -x

#find "$working_dir" -type f -name "*.mp3" | while read FILENAME

while read FILENAME
do

#unset ARTIST
#unset TITLE
#unset ALBUM
#unset GENRE
#unset NewFile

#get path - file name - extension

                f=$FILENAME
                path=${f%/*}
                xfile=${f##*/}
                title=${xfile%.*}
                ext=${xfile##*.}
               
        printf "\n\n\n\ this is extension -> $ext\n\n\n\n"
#get needed metadata tag info
ARTIST="`exiftool -Artist "$FILENAME" -p '$Artist'`"
TITLE="`exiftool  -Title  "$FILENAME" -p '$Title'`"
ALBUM="`exiftool  -Album  "$FILENAME" -p '$Album'`"
GENRE="`exiftool  -Genre  "$FILENAME" -p '$Genre'`"

# create parent directory

mkdir -vp "${move_to}"

# create directories needed for file to be placed in

[[ ! -z "${ARTIST}" ]] && ( artist="$copy_to"/"$ARTIST" ; mkdir -pv "${artist}" )
 
[[ ! -z "${ALBUM}" && ! -z "${ARTIST}" ]] && ( ArtistAlbum="$copy_to"/"$ARTIST"/"$ALBUM" ; mkdir -pv "${ArtistAlbum}" )


#Set newFile name for converted flac to mp3
[[ "${ext}" == 'flac' ]] && NewFile="$title"."mp3"

#check for extension type

 printf "next line is to lame flac file\n\n\n\n\n"
 
[[ "${ext}" == 'flac' ]] && flac -cd "$FILENAME" | lame -b 320 - "$NewFile"

# check to be sure file was created

[[ -f "${script_dir}"/"${NewFile}" ]] && resampledFileLoc="${script_dir}"/"${NewFile}"


printf "\n\n\ resampledFileLoc --> $resampledFileLoc\n\n\n\n"

#check to be sure it is there again and check to be sure directories to move it to are there before moving 
[[ ! -z "resampledFileLoc" && -d  "${ArtistAlbum}" ]] && ( mv -v "resampledFileLoc" "${ArtistAlbum}" ; unset ArtistAlbum )


[[ ! -z "resampledFileLoc" && -d "${artist}" ]] && ( mv -v "resampledFileLoc" "${artist}" ; unset resampledFIleLoc )


#[[ ! -z "$ARTIST" ]] && mkdir -vp "$artist"

#[[ ! -z "$ARTIST" && ! -z "$ALBUM" ]] && mkdir -vp "$ArtistAlbum"


[[ "${ext}" == 'mp3' && -d "${ArtistAlbum}" ]] && cp -v "$FILENAME"  "${ArtistAlbum}"

[[ "${ext}" == 'mp3' && ! -d "${ArtistAlbum}" && -d "${artist}" ]] && cp -v "${FILENAME}" "${artist}"



unset ARTIST
unset TITLE
unset ALBUM
unset GENRE
unset NewFile

done < <(find "$working_dir" -type f  \( -name '*.mp3' -o -name '*.flac' \))

it is no longer for what the description says its for. I just kept some of it, and rewrote the body of it to re-sample flac and copy mp3s from somewhere else into a different file structure

NevemTeve 04-07-2017 02:37 PM

Are you sure that every line of this scripts is necessary to show the problem?

BW-userx 04-07-2017 02:57 PM

yes because of this ;;;;;


if you change it to suit your system then run it, using test data one flac and one mp3

you'll see that none of the variables are being set or loses its value when a test returns true.

but look at it.

Code:

+ ALBUM='Marquee Moon '
++ exiftool -Genre '/media/data/Deluge_Done/(1977) Marquee Moon/03. Television - Friction.samantha.mp3' -p '$Genre'
+ GENRE=Post-Punk
+ mkdir -vp /media/data/LostTitle_iPhone_Music

## the varitable has a value so it is true, then it sets artist to equal the path to create
## the directory as told, then loses its value so it cannot be used later on with an mp3


+ [[ -n Television ]]
+ artist=/media/data/Sorted_iPhone-Music/Television
+ mkdir -pv /media/data/Sorted_iPhone-Music/Television


#
#here the value is no longer in the variable
#


+ printf '\n artist 1 --> \n\n'
 artist 1 -->

+ [[ -n Marquee Moon  ]]
+ [[ -n Television ]]
+ ArtistAlbum='/media/data/Sorted_iPhone-Music/Television/Marquee Moon '
+ mkdir -pv '/media/data/Sorted_iPhone-Music/Television/Marquee Moon '


#then its value inside of Variable is lost

+ printf ' \n\n ArtistAlbum 1 -->  \n\n'

the directories are being created

Code:

/media/data/Sorted_iPhone-Music/Television/Marquee Moon
copy paste off file manager to show its existence.

astrogeek 04-07-2017 03:19 PM

I have not been able to make sense of the original question or the following test case.

Please show us this and exactly this so that we can understand it and repeat it...

* Some variable with a value, echo that value
* Your test of that variable from your code which you say is affecting the variable value
* That same variable value after the test at which point you say has lost its value

michaelk 04-07-2017 03:43 PM

Code:

script_dir="~/scripts"
With quotes the ~ is not being expanded to /home/username.

BW-userx 04-07-2017 03:46 PM

Quote:

Originally Posted by astrogeek (Post 5694008)
I have not been able to make sense of the original question or the following test case.

Please show us this and exactly this so that we can understand it and repeat it...

* Some variable with a value, echo that value
* Your test of that variable from your code which you say is affecting the variable value
* That same variable value after the test at which point you say has lost its value

that is all within there already..just got a look for it, I even put it in bold but let me repeat myself just for you....

broke down as this is doing this with every test then set the value then it is lost through out the entire script.

without set -x using printf - results are this
Code:


userx@slackwhere⚡~/scripts $./metadata-restore-mp3s
\ this is extension -> mp3
 artist 1 -->
 
 ArtistAlbum 1 --> 
next line is to lame flac file
\ resampledFileLoc -->

the lines of code are this

Code:

                f=$FILENAME
                path=${f%/*}
                xfile=${f##*/}
                title=${xfile%.*}
                ext=${xfile##*.}
               
        printf "\n\n\n\ this is extension -> $ext\n\n\n\n"
#get needed metadata tag info
ARTIST="`exiftool -Artist "$FILENAME" -p '$Artist'`"
TITLE="`exiftool  -Title  "$FILENAME" -p '$Title'`"
ALBUM="`exiftool  -Album  "$FILENAME" -p '$Album'`"
GENRE="`exiftool  -Genre  "$FILENAME" -p '$Genre'`"

# create parent directory

mkdir -vp "${move_to}"

# create directories needed for file to be placed in

[[ -n "${ARTIST}" ]] && ( artist="$copy_to"/"$ARTIST" ; mkdir -pv "${artist}" )

printf "\n artist 1 --> $artist\n\n"
 
[[ -n "${ALBUM}" && -n "${ARTIST}" ]] && ( ArtistAlbum="$copy_to"/"$ARTIST"/"$ALBUM" ; mkdir -pv "${ArtistAlbum}" )

printf " \n\n ArtistAlbum 1 -->  $ArtistAlbum\n\n"


#Set newFile name for converted flac to mp3
[[ "${ext}" == 'flac' ]] && NewFile="$title"."mp3"

#check for extension type

 printf "next line is to lame flac file\n\n\n\n\n"
 
[[ "${ext}" == 'flac' ]] && flac -cd "$FILENAME" | lame -b 320 - "$NewFile"

# check to be sure file was created

[[ -f "${script_dir}"/"${NewFile}" ]] && resampledFileLoc="${script_dir}"/"${NewFile}"

if [[ -f "${script_dir}"/"${NewFile}" ]] ; then
{

id3v2 -A "$ALBUM" "$resampledFileLoc"
id3v2 -a "$ARTIST" "$resampledFileLoc"
id3v2 -t "$TITLE" "$resampledFileLoc"
id3v2 -g "$GENRE" "$resampledFileLoc"

}
fi


#if metadata is empty use file and directories for tags on re-sampled flac files

 file=$FILENAME
 file=${c##*/}
 album=${c#*"${c%/*/"$file"}"/}
 band=${c#*"${c%/*/"$album"}"/}
 band=${band%%/*}
 album=${album%%/*}

if [[ ! -n "$ALBUM" ]] ; then
        #  folder name of that band album added to orginal file.mp3 as tag info
        id3v2 -A "$album" "${FILENAME}"
  # echo  id3v2 -l ${file} " that is new tag added to "
  fi
 

if [[ ! -n "$ARTIST" ]] ; then
        # folder name of artist/band name added to original file.mp3 as tag info
        id3v2 -a "$band" "${FILENAME}"
    echo $band " added band to file "
        fi
       
        if [[ ! -n "$TITLE" ]] ; then
  # if no song title in tag info then add original file name as tag title info
        id3v2 -t "$pref" "${FILENAME}"
        echo
       
fi


printf "\n\n\ resampledFileLoc --> $resampledFileLoc\n\n\n\n"

none of that is getting processed properly.

set -x out put nd what I got a look at to show me that it is not working.


Code:



userx@slackwhere⚡~/scripts $./metadata-restore-mp3s
+ find /media/data/Deluge_Done -type f '(' -name '*.mp3' -o -name '*.flac' ')'
+ read FILENAME
+ f='/media/data/Deluge_Done/(1977) Marquee Moon/01. Television - See No Evil.samantha.mp3'
+ path='/media/data/Deluge_Done/(1977) Marquee Moon'
+ xfile='01. Television - See No Evil.samantha.mp3'
+ title='01. Television - See No Evil.samantha'
+ ext=mp3
+ printf '\n\n\n\ this is extension -> mp3\n\n\n\n'
\ this is extension -> mp3
++ exiftool -Artist '/media/data/Deluge_Done/(1977) Marquee Moon/01. Television - See No Evil.samantha.mp3' -p '$Artist'
+ ARTIST=Television
++ exiftool -Title '/media/data/Deluge_Done/(1977) Marquee Moon/01. Television - See No Evil.samantha.mp3' -p '$Title'
+ TITLE='See No Evil'
++ exiftool -Album '/media/data/Deluge_Done/(1977) Marquee Moon/01. Television - See No Evil.samantha.mp3' -p '$Album'
+ ALBUM='Marquee Moon '
++ exiftool -Genre '/media/data/Deluge_Done/(1977) Marquee Moon/01. Television - See No Evil.samantha.mp3' -p '$Genre'
+ GENRE=Post-Punk
+ mkdir -vp /media/data/LostTitle_iPhone_Music
+ [[ -n Television ]]
+ artist=/media/data/Sorted_iPhone-Music/Television
+ mkdir -pv /media/data/Sorted_iPhone-Music/Television
+ printf '\n artist 1 --> \n\n'
 artist 1 -->
+ [[ -n Marquee Moon  ]]
+ [[ -n Television ]]
+ ArtistAlbum='/media/data/Sorted_iPhone-Music/Television/Marquee Moon '
+ mkdir -pv '/media/data/Sorted_iPhone-Music/Television/Marquee Moon '
+ printf ' \n\n ArtistAlbum 1 -->  \n\n'
 
 ArtistAlbum 1 --> 
+ [[ mp3 == \f\l\a\c ]]
+ printf 'next line is to lame flac file\n\n\n\n\n'
next line is to lame flac file

using the same code

I do not know what else I can show you that is not already there


I can decipher it

lets me see if I can break it down some more... the whole script is doing this.

Code:


#gets value to put in variable or tested
script_dir="~/scripts"
title=${xfile%.*}
ext=${xfile##*.}

[[ "${ext}" == 'flac' ]] && NewFile="$title"."mp3"

# ext test returns true the Variables is then set by values see above.

next test again looking for ext = flac if yes re-sample change to mp3
[[ "${ext}" == 'flac' ]] && flac -cd "$FILENAME" | lame -b 320 - "$NewFile"

# to be sure that a flac had beed created test to see if that file is in the script dir where it
# gets created. it true then set the path/FileName to the variable for further processing

[[ -f "${script_dir}"/"${NewFile}" ]] && resampledFileLoc="${script_dir}"/"${NewFile}"

#I've changed my non working code so let me put it back as the changes did not effect it into working.

# now that the entire path to and newfile name have been put into a variable  it still needs to be checked
#due to this being procedure and working with two different extensions at once flac and mp3.
# check to see if file is true and that a directory have been created to put it in first before trying to move it.

[[ ! -z "$resampledFileLoc" && -d "${artist}" ]] && ( mv -v "$resampledFileLoc" "${artist}" ; unset resampledFIleLoc )

#by the time it gets to that line of code this Var resampledFileLoc is empty or it never gets filled. and the check on the dir for true #by the process by which it was created this variable artist too is empty so it return null. that value was lost right after it created #the directory.

+ [[ -n Marquee Moon  ]]
+ [[ -n Television ]]
+ ArtistAlbum='/media/data/Sorted_iPhone-Music/Television/Marquee Moon '
+ mkdir -pv '/media/data/Sorted_iPhone-Music/Television/Marquee Moon ' # dir created then value in variable is no longer there.
+ printf ' \n\n ArtistAlbum 1 -->  \n\n'


#the NewFile name var works as when it re-samples a flac proves that because it has the NewFile name value within it, and afterwords then #it is used again -f "${script_dir}"/"${NewFile}" to check the  to be sure that a file was created if a flac was  re-sampled

all of the variable are either not being set or if they are then they are being lost soon afterwords. Except for NewFile

Code:

Encoding <stdin> to 01 - Albatross.mp3
Encoding as 48 kHz j-stereo MPEG-1 Layer III (4.8x) 320 kbps qval=3
+ printf '\n\n\n\n NewFile -- > 01 - Albatross.mp3 \n\n\n\n'

# after its use it the value is still inside of the variable.

 NewFile -- > 01 - Albatross.mp3

this is the entire script that I added some more to it so it will be there when I get this thing working.
Code:

#!/bin/bash

#Take meta data and use it to rename mp3 files taken off iPhone
# then make directories by band name and album.
#Then copy it into same said directories
#accordingly
#Apr. 06, 2017

working_dir="/media/data/Deluge_Done"
copy_to="/media/data/Sorted_iPhone-Music"
move_to="/media/data/LostTitle_iPhone_Music"
script_dir="~/scripts"


set -x




#find "$working_dir" -type f -name "*.m4a" | while read FILENAME

find "$working_dir" -type f  \( -name '*.mp3' -o -name '*.flac' \) | while read FILENAME
#while read FILENAME
do

#get path - file name - extension

                f=$FILENAME
                path=${f%/*}
                xfile=${f##*/}
                title=${xfile%.*}
                ext=${xfile##*.}
               
        printf "\n\n\n\ this is extension -> $ext\n\n\n\n"
#get needed metadata tag info
ARTIST="`exiftool -Artist "$FILENAME" -p '$Artist'`"
TITLE="`exiftool  -Title  "$FILENAME" -p '$Title'`"
ALBUM="`exiftool  -Album  "$FILENAME" -p '$Album'`"
GENRE="`exiftool  -Genre  "$FILENAME" -p '$Genre'`"

# create parent directory

mkdir -vp "${move_to}"

# create directories needed for file to be placed in

[[ -n "${ARTIST}" ]] && ( artist="$copy_to"/"$ARTIST" ; mkdir -pv "${artist}" )

printf "\n artist 1 --> $artist\n\n"
 
[[ -n "${ALBUM}" && -n "${ARTIST}" ]] && ( ArtistAlbum="$copy_to"/"$ARTIST"/"$ALBUM" ; mkdir -pv "${ArtistAlbum}" )

printf " \n\n ArtistAlbum 1 -->  $ArtistAlbum\n\n"


#Set newFile name for converted flac to mp3
[[ "${ext}" == 'flac' ]] && NewFile="$title"."mp3"

#check for extension type

 printf "next line is to lame flac file\n\n\n\n\n"
 
[[ "${ext}" == 'flac' ]] && flac -cd "$FILENAME" | lame -b 320 - "$NewFile"

# check to be sure file was created

[[ -f "${script_dir}"/"${NewFile}" ]] && resampledFileLoc="${script_dir}"/"${NewFile}"

if [[ -f "${script_dir}"/"${NewFile}" ]] ; then
{

id3v2 -A "$ALBUM" "$resampledFileLoc"
id3v2 -a "$ARTIST" "$resampledFileLoc"
id3v2 -t "$TITLE" "$resampledFileLoc"
id3v2 -g "$GENRE" "$resampledFileLoc"

}
fi


#if metadata is empty use file and directories for tags on re-sampled flac files

 file=$FILENAME
 file=${c##*/}
 album=${c#*"${c%/*/"$file"}"/}
 band=${c#*"${c%/*/"$album"}"/}
 band=${band%%/*}
 album=${album%%/*}

if [[ ! -n "$ALBUM" ]] ; then
        #  folder name of that band album added to orginal file.mp3 as tag info
        id3v2 -A "$album" "${FILENAME}"
  # echo  id3v2 -l ${file} " that is new tag added to "
  fi
 

if [[ ! -n "$ARTIST" ]] ; then
        # folder name of artist/band name added to orginal file.mp3 as tag info
        id3v2 -a "$band" "${FILENAME}"
    echo $band " added band to file "
        fi
       
        if [[ ! -n "$TITLE" ]] ; then
  # if no song title in tag info then add orginal file name as tag title info
        id3v2 -t "$pref" "${FILENAME}"
        echo
       
fi


printf "\n\n\ resampledFileLoc --> $resampledFileLoc\n\n\n\n"

#check to be sure it is there again and check to be sure directories to move it to are there before moving 
[[ ! -z "$resampledFileLoc" && -d  "${ArtistAlbum}" ]] && ( mv -v "$resampledFileLoc" "${ArtistAlbum}" )
#; unset ArtistAlbum )


[[ ! -z "$resampledFileLoc" && -d "${artist}" ]] && ( mv -v "$resampledFileLoc" "${artist}" ; unset resampledFIleLoc )
#; unset resampledFIleLoc )


printf "\n\n\n\n here comes the MP3 stuff\n\n\n\n\n"
printf "\n\n\n\n ArtistAlbum --> $ArtistAlbum\n\n"
printf "\n\n artist -- > $artist\n\n"


[[ "${ext}" == 'mp3' && -d "${ArtistAlbum}" ]] && cp -v "$FILENAME"  "${ArtistAlbum}"

[[ "${ext}" == 'mp3' && ! -d "${ArtistAlbum}" && -d "${artist}" ]] && cp -v "${FILENAME}" "${artist}"



#unset ARTIST
#unset TITLE
#unset ALBUM
#unset GENRE
#unset NewFile

done

#< <(find "$working_dir" -type f  \( -name '*.mp3' -o -name '*.flac' \))


BW-userx 04-07-2017 03:58 PM

Quote:

Originally Posted by michaelk (Post 5694019)
Code:

script_dir="~/scripts"
With quotes the ~ is not being expanded to /home/username.

sooo no " " when setting golbals ? ( i think that is what they'd be)
Code:

this
working_dir=/media/data/Deluge_Done
copy_to=/media/data/Sorted_iPhone-Music
move_to=/media/data/LostTitle_iPhone_Music
script_dir=~/scripts
not this
working_dir="/media/data/Deluge_Done"
copy_to="/media/data/Sorted_iPhone-Music"
move_to="/media/data/LostTitle_iPhone_Music"
script_dir="~/scripts"

let me go see what I did a long long time ago in an old script. just to ease my mind.

back

Code:

working_dir="/home/userx/Music"
I used absolute path

better yet
Code:

script_dir="/home/userx/resort-resamplemp3a"
but it still does not explain a few things . let me go back fix that run it again and take a look at it.

BW-userx 04-07-2017 04:23 PM

Just using the Meta Tags example

here we see that YES in fact the value is gotten off the file
Code:


++ exiftool -Artist '/media/data/Deluge_Done/(1977) Marquee Moon/04. Television - Marquee Moon.samantha.mp3' -p '$Artist'
+ ARTIST=Television
++ exiftool -Title '/media/data/Deluge_Done/(1977) Marquee Moon/04. Television - Marquee Moon.samantha.mp3' -p '$Title'
+ TITLE='Marquee Moon'
++ exiftool -Album '/media/data/Deluge_Done/(1977) Marquee Moon/04. Television - Marquee Moon.samantha.mp3' -p '$Album'
+ ALBUM='Marquee Moon '
++ exiftool -Genre '/media/data/Deluge_Done/(1977) Marquee Moon/04. Television - Marquee Moon.samantha.mp3' -p '$Genre'
+ GENRE=Post-Punk
+ printf '\n\n Meta tags -> Television - Marquee Moon - Marquee Moon  - Post-Punk \n\n'
Meta tags -> Television - Marquee Moon - Marquee Moon  - Post-Punk

this data is used to create a directory structure by ARTIST ALBUM using the copy_to= as well

I test to be sure that their is a value within the ARTIST and ALBUM first because I already know their is a value in
copy_to

Code:

copy_to="/media/data/Sorted_iPhone-Music"
[[ -n "${ARTIST}" ]] && ( artist="$copy_to"/"$ARTIST" ; mkdir -pv "${artist}" )

if true then put all of the path into a variable then next using the semi colon create the directory structure which is created.
then what is inside of $artist is immediately lost afterwords.
we see that here.
Code:


+ [[ -n Television ]]
+ artist=/media/data/Sorted_iPhone-Music/Television
+ mkdir -pv /media/data/Sorted_iPhone-Music/Television


# the directory is created yes, then value in side of variable is lost immediately afterwords, which I've coded\
# like this before and never had this happen to be before.
# I can post an old script too if you want me to. :D

+ printf '\n artist 1 --> \n\n'

printf '\n artist 1 --> [ there should be the absolute path right here like this /media/data/Sorted_iPhone-Music/Television ] \n\n'

this s the printf code
Code:

printf "\n artist 1 --> $artist\n\n"

BW-userx 04-07-2017 04:59 PM

THIS WORKS! no variable values are being lost at all..
MIND you this is a completely different script but the same logic and data extraction and test are being used

I dug this one out of all of the old scripts I have, I thought I'd lost it --- but still
Code:

#!/bin/bash

#set -x

count=0 max=0


#void Linux
working_dir="/media/data/Deluge_Done"


#Slackware
#working_dir="/run/media/userx/WorkingDirMP3"

#void Linux
move_to="/media/data/FinishTest"

#Slackware
#move_to="/run/media/userx/iTunesMusic"

script_dir=~/scripts

max="$(find "$working_dir" -type f -name "*.mp3" | wc -l)"

while read FILENAME
do

ARTIST="`exiftool -Artist "$FILENAME" -p '$Artist'`"
TITLE="`exiftool  -Title  "$FILENAME" -p '$Title'`"
ALBUM="`exiftool  -Album  "$FILENAME" -p '$Album'`"
#GENRE="`exiftool  -Genre  "$FILENAME" -p '$Genre'`"

  #unsets formating of string making it left alined.
  set -- $ARTIST
  ARTIST="$@"

  set -- $ALBUM
  ALBUM="$@"
 
  set -- $TITLE
  TITLE="$@"
 
#  set -- $GENRE
#  GENRE="$@"

 #writes name to mp3 tag info

        eyeD3 -a "$ARTIST" "$FILENAME"
        eyeD3 -A "$ALBUM" "$FILENAME"
        eyeD3 -t "$TITLE" "$FILENAME"
        #eyeD3  --remove-all-comments "$FILENAME"
       
  #create and move file into respective areas
[[ -n "$ARTIST" && -n "$ALBUM" ]] && ( copyTo="$move_to/$ARTIST/$ALBUM" ; mkdir -pv "$copyTo" ; cp -v "$FILENAME"  "$copyTo" )
       
        echo "
                        MAX    is $max
                        count  is $((++count))
                                --------
                                  $((max - count))
                "
                   
done < <(find "$working_dir" -type f -name "*.mp3")

KEY POINTS
Code:


[[ -n "$ARTIST" && -n "$ALBUM" ]] && ( copyTo="$move_to/$ARTIST/$ALBUM" ; mkdir -pv "$copyTo" ; cp -v "$FILENAME"  "$copyTo" )

it does not lose its value

but that other one where I combined mp3 and flac which I have done countless times to be processed at the same time is not working properly WHY?

michaelk 04-07-2017 05:54 PM

Does not work as for me either. Try removing the parenthesis.

Code:

[[ -n "${ARTIST}" ]] &&  artist="$copy_to"/"$ARTIST" ; mkdir -pv "${artist}"

astrogeek 04-07-2017 05:57 PM

... none of which answered my request...

The point of troubleshooting is to isolate the point at which some unexpected behavior occurs, and identify the specific cause.

The point of asking for an exact, simplest case is to help you isolate both what is happening and where it is happening.

In the process of attempting to provide those answers you will either provide us the information we need to understand the problem, or often as not, you will find that the problem is not what you thought it was... either way it is progress!

Not attempting to answer those questions as asked leaves us several lengthy posts later, no closer to understanding the actual problem. Changing to another script entirely is also not helpful as the proverbial wild geese typically enter at that point...

The debugging output produced by set -xv is mostly helpful to the person sitting at the keyboard. It is a powerful option to help you interactively isolate and identify what your code is actually doing at runtime. We cannot participate in that interactive debugging process, so posting the debugging output is less helpful to us. What is helpful to us is attempting to provide the results of your own interactive use of that debugging output, the isolated errant code.

You have posited that some variable was losing its value after some test. Repeatedly justifying that conclusion with lengthy debugging output is not helpful. Attempting to isolate the point at which the variable loses its value, as reqquested would be helpful to us and to you.

You will often see people here ask, "Help us help you". This is one of those times.

BW-userx 04-07-2017 06:53 PM

OK RESET EVERY THING I rewrote the entire script and am still getting the same problem.

let me see which first, script or results?

script is this
Bold shows code to print value in variable

Code:

#!/bin/bash

working_dir=/media/data/Deluge_Done

move_to=/media/data/Sorted_iPhone-Music

script_dir=/home/userx/scripts

#set -x

while read FILENAME
do

f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}

ARTIST="`exiftool -Artist "$FILENAME" -p '$Artist'`"
TITLE="`exiftool -Title "$FILENAME" -p '$Title'`"
ALBUM="`exiftool -Album "$FILENAME" -p '$Album'`"
GENRE="`exiftool -Genre "$FILENAME" -p '$Genre'`"

[[ -n "$ALBUM" && -n "$ALBUM" ]] && ( ArtistAlbum="$move_to/$ARTIST/$ALBUM" ; printf " num 1 . ArtistAlbum -> in create dir $ArtistAlbum\n" ;  mkdir -pv "$ArtistAlbum" )

printf " num 2 ArtistAlbum -> $ArtistAlbum\n"

#It does not get this far down in the script because the first file meets the conditions of the first test

[[ "$ext" == 'flac' && -n "$ALBUM" && -n "$ARTIST" ]] && ( NewFile="$title"."mp3" ; flac -cd "$FILENAME" | lame -b 320 - "$NewFile" ; printf "num 3 script_dir - NewFile -> $script_dir/$NewFile\n $ArtistAlbum\n" ;  mv -v "$script_dir/$NewFile" "$ArtistAlbum" \
id3v2 -A "$ALBUM" "$ArtistAlbum/$NewFile" ; id3v2 -a "$ARTIST" "$ArtistAlbum/$NewFile" ; id3v2 -t "$TITLE" "$ArtistAlbum/$NewFile" ; id3v2 -g "$GENRE" "$ArtistAlbum/$NewFile" )


[[ ! -n "$ALBUM" && -n "$ARTIST" ]] && ( Artist="$move_to/$ARTIST" ; mkdir -pv "$Artist" )

printf "Artist - > $Artist\n"

[[ "$ext" == 'flac' && -n "$ARTIST" &&  ! -n "$ALBUM" ]] && ( NewFile="$title"."mp3" ; flac -cd "$FILENAME" | lame -b 320 - "$NewFile" ;  printf "script_dir - NewFile -> $script_dir/$NewFile\n" ; mv -v "$script_dir/$NewFile" "$Artist" \
id3v2 -A "$ALBUM" "$Artist/$NewFile" ; id3v2 -a "$ARTIST" "$Artist/$NewFile" ; id3v2 -t "$TITLE" "$Artist/$NewFile" ; id3v2 -g "$GENRE" "$Artist/$NewFile" )



done < <( find "$working_dir" -type f -name "*.flac" )

results bold shows the variable with value then when it is gone after being used.
Code:

userx@slackwhere⚡~/scripts $./resample-flacy
num 1 . ArtistAlbum -> in create dir /media/data/Sorted_iPhone-Music/Public Image Ltd./Metal Box
num 2 ArtistAlbum ->

flac 1.3.1, Copyright (C) 2000-2009  Josh Coalson, 2011-2014  Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
welcome to redistribute it under certain conditions.  Type `flac' for details.

01 - Albatross.flac: done       
LAME 3.99.5 64bits (http://lame.sf.net)
Resampling:  input 96 kHz  output 48 kHz
Using polyphase lowpass filter, transition band: 20323 Hz - 20903 Hz
Encoding <stdin> to 01 - Albatross.mp3
Encoding as 48 kHz j-stereo MPEG-1 Layer III (4.8x) 320 kbps qval=3
num 3 script_dir - NewFile -> /home/userx/scripts/01 - Albatross.mp3
 
mv: target ' ' is not a directory
Couldn't stat file '/01 - Albatross.mp3'
Couldn't stat file '/01 - Albatross.mp3'
Couldn't stat file '/01 - Albatross.mp3'
Couldn't stat file '/01 - Albatross.mp3'
Artist - >

It is loosing the value in the variable "$ArtistAlbum" it is the target directory but because it lost its value right after it creates the directory. it shows this instead mv: target ' ' is not a directory


now what are your thoughts on this?

sorry but I felt if a rewrite was done it might just work. and be less confusing to read. so I condensed everything for you ;)

looks nice huh? :D

BW-userx 04-07-2017 07:10 PM

I did a reassment on that var and reset it to the proper vaule but am still getting

Code:

+ mv -v '/home/userx/scripts/02 - Memories.mp3' '/media/data/Sorted_iPhone-Music/Public Image Ltd./Metal Box' ' '
mv: target ' ' is not a directory

that ' ' ' I need to figure out where that is coming from.
Code:



[[ "$ext" == 'flac' && -n "$ALBUM" && -n "$ARTIST" ]] && ( NewFile="$title"."mp3" ; flac -cd "$FILENAME" | lame -b 320 - "$NewFile" ; printf "num 3 script_dir - NewFile -> $script_dir/$NewFile\n $ArtistAlbum\n" ;  mv -v "$script_dir/$NewFile" "$ArtistAlbum" \
id3v2 -A
"$ALBUM" "$ArtistAlbum/$NewFile" ; id3v2 -a "$ARTIST" "$ArtistAlbum/$NewFile" ; id3v2 -t "$TITLE" "$ArtistAlbum/$NewFile" ; id3v2 -g "$GENRE" "$ArtistAlbum/$NewFile" )

it has to be somewhere between that bold part maybe the \ is too far spaced out. checking....


the reassignment btw
Code:


ArtistAlbum="$move_to/$ARTIST/$ALBUM"

printf " num 2 ArtistAlbum -> $ArtistAlbum\n"



All times are GMT -5. The time now is 11:25 PM.