LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-14-2014, 12:02 PM   #1
sparklyballs
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.


#!/bin/sh

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

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, 12:26 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,105
Blog Entries: 10

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

Code:
if [ a == b ]; then
    if [ a > c ]; then
        # do something
    else
        # do something different
    fi
fi
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 12:29 PM.
 
1 members found this post helpful.
Old 01-14-2014, 10:48 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
& = bitwise
&& = logical

You'll want the latter I'd think.

Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

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 http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 01-14-2014, 11:57 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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:
Code:
date -d '2 year ago' '+%Y'
 
Old 01-22-2014, 06:32 AM   #5
sparklyballs
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.


#!/bin/sh

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
*$curyear*)
echo "[NZB] PRIORITY=50";;
*$lastyear*)
echo "[NZB] PRIORITY=50";;
*$twoyearsago*)
echo "[NZB] PRIORITY=50";;
esac
fi

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


Reply


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


All times are GMT -5. The time now is 08:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration