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 07:15 PM

ahhhhh now I am getting crazy stuff

I moved the space between the ; "command" \next line
then hit enter to put it on the next line and now I am getting this.
Code:

; "command \
next line

Code:

+ mv -v '/home/userx/scripts/03 - Poptones.mp3' '/media/data/Sorted_iPhone-Music/Public Image Ltd./Metal Box' id3v2 -A 'Metal Box' '/media/data/Sorted_iPhone-Music/Public Image Ltd./Metal Box/03 - Poptones.mp3'
mv: invalid option -- 'A'
Try 'mv --help' for more information.

I have no idea where it is seeing I have an option 'A' in there. Just look back at the code. nothing changed but the spacing.

on I look at it again in the post.. I can see what it is now. got a go check it. prob forgot a ';' semi-colon

michaelk 04-07-2017 07:24 PM

I would suggest not using conditional execution and go back to a regular if then statement.
Code:

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


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

Quote:

Originally Posted by michaelk (Post 5694102)
I would suggest not using conditional execution and go back to a regular if then statement.
Code:

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


naaa that is no fun it is made to work like I wrote it. I read up on it.

the only puzzle here is why did ArtistAlbum lose it's value after it was used once?

if I do your code then another printf is needed to see if that way does not lose ArtistAlbum value after the mkdir. if it does then what's UP?

I'll get back to you on this.

as it stand it is now working as it should. with my conditional executions one liners.


Code:

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


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

just for a follow up on @michaelk advice

used his conditional block statement
Code:

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

printf " num 4343 ArtistAlbum -> in create dir $ArtistAlbum\n"

Positive results were positive no reassignment of variable needed.

Code:

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

still keeping my one liners :D thanks


someone submit a bug report -- for one liner conditionals losing variable value after first use. not me I'm busy. :rolleyes:

working script for the masses to see what I did to fix it. and make note that @michaelk works too. Moral of the story. There is more then one way to skin
a cat (methodology) kids. :hattip:


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" )


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


[[ "$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" )

 Artist="$move_to/$ARTIST"

[[ "$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" )


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

Quote:

Originally Posted by astrogeek (Post 5694079)
... 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.

I showed my code that was giving me bad results and I showed what set -x was showing not set -xv but set -x which shows values in variables. that showed the results of said code.

no more or less than I have to work with to figure it out. so I do not understand your statements

which was as it turns out only one issue that I'd messed up on.

you say to not rewrite the code it is like chasing ducks another asks me to rewrite it so it only shows what is erroring.

rewriting what I rewrote it fixed a lot of it until I did get it down to just one issue that someone else helped me with. that was not the one I posted on but never the less it was still there within that code.

More than one were error-ing in losing value so to isolate it when everything is leaking still takes more then you expected me to show you.

BW-userx 04-07-2017 08:06 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

I could not make sense out of it either perhaps that is why you could not. But that is why I posted it.

NevemTeve 04-07-2017 11:42 PM

You were asked to provide a minimal complete example. You don't know what this means, or simply don't feel like doing it?

astrogeek 04-08-2017 01:50 AM

I set aside some time to re-read the entire thread to try to make sense of the problem. I am confident that I now see the source of some, hopefully the, "lost" variable values.

Limiting the test cases to the code and debug data posted in #3 and assuming that the "it" you refer to is either or both of artist and ArtistAlbum when you say...

Quote:

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
You set artist in a subshell in line 49...

Code:

[[ ! -z "${ARTIST}" ]] && ( artist="$copy_to"/"$ARTIST" ; mkdir -pv "${artist}" )
...then test for it in lines 74 and 84...

Code:

[[ ! -z "resampledFileLoc" && -d "${artist}" ]] && ( mv -v "resampledFileLoc" "${artist}" ; unset resampledFIleLoc )
...
[[ "${ext}" == 'mp3' && ! -d "${ArtistAlbum}" && -d "${artist}" ]] && cp -v "${FILENAME}" "${artist}"

You set the value of artist inside a subshell, (...), it is NEVER set in the shell in which the main body of the script is running, and where the tests are executed. To fix that, rethink the parenthesis as suggested by michaelk in post #12.

Also, unset resampledFIleLoc has no effect outside the subshell in which it is executed in that line.

Same for ArtistAlbum, you set it in line 51, inside a subshell...

Code:

[[ ! -z "${ALBUM}" && ! -z "${ARTIST}" ]] && ( ArtistAlbum="$copy_to"/"$ARTIST"/"$ALBUM" ; mkdir -pv "${ArtistAlbum}" )
...then test for it in lines 71 (and unset it in another subshell...), 82 and 84...

Code:

[[ ! -z "resampledFileLoc" && -d  "${ArtistAlbum}" ]] && ( mv -v "resampledFileLoc" "${ArtistAlbum}" ; unset ArtistAlbum )
...
[[ "${ext}" == 'mp3' && -d "${ArtistAlbum}" ]] && cp -v "$FILENAME"  "${ArtistAlbum}"

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

There are still a few un-tamed geese in this thread and the snippets quoted above, but I suspect these cases cover the original problem.

I will ask again that in future you please try to be more precise in your problem descriptions (refer to things explicitly by name and avoid using vague and ambiguous references such as "it" above). And post debugging output sparingly - it is mostly useful to you interactively and often clutters our ability to see the actual problem.

Make effort to reduce the problem to some simplest, minimal test case which demonstrates it clearly (using the debug output), then post that simplest case if the problem still exists.

And please try to respond to requests for specific information with that specific information. Reducing a problem to a simplest example will almost always lead directly to the problem, as I think it would have done here.

Again, please help us help you, and respect the time and efforts of others by helping them to apply their effort efficiently.

ondoho 04-08-2017 01:58 AM

Quote:

Originally Posted by BW-userx (Post 5694116)
Code:

if [[ -n "$ALBUM" && -n "$ALBUM" ]];

what is this supposed to accomplish?

pan64 04-08-2017 02:16 AM

you need to use { } instead of ( ), that will probably solve some of your issues.
If that was unclear: ( something ) means something will be executed in a new shell (which is actually a child of the "current" shell - or just named subshell). This subshell cannot reach/access the environment of its parent, so for example cannot change its variables.

And another tip (which will not solve anything, just will simplify things a bit):
Code:

# instead of:
ArtistAlbum="$copy_to"/"$ARTIST"/"$ALBUM"
# you can write:
ArtistAlbum="$copy_to/$ARTIST/$ALBUM"

here:
Code:

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

you just missed the $ in front of the variable names.

BW-userx 04-08-2017 07:45 AM

Quote:

Originally Posted by pan64 (Post 5694210)
you need to use { } instead of ( ), that will probably solve some of your issues.
If that was unclear: ( something ) means something will be executed in a new shell (which is actually a child of the "current" shell - or just named subshell). This subshell cannot reach/access the environment of its parent, so for example cannot change its variables.

And another tip (which will not solve anything, just will simplify things a bit):
Code:

# instead of:
ArtistAlbum="$copy_to"/"$ARTIST"/"$ALBUM"
# you can write:
ArtistAlbum="$copy_to/$ARTIST/$ALBUM"

here:
Code:

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

you just missed the $ in front of the variable names.

This is already been marked solved. please go look at post # 19 to see the entire script in working order. :D

as you stated that is what I was going to put to the test today. Having given it more thought after I posted is to try the { } vs ( ) because I'd remembered something about what I read said something about how ( ) creates another shell and { } does not. so if I remember correctly, when that happens and a Variable is used it cannot pass values between them.

like loops cannot passing values into another loop if I think it is from out side loop to inside loop, and from an inside loop it can be pass to the outside loop, or visa versa. I do not do that enough to remember exactly.

Only that the ( ) creates a separate shell so when the Variable was set then used when it came back its value was lost due to that effect.

I was going to test that today...

BW-userx 04-08-2017 08:02 AM

Quote:

Originally Posted by ondoho (Post 5694206)
what is this supposed to accomplish?

cause due to hack job, copy paste, as I stated in a post in here. I hacked this together.

As well as got yelled at for changing my code while some others (you included) were trying to figure it out.

well I didn't feel like sitting here with my thumb up my butt and letting everyone figure out all of my mistakes for me when I have the abilities to help figure out my own mistakes at the same time. That is why I posted my changes to show the progress and to update them that were trying to help.

I got a lecture out of that one from someone in here.

BW-userx 04-08-2017 08:20 AM

Quote:

Originally Posted by NevemTeve (Post 5694175)
You were asked to provide a minimal complete example. You don't know what this means, or simply don't feel like doing it?

I gave out all of the information needed to have someone else see what I did not.

I gave the proper example that is to be used to do what I was doing and sighted the web page I got it from.
Post #1
Quote:

if [ -f /var/log/messages ]
then
echo "/var/log/messages exists."
fi
I gave out the proper offending code that was causing the error
post #1
Quote:

[[ -f "${script_dir}"/"${NewFile}" ]] && resampledFileLoc="${script_dir}"/"${NewFile}"
I gave out the proper error message to show how it was producing the error
post #1
Quote:

+ [[ -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 '' ]]
then you asked me to rewrite it into a script that will isolate that error. what part of that can't you use to figure it out without me having to do this.

Code:

#!/bin/bash

script_dir="~/scripts"



# to egt the file name so I can rename it
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##*.}

#Set newFile name for converted flac to mp3

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

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

done

just so you can see the same error from set -x which would be the same thing

right here
post #1
Quote:

+ [[ -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 '' ]]
where
Quote:

Quote:

Originally Posted by michaelk (Post 5694019)
Code:

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


michaelk picked up on it. using what I gave out. That set me in the right direction to going back to using absolute path instead.

so your request holds no water and I seen that as soon as I seen your request, and to me is was waste of time to have obeyed your request. so I did not. Due to what I already stated.

as well as I would have done just what I've done to someone script to try and help them out with it if needed.

I'll write out their script and try to get it to work, and if I do I'll show them how I did it and sometimes left out something or a clue on how to and tell them about it, so they can use their brain as well. In hopes that they learn how to use their own brain to figure out problems on there own instead of always relying on others to figure out all of their problems for them.


that is not to say that everyone just needs to just figure their own problems out themselves as everyone does need a little help now and then.

P.S> and yes there was more to do, and I got yelled at (or a stern talking to) for doing so. along with the all of you.

BW-userx 04-08-2017 08:22 AM

**** please note that this post has been marked solved. I am not going to waste anymore electronic paper on it. *****

it was marked solved at post # 19
the only thing I am going to do is test

{ } vs ( )

that I thought of yesterday after singing off for the day, and then someone seen it too and posted about it in here as well. After I marked it solved. Which only confirms my suspicions on it.

[ points given to them that I gave them to ]
Thank you
and
Happy Canonic
:party:

NevemTeve 04-08-2017 09:31 AM

Additional reading: http://stackoverflow.com/help/mcve


All times are GMT -5. The time now is 08:45 PM.