Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-14-2014, 11:02 AM   #1
LQ Newbie
Registered: Jan 2014
Posts: 2

Rep: Reputation: Disabled
Question Help with if statements.

So far I have this script for setting a priority for TV shows in preprocessing NZB's in Nzbget.


if [ "$NZBNP_CATEGORY" = "series" ]; then
echo "[NZB] PRIORITY=100" <<<<<<<<< sets the priority in Nzbget

which is fairly easy and works , but what i'm looking to do is have broader script that will incorporate this and then set movies as priority 50 based on year which is specified in a file name and anything else that isn't either a tv show or a movie of the 3 years i'm interested in to be set at priority 0.

As I want it to very much set and forget i propose generating the years to match like this,

curyear=`date +%Y`
lastyear=$((`date +%Y` -1))
2yearsago=$((`date +%Y` -2))

so my first question is, do i have the syntax right here ?

and then the big question, how do i tie the first script together with these variables to achieve this....

if a file matches against category series (which means it's a tv show) set priority to 100, and if it matches against category movies AND one of the 3 years i'm interested in set priority to 50, and anything else set priority to 0.

Any help greatly appreciated.
Old 01-14-2014, 11:26 AM   #2
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,595
Blog Entries: 12

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
I think there are some gotchas in the syntax of using things like singular & versus double &&.

See this, Stack Overflow Question I found about compound if statements and what you'll see are a few different stylistic recommendations from the answerers. And further you'll see that many of them qualify their answers by saying "for numbers" or "for strings" and one or two of them show test scripts to prove their solution/recommendation.

My recommendation is that you determine logically what you need to test for and you can nest if-elseif-else in the bash script, like:

if [ a == b ]; then
    if [ a > c ]; then
        # do something
        # do something different
And that you test the results incrementally to build your script. If you try to write it all in a few brief, killer lines it may take a lot to debug when writing it expanded, but clearer may just get done what you intend.

As far as whether or not you have the syntax correct with each of your determinations of dates; you've assigned this information to variables $curyear, $lastyear, and $2yearsago. Echo that information out and you'll see if your syntax is correct for what you intend to see. Also, I personally do not recommend starting any variable name with a number; instead do twoyearsago, or consider TwoYearsAgo, twoYearsAgo, or two_years_ago; something like that.

Last edited by rtmistler; 01-14-2014 at 11:29 AM.
1 members found this post helpful.
Old 01-14-2014, 09:48 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
& = bitwise
&& = logical

You'll want the latter I'd think.

Useful links

Do notice that string comparators are different from numeric ones; ensure you use the correct ones to avoid unexpected results.
I do recommend [[ ]] over [ ] as per
Old 01-14-2014, 10:57 PM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Another gotcha to be wary of is if there are other choices, ie you mention that a category could be either series or movies ... are there any other choices?

This changes your 'if' statement from using a simple if / else structure to requiring an if / elif structure (or perhaps consider a case statement)

Also, for command substitution, pick a format and stay with it, ie you use both backticks (``) and $() method. As for the latter, you only require a single set of brackets as date can perform the math for you:
date -d '2 year ago' '+%Y'
Old 01-22-2014, 05:32 AM   #5
LQ Newbie
Registered: Jan 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
This is the solution that I eventually went with and it works nicely, I got a little bit of help from every answer on this thread.

Thanks guys, (BTW for "everything else", turns out wasn't required to script for as normal is the default priority for anything added to the NZBget Queue.


curyear=`date +%Y`
lastyear=date -d '1 year ago' '+%Y'
twoyearsago=date -d '2 year ago' '+%Y'

if [ "$NZBNP_CATEGORY" = movies ] ; then
case "$NZBNP_NZBNAME" in
echo "[NZB] PRIORITY=50";;
echo "[NZB] PRIORITY=50";;
echo "[NZB] PRIORITY=50";;

if [ "$NZBNP_CATEGORY" = series ] ; then
echo "[NZB] PRIORITY=100"


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] if statements Chupakabra Linux - Newbie 1 07-28-2011 04:09 PM
[SOLVED] if statements wtruong Programming 1 01-07-2010 03:56 PM
if else statements 0.o Programming 7 09-27-2005 02:59 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
if statements thesnaggle Linux - Software 1 02-16-2004 09:52 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:03 PM.

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