LinuxQuestions.org
Help answer threads with 0 replies.
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 07-14-2014, 04:38 PM   #1
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Rep: Reputation: Disabled
Friday the 13th bash script


Ok here goes for the past 2 weeks I have been looking for anything that will help. I have to create a bash script using Ubuntu with terminal commands. The script must check if today is a friday and if it is say today is friday. I got that at least I think I do. The second part is to check if today is Friday the 13th and if it is say Today is Friday the 13th. All this in one bash script. I have seen all the date commands as seen in my code I just can't find anything that actually explains in detail what I am trying to do. Please help and please explain in detail so that I will forever understand. Thank you here is my code.

#!/bin/bash

TODAY=$(date +%a)
echo "Today is $TODAY"
if [$TODAY = "Friday" ]
then
echo "Today is Friday"
if [$TODAY = "Friday the 13th" ]
echo "Today is Friday the 13th"
 
Old 07-14-2014, 04:48 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,816

Rep: Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346
Here are a few clues for this homework assignment.

Code:
# assign 'Today is Mon' to TODAY variable
TODAY=$(date +%a)
You are assigning a string created by 'date +a%' that says 'Mon' in todays case, to the variable TODAY. On a friday it would assign 'Fri' to the TODAY variable.

If you want to check against that string variable to determine whether it is friday or not, you would want to check for the text 'Fri' in your IF test.

Try modifying the code to fit my description and we will move on to the date part.
 
Old 07-14-2014, 04:58 PM   #3
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you for the quick response szboardstretcher.
I hope I understood you correctly lets find out.

#!/bin/bash

TODAY=$(date +%a 'Fri')
echo "Today is $TODAY"
if [$TODAY = "Fri" ]
then
echo "Today is Friday"
if [$TODAY = "Friday the 13th" ]
echo "Today is Friday the 13th"
 
Old 07-14-2014, 05:06 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,816

Rep: Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346
Nice. This is correct.

Code:
if [$TODAY = "Fri" ]
But this part is not. You did not need to change the date command, as you were already getting the output assigned to TODAY that you wanted.

Code:
TODAY=$(date +%a 'Fri')
On to the next part. What you want to do is check that today is a 'friday' and the date is a '13. So you need to check against:

date +%a (Fri)
date +%d (13)

You will want to use an AND test as you want BOTH of those to be true. The psuedo-code would be something like.

DAY=(the +%a one)
DATE=(the +%d one)

if DAY = Fri, AND DATE = 13 then print 'today is friday the 13th'

Once you understand that, try to write it in proper bash code, and you should be all set. If you have specific questions, post them and people will give you a hand. But try to read through your assignment and google some examples, you will learn better that way.

Last edited by szboardstretcher; 07-14-2014 at 05:13 PM.
 
Old 07-14-2014, 05:11 PM   #5
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Got it so it should look like this then.

#!/bin/bash

TODAY=$(date +%a )
echo "Today is $TODAY"
if [$TODAY = 'Fri' ]
then
echo "Today is Friday"
if [$TODAY = "Friday the 13th" ]
echo "Today is Friday the 13th"

Last edited by Linux hatesme; 07-14-2014 at 05:13 PM.
 
Old 07-14-2014, 05:15 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,816

Rep: Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346
Sorry, that's the opposite of what I said.

Do you have a reference for this assignment? Notes? Do you understand what a date format is, how if-then-else works?

Have you found any Bash reference manuals online and read through the examples?

You are trying to do something fairly simple and common, you should be able to find a good example.

Last edited by szboardstretcher; 07-14-2014 at 05:18 PM.
 
Old 07-14-2014, 05:37 PM   #7
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Sorry I am a complete Linux newb. This is an online class no notes no references. Up to this script I have been able to find videos and tutorials that explain what needs to be done. I have found the different date commands just nothing that shows an example like this. My book is not for beginners it is for people that already have a working understanding of Linux. I have an extensive knowledge of windows but if anything that is creating problems for me. My professor takes a week to answer an e-mail and his hint was the date commands found here http://mdlug.org/pipermail/mdlug/2007-March/011711.html
I am very thankful for your help on this as I am sure basic stuff like this is annoying to experienced users.
 
1 members found this post helpful.
Old 07-15-2014, 10:49 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,816

Rep: Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346
Not at all. Thanks for being honest. I think it best to show you what I would do, and explain it line by line:

# This calls out the interpreter to use to run the script, and -x will output the run to screen. You can remove the '-x' when you have it working correctly. Great for debugging.

Code:
#!/bin/bash -x
# We will assign the output of 'date +%a' to the variable DAY
Code:
DAY=$(date +%a)
# We are checking to see if the text 'Fri' is in the variable DAY
Code:
if [ $DAY == "Fri" ]
# If it is 'Fri' then we echo "Today is Friday"
Code:
then
 echo "Today is Friday"
# If it is NOT 'Fri' then we echo "Today is NOT Friday"
Code:
else
 echo "Today is NOT Friday"
# You always end an 'if block' with 'fi' to let the program know that you are moving on to the next part
Code:
fi
# Assign output of date +%d to the variable DATE
Code:
DATE=$(date +%d)
# Here we are checking that the variable DATE has "13" **AND** DAY has "Fri". That is what && means, it means that you are testing whether BOTH ARE TRUE.
Code:
if [ $DATE == "13" ] && [ $DAY == "Fri" ]; then
# IF BOTH ARE TRUE, then print this
Code:
 echo "Today is Friday the 13th"
# If one or both are false, print this.
Code:
else
 echo "Today is NOT Friday the 13th"
# Close your if block
Code:
fi
All together it looks like this.

Code:
#!/bin/bash -x

DAY=$(date +%a)
if [ $DAY == "Fri" ]
then
 echo "Today is Friday"
else
 echo "Today is NOT Friday"
fi

DATE=$(date +%d)
if [ $DATE == "13" ] && [ $DAY == "Fri" ]; then
 echo "Today is Friday the 13th"
else
 echo "Today is NOT Friday the 13th"
fi

Last edited by szboardstretcher; 07-15-2014 at 10:51 AM.
 
2 members found this post helpful.
Old 07-15-2014, 11:09 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,945

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by Linux hatesme View Post
Sorry I am a complete Linux newb. This is an online class no notes no references. Up to this script I have been able to find videos and tutorials that explain what needs to be done. I have found the different date commands just nothing that shows an example like this. My book is not for beginners it is for people that already have a working understanding of Linux. I have an extensive knowledge of windows but if anything that is creating problems for me. My professor takes a week to answer an e-mail and his hint was the date commands found here http://mdlug.org/pipermail/mdlug/2007-March/011711.html
I am very thankful for your help on this as I am sure basic stuff like this is annoying to experienced users.
szboardstretcher is right on the money here, but I did want to interject. We are always happy to help someone new learn, but the biggest pluses for you are:
  • You were honest about it being homework
  • You asked a clear question
  • You showed effort of your own
Those things are almost guaranteed to get you help. You'd be surprised how many people come here and flat out ASK for a script to be written for them, or give a homework question with no effort of their own, so thank you.

If you're just starting out with bash scripting, I'd suggest these pages:

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/abs/html/

...which will take you through the very beginning basics, through the advanced concepts later, all with examples and pretty good explanations.
 
Old 07-15-2014, 02:20 PM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,122

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
Quote:
Originally Posted by Linux hatesme View Post
Sorry I am a complete Linux newb.
I don't like this sentence. Everyone (of us) was a beginner, and everyone of us has the right to think. So you do not need to say sorry at all, but you need to think about the answer you got (as it was mentioned by TB0ne).

From the other hand linux does not hate you (or anyone else), but nothing works without knowing its soul - or its internals. So ask/understand/practice/ask... and you will see how nice is it.

Code:
if [ $(date +%a%d) = Fri13 ]; then
 echo "Today is Friday the 13th"
else
 echo "Today is NOT Friday the 13th"
fi
 
Old 07-15-2014, 02:25 PM   #11
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Szboardstretcher,

Thank you so much for your time and expertise in explaining this code to me in detail line by line. The double ampersand was huge and your detailed instructions actually explained what was needed Thank you Thank you Thank you. The only thing I did not really understand was the use of a double equal sign. I am sure that I will find the significance of that in the links provided by TB0ne. Thank You so much for your help and understanding.

TB0ne,
Thank you for your strong words of encouragement and for providing me with some good informative links they will be saved in my folder for Linux and I will be reading them as soon as I am done saying thank you.

To both of you THANK YOU for taking the time to help me learn and understand. It took me 2 weeks of failure to finally throw my-self to the wolves and post on a forum. Both of you were very polite and non elitist to my post which was my biggest fear. I am glad I chose this site mainly for all the teach a man how to fish references as that is a viewpoint lost in today's instant gratification world we live in. Thank you both again.

Last edited by Linux hatesme; 07-15-2014 at 02:27 PM.
 
Old 07-15-2014, 04:07 PM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,945

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by Linux hatesme View Post
Szboardstretcher,

Thank you so much for your time and expertise in explaining this code to me in detail line by line. The double ampersand was huge and your detailed instructions actually explained what was needed Thank you Thank you Thank you. The only thing I did not really understand was the use of a double equal sign. I am sure that I will find the significance of that in the links provided by TB0ne. Thank You so much for your help and understanding.

TB0ne,
Thank you for your strong words of encouragement and for providing me with some good informative links they will be saved in my folder for Linux and I will be reading them as soon as I am done saying thank you.

To both of you THANK YOU for taking the time to help me learn and understand. It took me 2 weeks of failure to finally throw my-self to the wolves and post on a forum. Both of you were very polite and non elitist to my post which was my biggest fear. I am glad I chose this site mainly for all the teach a man how to fish references as that is a viewpoint lost in today's instant gratification world we live in. Thank you both again.
You are very welcome, although szboardstretcher provided the real help. Showing effort, thinking, and being honest will take you far in life...please continue.

Hope to see you back soon.
 
Old 07-30-2014, 11:39 PM   #13
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,816

Rep: Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346
Quote:
Originally Posted by Linux hatesme View Post
The only thing I did not really understand was the use of a double equal sign.
== is an evaluation/test operator,.. (Is this == that)

= is an assignment operator,.. (let X=10)
 
  


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
bash script to count the number of occurences of friday the 13th in a given year michaelgg13 Linux - Newbie 33 07-31-2014 01:39 PM
Friday the 13th Massive Upgrade willysr Slackware 232 10-18-2012 08:15 AM
LXer: One of those magic times: On Friday the 13th! LXer Syndicated Linux News 1 02-07-2009 10:43 AM


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