LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-14-2015, 11:25 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 261

Rep: Reputation: 3
defining a 3rd (and fourth) if statement


I am using the script below to play music from predefined folders. If it's a week day I play from $morning folder and if it's a weekend I play from $weekend folder.

Quote:
if [[ $(date +u) -lt 6 ]] ; then
/usr/bin/mpg123 -Z -q -f $volume $morning/* &
else
/usr/bin/mpg123 -Z -q -f $volume $weekend/* &
fi
I need to somehow play music from $evening folder, only if its after 3pm and only if its a weekday.

Can I get some help on doing that?

Thanks.
 
Old 08-14-2015, 12:07 PM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
You ought to be able to use a nested if. Something like this:
Code:
#!/bin/bash

# Check if weekday or not
if [[ $(date +u) < 6 ]]; then
    if [[ $(date +%H) > 15 ]]; then
        echo "A weekday after 3pm (15:00 hours)."
        echo "Play from evening directory"
    else
        echo "A weekday before 3pm (15:00 hours)"
    fi  
else
    echo "Play from weekend directory"
fi
Upon execution (now, at fri 14 aug 2015 19:06:25 CEST), I get this result:
Code:
$ ./check_day_time.sh 
A weekday after 3pm (15:00 hours).
Play from evening directory
If I simulate -7 hours:
Code:
#!/bin/bash

# Check if weekday or not
if [[ $(date +%u) < 6 ]]; then
    #if [[ $(date +%H) > 15 ]]; then
    if [[ $(date -d '7 hour ago' "+%H") > 15 ]]; then
        echo "A weekday after 3pm (15:00 hours)."
        echo "Play from evening directory"
    else
        echo "A weekday before 3pm (15:00 hours)"
    fi  
else
    echo "Play from weekend directory"
fi
I get this result.
Code:
./check_day_time.sh 
A weekday before 3pm (15:00 hours)
And finally, if I simulate a weekend:
Code:
!/bin/bash

# Check if weekday or not
#if [[ $(date +%u) < 6 ]]; then
if [[ $(date -d '+1 day' "+%u") < 6 ]]; then
    #if [[ $(date +%H) > 15 ]]; then
    if [[ $(date -d '7 hour ago' "+%H") > 15 ]]; then
        echo "A weekday after 3pm (15:00 hours)."
        echo "Play from evening directory"
    else
        echo "A weekday before 3pm (15:00 hours)"
    fi  
else
    echo "Play from weekend directory"
fi
I get this result:
Code:
$ ./check_day_time.sh 
Play from weekend directory
Best regards,
HMW

Last edited by HMW; 08-14-2015 at 12:19 PM. Reason: Added bold, added echo statement
 
  


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
Running mysql from ssh & query statement has a Text Header in the SELECT statement? djlerman Linux - Server 6 11-19-2013 06:33 PM
How to create folders in 2nd and 3rd partions and how to mount /home on 3rd Partition AnApproach2DigestLinux Linux - Newbie 1 12-06-2012 10:25 PM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Defining loop statement for creating new file each time neo009 Linux - Newbie 6 04-12-2009 06:49 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:27 PM.

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