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! |
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. |
I'd do this with ncal and grep
this answer may help with the grep(s) I can also see cal and awk working |
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. |
Quote:
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? |
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 |
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.
|
Quote:
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". |
Quote:
|
Quote:
|
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 |
Nevermind, I feel stupid it has to be year=`date +"%Y"`
completely forgot the `` |
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-- |
michaelgg13: We must be in the same class :)
|
Quote:
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. |