LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 10:46 PM   #16
UNIXnub22
LQ Newbie
 
Registered: Apr 2014
Location: New York
Posts: 8

Rep: Reputation: Disabled

So would this be correct?:

for ((count=1 ; count < 13 ; count++)); do
answer=`cal $count $@ | awk '{print $6}' | grep 13 | wc -l`
total=0
total+=answer
done
 
Old 04-28-2014, 10:49 PM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please use [code][/code] tags when displaying code / data to make it more readable.

I also see no reason to use multiple commands:
Code:
cal -y 2013 | awk '{if($6 == 13 || $13 == 13 || $20 == 13)x++}END{print x}'

cal -y 2013 | ruby -ane 'x||=0;x+=[$F[5],$F[12],$F[19]].count("13");END{p x}'
Edit: Actually the awk is flawed here, but will let others work out why

Last edited by grail; 04-28-2014 at 10:54 PM.
 
Old 04-28-2014, 10:51 PM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@UNIXnub22 - you might want to reconsider where you keep resetting the total variable and of course the fact that you never display it
 
1 members found this post helpful.
Old 04-28-2014, 10:58 PM   #19
UNIXnub22
LQ Newbie
 
Registered: Apr 2014
Location: New York
Posts: 8

Rep: Reputation: Disabled
I only posted a portion of my code. Here's the whole thing:

Code:
     1	#!/bin/bash
     2	# friday13th program
     3	# invoke as ./friday13th [year]
     4	#
     5	#
     6	# check if more than one argument
     7	#
     8	if [[ $# -gt 1 ]]
     9		then
    10			echo 'usage: ./friday13th [year]'
    11			exit 1
    12	fi
    13	#
    14	# check if there is one argument
    15	#
    16	if [[ $# -eq 1 ]]
    17		then
    18			for ((count=1 ; count < 13 ; count++)); do
    19				answer=`cal $count $@ | awk '{print $6}' | grep 13 | wc -l`
    20				total+=$answer
    21			done
    22	
    23		echo "Number of Friday the 13th in year $@ is: $total"
    24	fi
    25	#
    26	# check if there is no arguments
    27	#
    28	if [[ $# -eq 0 ]]
    29		then
    30			date=$(date +'%Y')
    31			echo "Number of Friday the 13th in year $date is: $total"
    32	fi
When I run 'friday13th 2014' I get this as the output now:
Number of Friday the 13th in year 2014 is: 000001000000

It is correct in the fact that there's only one Fri-the-13th in 2014 (I checked to make sure) but how do I just make it one sum? I can't get my total statement correct still :/

Thanks--

Last edited by UNIXnub22; 04-28-2014 at 11:26 PM. Reason: [code][/code]
 
Old 04-28-2014, 11:04 PM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I wonder, as you would obviously need to use something to generate the year, ie cal, ncal and so on, apart from that command (and assuming this is a course on learning bash) can you not make
an entire bash script to solve the problem?

I am happy to post solution once the 2 new people give it a crack
 
Old 04-28-2014, 11:10 PM   #21
UNIXnub22
LQ Newbie
 
Registered: Apr 2014
Location: New York
Posts: 8

Rep: Reputation: Disabled
Yes, this is a intro bash+UNIX course.
We are supposed to create an entire script, like the one I posted above ^^.

His hint was:
Use cal, awk, grep, wc in that order suitably with pipe to compute the number of Friday the 13ths in a specific month in a specific year as either 0 or 1.
For ex: 0 Friday the 13th in July 2013, and 1 Friday the 13th in September 2013.
Run a loop over all months to determine the number for the whole year (that was given as the one command line argument).
If no year is given in the CL, the system determines the year and uses it. Determined from the date command.

Should look something like this:
[51] [jdoe1@blah:~/blah/blah]$ ./friday13th 2007
Number of Friday the 13th in the year 2007 is: 2

Quote:
Originally Posted by grail View Post
I wonder, as you would obviously need to use something to generate the year, ie cal, ncal and so on, apart from that command (and assuming this is a course on learning bash) can you not make
an entire bash script to solve the problem?

I am happy to post solution once the 2 new people give it a crack
 
Old 04-28-2014, 11:42 PM   #22
UNIXnub22
LQ Newbie
 
Registered: Apr 2014
Location: New York
Posts: 8

Rep: Reputation: Disabled
Still stumped. Seems so simple but i'm not familiar with bash really..

Can someone message me or point me in the right direction?

--Thanks
 
Old 04-29-2014, 12:36 AM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Explain what you think is happening here:
Code:
total+=$answer
Also, the best advice anyone can give for diagnosing bash scripts is to have the following as the second line after the interpreter to be:
Code:
set -xv
As per my previous post though .. cal, awk, grep, wc .. except for cal you could do the entire script using just bash

Last edited by grail; 04-29-2014 at 12:37 AM.
 
Old 04-29-2014, 02:35 AM   #24
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Talking

Quote:
Originally Posted by michaelgg13 View Post
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]
Do any of the previous answers count as "simple"?
 
Old 04-29-2014, 06:11 AM   #25
UNIXnub22
LQ Newbie
 
Registered: Apr 2014
Location: New York
Posts: 8

Rep: Reputation: Disabled
Exclamation

It's taking the value of answer and adding it to the variable total, then when it reaches done it increases the count, and starts the loop over again?
Or atleast that's what i want it to do, haha. I'm not sure what the correct way to do this is...

The -xv will give me the debug lines, i'm aware of that. I know that total+= line is wrong.. i just don't know the right way to write it...

And yes, I know the program can be cut down into almost nothing but using just bash, but that's not what i'm looking for.

Any help is appreciated...

--Thanks

Quote:
Originally Posted by grail View Post
Explain what you think is happening here:
Code:
total+=$answer
Also, the best advice anyone can give for diagnosing bash scripts is to have the following as the second line after the interpreter to be:
Code:
set -xv
As per my previous post though .. cal, awk, grep, wc .. except for cal you could do the entire script using just bash
 
Old 04-29-2014, 06:56 AM   #26
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
Again I'll point you back to this guide

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

There's a certain way to evaluate arithmetic expressions in bash. I'd actually recommended checking out the entire bash scripting guide on that site. Bash can be pretty tricky if you come from another coding language and that site does a good job of telling you all of the little details that you need to know so that you're code isn't constantly generating unexplainable error messages.


http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

http://www.tldp.org/LDP/abs/html/
 
Old 04-29-2014, 06:59 AM   #27
wstewart90
Member
 
Registered: May 2013
Distribution: Arch Linux
Posts: 86

Rep: Reputation: Disabled
Quote:
Originally Posted by UNIXnub22 View Post
It's taking the value of answer and adding it to the variable total, then when it reaches done it increases the count, and starts the loop over again?
Or atleast that's what i want it to do, haha. I'm not sure what the correct way to do this is...

The -xv will give me the debug lines, i'm aware of that. I know that total+= line is wrong.. i just don't know the right way to write it...

And yes, I know the program can be cut down into almost nothing but using just bash, but that's not what i'm looking for.

Any help is appreciated...

--Thanks
Not sure what you mean. I was under the impression that you were doing this to learn bash after the thread continued on for so long. If you're just looking for a quick and easy way to do what you were looking for then the answers on the first two pages will do just that.
 
Old 04-29-2014, 08:33 AM   #28
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Here's my semi-working attempt (no validity checking on date) - I have trouble with numbers too in BASH:-
Code:
#! /bin/bash

tot=0
for month in {1..12}
do
  day=$(date -d "$month/13/$1" +%a)
  if [ "$day" == 'Fri' ]; then
     $((tot++))
  fi
done

echo "Number of Fridays in $1 = $tot"
 
Old 04-29-2014, 09:17 AM   #29
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by JeremyBoden View Post
Here's my semi-working attempt (no validity checking on date) - I have trouble with numbers too in BASH:-
Code:
#! /bin/bash

tot=0
for month in {1..12}
do
  day=$(date -d "$month/13/$1" +%a)
  if [ "$day" == 'Fri' ]; then
     $((tot++))
  fi
done

echo "Number of Fridays in $1 = $tot"
Not bad. At least it gives the right answer. I see error messages when it tries to execute $((tot++)), but that doesn't prevent it from performing the incrementation. Take a look at this reference, which I found by googling bash arithmetic:
http://www.tldp.org/LDP/abs/html/ops.html
and specifically look at Example 8-2, a script which shows 10 different ways to increment in bash. You don't need 10 different ways. Just pick one that you can easily remember.
 
1 members found this post helpful.
Old 04-29-2014, 09:35 AM   #30
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Probably a good idea for me to preserve my working version, for future reference.
I chose prefixing $((tot++)) with a colon to give
Code:
: $((tot++))
BTW The space after the : appears to be required.
 
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:44 AM.

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
Open Source Consulting | Domain Registration