LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2017, 11:57 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

so now cut off what I do not need else skip it.


http://tldp.org/LDP/GNU-Linux-Tools-...tml/x11655.htm

googling "Pattern testing bash" while awaiting a rely

found this basically telling me to do what I am doing
https://www.safaribooksonline.com/li...4/ch06s07.html

if you read the text you'll see the ? and telling about it

Last edited by BW-userx; 04-08-2017 at 12:08 PM.
 
Old 04-08-2017, 12:52 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Assuming you always want it removed, just issue the remove and if it is there it will be gone and if not, no harm no foul:
Code:
TRACK=${TRACK%/*}
 
2 members found this post helpful.
Old 04-08-2017, 01:19 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
Assuming you always want it removed, just issue the remove and if it is there it will be gone and if not, no harm no foul:
Code:
TRACK=${TRACK%/*}
I have a habit of looking before jumping...

is that a bad thing?

here is my fix - I just didn't post it yet. until right this second.


Code:
REGEX="^[0-99]"/"[0-99]*$"

[[ "$TRACK" =~ $REGEX ]] && ( printf "got it\n" ;  track=${TRACK%/*} )
 
Old 04-08-2017, 10:47 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
Old 04-09-2017, 02:00 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
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

Last edited by BW-userx; 04-09-2017 at 02:02 PM.
 
Old 04-09-2017, 02:43 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
You do not need an extra { } block in an if-then block
Code:
if [ "vslue" == "Pattern" ]
then
  issue command
  issue command
  issue command
fi
You *can* use an extra block like this
Code:
[ "vslue" == "Pattern" ] && {
  issue command
  issue command
  issue command
}
Also note that [ ] is a test command with arguments that are subject to word splitting and globbing.
The [[ ]] compound behaves differently.

Last edited by MadeInGermany; 04-09-2017 at 02:48 PM.
 
Old 04-09-2017, 02:45 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by MadeInGermany View Post
You do not need an extra { } block in an if-then block
Code:
if [ "vslue" == "Pattern" ]
then
  issue command
  issue command
  issue command
fi
Quote:
Originally Posted by MadeInGermany
You *can* use an extra block like this
Code:
[ "vslue" == "Pattern" ] && {
  issue command
  issue command
  issue command
}
no semi-colon separators needed?
more then one way to skin a cat...
I'll give that one a try.. tks

Last edited by BW-userx; 04-09-2017 at 02:48 PM.
 
Old 04-09-2017, 10:25 PM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Code:
if [ "vslue" == "Pattern" ]  ; then
this statement will always be false.
also i suspect wrong usage of [ and =.
 
Old 04-10-2017, 08:33 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by ondoho View Post
Code:
if [ "vslue" == "Pattern" ]  ; then
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.

Last edited by BW-userx; 04-10-2017 at 09:08 AM.
 
Old 04-10-2017, 03:01 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
@BW-userx:

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.
 
Old 04-10-2017, 03:13 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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?

have you even heard the expressions

a tree is known by its fruit

An apple does not fall far from the tree

like two peas in a pod

two wrongs do not make a right

That is all I have to say

Last edited by BW-userx; 04-11-2017 at 08:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash conditional | getting logic wrong? the_gripmaster Programming 5 03-16-2013 03:16 AM
LSI Logic / Symbios Logic 53c875 (rev 14) -> HP Storageworks 1/8 G2 gileravxr Linux - Hardware 0 07-21-2009 04:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration