LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-18-2011, 01:05 PM   #1
gorrillamcd
LQ Newbie
 
Registered: Apr 2011
Distribution: Ubuntu, Fedora
Posts: 17

Rep: Reputation: 3
Question Bash Script Using "date" command error


I'm still learning about writing scripts for my server, so this will probably be a fairly simple solution to my question.

I have a server running Ubuntu Server 10.04 and squid. I'm using calamaris to parse the squid logs and wrote a script to include in cron to run every day.

Code:
#!/bin/bash

DATE=`date +"%a"`
CALAMARIS="/usr/bin/calamaris"
CALAMARISOPTIONS="-a --config-file /etc/calamaris/calamaris.conf -F html-frame --output-path /var/www/calamaris --output-file ${DATE}.html"
cat /var/log/squid3/access.log | $CALAMARIS $CALAMARISOPTIONS
I use the "date +%a" command to generate the name of the html file that calamaris generates and I want it to the files like mon.html, tue.html, wed.html, and so on.

However, it is just rewriting each new daily report over thursday's file (which I believe was the day I first started using the script). Is there something wrong with my script that would cause it to do this?
 
Old 10-18-2011, 01:11 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
That should just work. Just note that the generated file will
have an uppercase first letter ... so you should see
Mon.html
Tue.html


Cheers,
Tink
 
1 members found this post helpful.
Old 10-18-2011, 03:38 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You shouldn't use a single variable to hold a list of options. Whenever you have a list of items, use an array instead.

(But actually, variables (including arrays) are intended for data, not code. While it's usually ok to store options in a variable, functions should be used for setting up complex commands. http://mywiki.wooledge.org/BashFAQ/050 )

Also, $(..) is highly recommended over `..`

useless use of cat. You can generally just use file redirection.

Oh, And I think it's also generally recommended to make your variables lower or mixed case, to avoid conflicts with preset shell variables, which are almost universally uppercase. I personally think too many upper case words is hard to look at.

Code:
#!/bin/bash

infile=/var/log/squid3/access.log

run_calamaris() {

	local day=$( date +"%a" )
	local -a calamarisoptions

	calamarisoptions+=( '-a' '--config-file' '/etc/calamaris/calamaris.conf'  )
	calamarisoptions+=( '-F' 'html-frame' '--output-path' '/var/www/calamaris' )
	calamarisoptions+=( '--output-file' "$day.html" )

	/usr/bin/calamaris "${calamarisoptions[@]}"

}

run_calamaris <"$infile"
I believe it should work as written, as long as the command in the function takes input from stdin (and there's only one that does so).
 
1 members found this post helpful.
Old 10-18-2011, 07:08 PM   #4
gorrillamcd
LQ Newbie
 
Registered: Apr 2011
Distribution: Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: 3
Got it

Thanks for the answers guys. You're both right. The script is working as it should, but not how I wanted

When Calamaris outputs an html report, it creates a somename.html file (the "--output-file ${DATE}.html" in my script), but this file just references the other html files that actually store the report info. So to achieve what I want, I have to set --output-file to index.html and have each day's report in a directory (the --output-path option) named path/www/"Day of week".

Thank you for the corrections on syntax as well.
 
  


Reply



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
bash script: how to check stderr after command "find" Vilmerok Programming 1 07-08-2009 07:35 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 09:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:38 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
Open Source Consulting | Domain Registration