[SOLVED] A little help on logic with conditional testing using wildcards
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.
A little help on logic with conditional testing using wildcards
I need to check to see if a track number has two separate number separated with a /
if yes then strip off the ending and leave only the track number without the total number.
example
track=1/20 ends with track=1
This might just be the wrong way of testing before stripping.
a complete working test script for only the purpose of testing this part of what is needed within a bigger script. So please do not ask me to rewrite it to only show what is causing an error. thank you
Code:
#!/bin/bash
working_dir="/media/data/iTune_Music/09 The Albion Punk Years (1996)"
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'`"
TRACK="`exiftool -Track "$FILENAME" -p '$Track'`"
#if TRACK number is 1/32 number track out of total number 3/20
#then cut off the total and the separator /
# wildcard usage is question mark ?
#? (question mark)
#this can represent any single character.
#If you specified something at the command line like "hd?"
#GNU/Linux would look for hda, hdb, hdc and every other
#letter/number between a-z, 0-9.
[[ "$TRACK" == '?/?' ]] && ( track=${TRACK%/*} ; printf "Track = $track\n" )
[[ "$TRACK" == "?/?" ]] && ( track=${TRACK%/*} ; printf "Track = $track\n" )
printf "$TRACK track"
# set to exit on first Var that has a value in order to stop test script.
[[ -n "$TRACK" ]] && exit 0
done < <(find "$working_dir" -type f \( -name '*.mp3' -o -name '*.MP3' \))
the MP3 file showing its true track value.
Code:
+ TRACK=1/20
set -x bold is the important parts.
Code:
userx@slackwhere⚡~/scripts $./fix-track-number-sniplet
+ read FILENAME
++ find '/media/data/iTune_Music/09 The Albion Punk Years (1996)' -type f '(' -name '*.mp3' -o -name '*.MP3' ')'
+ f='/media/data/iTune_Music/09 The Albion Punk Years (1996)/01 Obsessed.MP3'
+ path='/media/data/iTune_Music/09 The Albion Punk Years (1996)'
+ xfile='01 Obsessed.MP3'
+ title='01 Obsessed'
+ ext=MP3
++ exiftool -Track '/media/data/iTune_Music/09 The Albion Punk Years (1996)/01 Obsessed.MP3' -p '$Track'
+ TRACK=1/20+ [[ 1/20 == \?\/\? ]]
+ [[ 1/20 == \?\/\? ]]
+ printf '1/20 track'
1/20 track+ [[ -n 1/20 ]]
+ exit 0
userx@slackwhere⚡~/scripts $
may I ask then what is the right syntax to have the test use the wildcard so that it does not matter what the numbers are. only that if their is such a value that has numbers on both sides and a / between them to separate them then it is a Match
As I said, the test is superfluous for any real gain. That being said, why all the extra quotes? It also will not work as expected. You are now using regex and not globbing, so [0-99] will still only return
a single digit. Of course as you are not using the found information for anything it probably doesn't matter.
So here is what I would have done, had I used a regex:
Code:
REGEX='^[0-9]+/[0-9]+$'
The other side to look at here is all you are really interested in is, is there a slash in the variable. So the regex could simply be:
Code:
REGEX='/'
Either of these will yield the same result, again as you are not doing anything with the matched data.
Lastly, by placing the work inside () you have now created a subshell which means that the assignment to 'track' will be lost the moment you leave that line.
As I said, the test is superfluous for any real gain. That being said, why all the extra quotes? It also will not work as expected. You are now using regex and not globbing, so [0-99] will still only return
a single digit. Of course as you are not using the found information for anything it probably doesn't matter.
So here is what I would have done, had I used a regex:
Code:
REGEX='^[0-9]+/[0-9]+$'
The other side to look at here is all you are really interested in is, is there a slash in the variable. So the regex could simply be:
Code:
REGEX='/'
Either of these will yield the same result, again as you are not doing anything with the matched data.
Lastly, by placing the work inside () you have now created a subshell which means that the assignment to 'track' will be lost the moment you leave that line.
yeah I put it into a conditional code block instead.
Code:
if [ "vslue" == "Pattern" ] ; then
{
issue command
issue command
issue command
}
fi
this statement will always be false.
also i suspect wrong usage of [ and =.
I cannot justify why I placed a simple non working example (pseudo code) showing a comparison '==' between two values and not an assignment '=' without having to fear being falsely accused by someone or someone(s) in here of me being rude to or offensive or off topic or whatever other words they can think of that give a negative connotation to someones actions just so they can justify to themselves that their false claims against me or others is rightfully handed out, then after being judged by them I and others are then condemned by them.
if they are even proven to be wrong in their judgments they do not care and stick to line of error in beliefs. Making themselves unjust persons.
as they become the third person it no longer matters who is right or wrong it is always the one that is right between the two that they pass judgment against stating they are wrong, and then condemn them unjustly. because they have no real understanding on life itself on how it works.
If you or anyone wants to discus pseudo code .....
this post has been marked solved. I'd close it if I could.
I'd even post my defense in here but then there is a 99% chance that someone would judge me as being rude for showing you that you are wrong and trying to correct you in your belief, but you have nothing to fear for trying to do just that with me.
because that is how the man rolls in here. you should know that first hand.
so I will PM you on this matter @ondoho proving my case in defense of myself showing you that are are the one in error not I.
Please keep your comments on-topic and refrain from spreading your displeasure across unrelated threads.
If you feel that you have been treated unfairly use the report button to complain, or contact Jeremy directly. Do not clutter these forums with self-justifications and needless self-defense arguments.
Stop this behavior, now.
Truth is more than a word in a sig. The truth of every member's posting history is available for all to see, and speaks for itself. Please remember that each time you post, you add to that record. Make of it what you will.
please stop making me have to defend and justify myself to you only to be marked guilty by you no matter what I prove.
I gave you truth then you run and hide behind what?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.