Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"
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.
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"
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 04:13 PM.
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 04:13 PM.
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.
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 09:51 AM.
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:
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
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 01:27 PM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.