LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script to count the number of occurences of friday the 13th in a given year (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-count-the-number-of-occurences-of-friday-the-13th-in-a-given-year-4175503292/)

michaelgg13 04-28-2014 08:29 PM

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!

JeremyBoden 04-28-2014 08:57 PM

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.

AlucardZero 04-28-2014 08:59 PM

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

I can also see cal and awk working

wstewart90 04-28-2014 09:14 PM

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.

michaelgg13 04-28-2014 09:27 PM

Quote:

Originally Posted by wstewart90 (Post 5160933)
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?

AlucardZero 04-28-2014 09:35 PM

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.

wstewart90 04-28-2014 09:44 PM

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.

michaelgg13 04-28-2014 09:44 PM

Quote:

Originally Posted by AlucardZero (Post 5160944)
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".

wstewart90 04-28-2014 09:47 PM

Quote:

Originally Posted by AlucardZero (Post 5160944)
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.

wstewart90 04-28-2014 09:50 PM

Quote:

Originally Posted by michaelgg13 (Post 5160950)
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?

wstewart90 04-28-2014 09:52 PM

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

michaelgg13 04-28-2014 09:52 PM

Nevermind, I feel stupid it has to be year=`date +"%Y"`

completely forgot the ``

UNIXnub22 04-28-2014 10:24 PM

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--

UNIXnub22 04-28-2014 10:29 PM

michaelgg13: We must be in the same class :)

wstewart90 04-28-2014 10:42 PM

Quote:

Originally Posted by UNIXnub22 (Post 5160975)
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


All times are GMT -5. The time now is 10:12 PM.