[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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"
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 thanks
someone submit a bug report -- for one liner conditionals losing variable value after first use. not me I'm busy.
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.
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.
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.
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 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...
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.
Last edited by astrogeek; 04-08-2017 at 02:14 AM.
Reason: typo - added comment, typos, typos, typos...
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"
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"
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.
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.
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.
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
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.
**** 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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.