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 04-28-2014, 08:29 PM   #1
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Rep: Reputation: Disabled
bash script to count the number of occurences of friday the 13th in a given year


Hi all,

I think the title is pretty self explanatory. I want a simple bash script that will return the number of occurrences of friday the 13th in a given year when you type

fridaythe13th [year goes here]

and I really am not good with writing bash scripts

any help would be appreciated

Thanks!
 
Old 04-28-2014, 08:57 PM   #2
JeremyBoden
Senior Member
 
Registered: Nov 2011
Distribution: Debian
Posts: 1,144

Rep: Reputation: 235Reputation: 235Reputation: 235
1. Construct a date string for the 13th day of each month of the given year.
2. Convert to give a day name
3. If it is Friday, increment a counter.

Refer to any good BASH tutorial.

Date conversions - see man page of date.
 
Old 04-28-2014, 08:59 PM   #3
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,823

Rep: Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611
I'd do this with ncal and grep
this answer may help with the grep(s)

I can also see cal and awk working
 
1 members found this post helpful.
Old 04-28-2014, 09:14 PM   #4
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
cal -y|awk '{print $6$13$20}'|grep -o 13|wc -l

or


#!/bin/bash
cal -y $1|awk '{print $6$13$20}'|grep -o 13|wc -l


Pass the year as the first and only argument. If you want something fancy like a help command or something then you're going to have to learn bash. I did this because I'm bored and finishing up my last two days at a NOC before moving on to an admin job.
 
Old 04-28-2014, 09:27 PM   #5
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wstewart90 View Post
cal -y|awk '{print $6$13$20}'|grep -o 13|wc -l

or


#!/bin/bash
cal -y $1|awk '{print $6$13$20}'|grep -o 13|wc -l


Pass the year as the first and only argument. If you want something fancy like a help command or something then you're going to have to learn bash. I did this because I'm bored and finishing up my last two days at a NOC before moving on to an admin job.


Ok, so break this down for me. cal -y $1 will be the year passed in by the user, which will then be passed to awk(which I have no idea how to use) whatever comes out of awk will grepped to only 13, and it'll count however many come out of that. Wouldn't you need a for loop to iterate over every month, count the number for each month?
 
Old 04-28-2014, 09:35 PM   #6
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,823

Rep: Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611
Way to just feed him the answer wstewart90.

> Wouldn't you need a for loop to iterate over every month, count the number for each month?

Look at the output of cal -y, or read cal's man page

Any since he's not going to figure it out himself now here's mine:
Code:
ncal -S -h 2012 | grep ^Fr | grep -o 13 | wc -l
Replace 2012 with the year or a variable or a script argument as you will.
 
Old 04-28-2014, 09:44 PM   #7
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
Sorry. I'm so bored right now I'm just looking for a problem to solve. Guess I should be following that whole teach a man how to fish thing.
 
Old 04-28-2014, 09:44 PM   #8
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AlucardZero View Post
Way to just feed him the answer wstewart90.

> Wouldn't you need a for loop to iterate over every month, count the number for each month?

Look at the output of cal -y, or read cal's man page

Any since he's not going to figure it out himself now here's mine:
Code:
ncal -S -h 2012 | grep ^Fr | grep -o 13 | wc -l
Replace 2012 with the year or a variable or a script argument as you will.

Thanks for the reply, I chose to use the other guys(sorry ;P)

But I have another question, firstly here is my script:

#!/bin/bash
# useage: ./friday13th [year]
if [[ $# -eq 0 ]]
then
echo useage: ./friday13th [year]
year=cal -y
else
year=$1
fi
cal -y $year|awk '{print $6$13$20}'|grep -o 13|wc -l

exit 0

When the user does not specify a year, how would I properly gather the current year. year =cal -y does not work. I have also tried date +"%Y".
 
Old 04-28-2014, 09:47 PM   #9
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
Quote:
Originally Posted by AlucardZero View Post
Way to just feed him the answer wstewart90.

> Wouldn't you need a for loop to iterate over every month, count the number for each month?

Look at the output of cal -y, or read cal's man page

Any since he's not going to figure it out himself now here's mine:
Code:
ncal -S -h 2012 | grep ^Fr | grep -o 13 | wc -l
Replace 2012 with the year or a variable or a script argument as you will.
Tried to do a yum search for ncal on centos with nothing but the base repo and the epel repo and the only results that turned up were for mencal. You don't want to know what that's for.
 
1 members found this post helpful.
Old 04-28-2014, 09:50 PM   #10
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
Quote:
Originally Posted by michaelgg13 View Post
Thanks for the reply, I chose to use the other guys(sorry ;P)

But I have another question, firstly here is my script:

#!/bin/bash
# useage: ./friday13th [year]
if [[ $# -eq 0 ]]
then
echo useage: ./friday13th [year]
year=cal -y
else
year=$1
fi
cal -y $year|awk '{print $6$13$20}'|grep -o 13|wc -l

exit 0

When the user does not specify a year, how would I properly gather the current year. year =cal -y does not work. I have also tried date +"%Y".
you're date command should work for getting the current year. What output are you getting?
 
Old 04-28-2014, 09:52 PM   #11
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
that year variable that you created with the cal -y command isn't being evaluated as a command. bash is tricky like that. It needs a space after the '=' sign.



http://www.tldp.org/LDP/abs/html/commandsub.html
 
Old 04-28-2014, 09:52 PM   #12
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Nevermind, I feel stupid it has to be year=`date +"%Y"`

completely forgot the ``
 
Old 04-28-2014, 10:24 PM   #13
UNIXnub22
LQ Newbie
 
Registered: Apr 2014
Location: New York
Posts: 8

Rep: Reputation: Disabled
Could just do a:

date=$(date +'%Y')
echo "Number of Friday the 13th in year $date is: $total"


I'm curious though as well as how to create a for loop to iterate over every month (1-12), and then count the cal|awk|grep|wc statement number for each month?

Something like this? Or am I wrong?

for ((count=1 ; count < 13 ; count++)); do
answer=$(( cal $count $@ | awk '{print $6}' | grep 13 | wc -l ))
total=0
total+=answer
done

Keep getting an error that says:
./friday13th: line 19: cal 1 2014 | awk '{print }' | grep 13 | wc -l : syntax error in expression (error token is "1 2014 | awk '{print }' | grep 13 | wc -l ")





Thanks--
 
Old 04-28-2014, 10:29 PM   #14
UNIXnub22
LQ Newbie
 
Registered: Apr 2014
Location: New York
Posts: 8

Rep: Reputation: Disabled
michaelgg13: We must be in the same class
 
Old 04-28-2014, 10:42 PM   #15
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
Quote:
Originally Posted by UNIXnub22 View Post
Could just do a:

date=$(date +'%Y')
echo "Number of Friday the 13th in year $date is: $total"


I'm curious though as well as how to create a for loop to iterate over every month (1-12), and then count the cal|awk|grep|wc statement number for each month?

Something like this? Or am I wrong?

for ((count=1 ; count < 13 ; count++)); do
answer=$(( cal $count $@ | awk '{print $6}' | grep 13 | wc -l ))
total=0
total+=answer
done

Keep getting an error that says:
./friday13th: line 19: cal 1 2014 | awk '{print }' | grep 13 | wc -l : syntax error in expression (error token is "1 2014 | awk '{print }' | grep 13 | wc -l ")





Thanks--
It looks like you want to do

http://www.tldp.org/LDP/abs/html/commandsub.html

But you're doing

http://www.tldp.org/LDP/abs/html/arithexp.html
 
1 members found this post helpful.
  


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
Friday the 13th Massive Upgrade willysr Slackware 232 10-18-2012 07:15 AM
bash script to count number of lines with a specific property7 hhamid Programming 10 08-13-2010 01:35 AM
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 10:11 AM
C++ STL count() number of substring occurences... nyk Programming 4 06-25-2004 07:12 AM


All times are GMT -5. The time now is 05:02 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