Variable lost in loop
by pedropt from LinuxQuestions.org on (#5R292)
I have been looking into the code all day and i can not find the reason for this .
Maybe some people here have a different output .
One of my variables do not show up when i use echo to write in on a file , but if i call that variable alone in the code to display only then it will show up correctly .
I am building this script to catalog all music files in my database , this is only a portion of it because the issue is here in this part .
What does this full script does :
This full script will search for m3u , cue , flac and wav files over specific locations (pre configured) or single location depending on user desire , after catching all that data will put all those directories in one file and then will start executing each directory on wich file is there , case is a cue file then will run a cue function , m3u file will run m3u function , etc ... , after parsing all that from cue , m3u file will write the artists and song names to a database.csv
the information writen on the database is :
Artist name , Song name , Location of that file in the server
I dont know why , but i can only get the location of the files written in database , the artist and song name are not written when i use echo with the variables to write in $db , but if before writing i call after each sed call what it have inside that variable then it is fine .
an example of what is going on on the first part of the script i wrote is something like this :
Quote:
Variable $drd its the location on my system where that file is .
$art = file art.tmp -> look image for output of them
$song = file song.tmp -> look image for output of them
Code:chkart=$(wc -l "$art" | awk '{print$1}')
if [[ "$chkart" -ge "2" ]]
then
sed -i -e '1d' "$art"
cntsng=$(wc -l "$song" | awk '{print$1}')
for i in $(seq "$cntsng")
do
# ITs various artists
performer=$(sed -n "${i}p" "$art")
title=$(sed -n "${i}p" "$song")
chkdb=$(wc -c "$db" | awk '{print$1}')
if [[ "$chkdb" -eq "0" ]]
then
echo "$performer - $title , $drd" > "$db"
else
echo "$performer - $title , $drd" >> "$db"
fi
done
else # Album
cntnew=$(wc -l "$song" | awk '{print$1}')
for i in $(seq "$cntnew")
do
title=$(sed -n "${i}p" "$song")
performer=$(sed -n 1p "$art")
chkdb=$(wc -c "$db" | awk '{print$1}')
if [[ "$chkdb" -eq "0" ]]
then
echo "$performer - $title , $drd" > "$db"
else
echo "$performer - $title , $drd" >> "$db"
fi
done
fiIn the image i cat the files that sed was reading and everything is fine , but the output data.csv just show 1 variable , and not all 3 .
On my database output i only get the location where that specific file is , i dont get artist name and song .
Check the image attached
Attached Thumbnails
Maybe some people here have a different output .
One of my variables do not show up when i use echo to write in on a file , but if i call that variable alone in the code to display only then it will show up correctly .
I am building this script to catalog all music files in my database , this is only a portion of it because the issue is here in this part .
What does this full script does :
This full script will search for m3u , cue , flac and wav files over specific locations (pre configured) or single location depending on user desire , after catching all that data will put all those directories in one file and then will start executing each directory on wich file is there , case is a cue file then will run a cue function , m3u file will run m3u function , etc ... , after parsing all that from cue , m3u file will write the artists and song names to a database.csv
the information writen on the database is :
Artist name , Song name , Location of that file in the server
I dont know why , but i can only get the location of the files written in database , the artist and song name are not written when i use echo with the variables to write in $db , but if before writing i call after each sed call what it have inside that variable then it is fine .
an example of what is going on on the first part of the script i wrote is something like this :
Quote:
a=some.file b=other.file if i want 2 variables for 2 calls i will make sed do , usually i will write : var1=$(sed -n 3p "$a") -> this will read line 3 of file some.file then i echo that variable to be written like : echo "$var1" > another.file in normal circunstance this works fine , but in my script ahead i can not get those values written in database or $db , only the 3rd , and i checked the files and everything is fine , no empty spaces , no empty lines , nothing . I just loose the variables in echo command without reason . |
$art = file art.tmp -> look image for output of them
$song = file song.tmp -> look image for output of them
Code:chkart=$(wc -l "$art" | awk '{print$1}')
if [[ "$chkart" -ge "2" ]]
then
sed -i -e '1d' "$art"
cntsng=$(wc -l "$song" | awk '{print$1}')
for i in $(seq "$cntsng")
do
# ITs various artists
performer=$(sed -n "${i}p" "$art")
title=$(sed -n "${i}p" "$song")
chkdb=$(wc -c "$db" | awk '{print$1}')
if [[ "$chkdb" -eq "0" ]]
then
echo "$performer - $title , $drd" > "$db"
else
echo "$performer - $title , $drd" >> "$db"
fi
done
else # Album
cntnew=$(wc -l "$song" | awk '{print$1}')
for i in $(seq "$cntnew")
do
title=$(sed -n "${i}p" "$song")
performer=$(sed -n 1p "$art")
chkdb=$(wc -c "$db" | awk '{print$1}')
if [[ "$chkdb" -eq "0" ]]
then
echo "$performer - $title , $drd" > "$db"
else
echo "$performer - $title , $drd" >> "$db"
fi
done
fiIn the image i cat the files that sed was reading and everything is fine , but the output data.csv just show 1 variable , and not all 3 .
On my database output i only get the location where that specific file is , i dont get artist name and song .
Check the image attached
Attached Thumbnails