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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-26-2012, 12:21 PM
|
#1
|
|
LQ Newbie
Registered: Aug 2008
Location: San Diego, CA
Distribution: Linux Mint
Posts: 15
Rep:
|
shell scripting vs. perl, etc. for simple text script
Hi there,
Description of problem:
I keep a little log of what I'm working on while programming, it's just the current time and a description, like this:
Quote:
|
14:02 - debugging game.Events class
|
Well, I wanted to do
Code:
date +%R >> log.dat
and something like
Code:
echo ' - debugging game.Events class' >> log.dat
but 1) date always entered a carriage return at the end,
and 2) it's just as much trouble to type:
as
so I thought it would be nice to make my own command, call it 'log', so that I could type:
Code:
log 'debugging game.Events class'
and it would append
Quote:
|
14:02 - debugging game.Events class
|
to ~/log.dat
without me having to cd over to it or anything.
Question:
So, how would one do this? It seems simple enough, but I'm not sure where to begin. Would I use a shell script or a scripting language like Perl? I do a bit of programming with Java, C++, Python (just started). But haven't really done scripting before.
Thanks for any advice,
Jonathan
|
|
|
|
06-26-2012, 12:46 PM
|
#2
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
It could be done a number of ways. You could write a binary or probably even a python script to do it.
A quick bash script that would do it:
Code:
#!/bin/bash
entry=$@
echo $(date +%R) $entry
Just name it log, make it executable and insure it is in your path then you could run log command followed by your entry:
log this is a test
Which should output something like:
13:43 this is a test
You could either redirect command line to a log:
log this is a test >>mylog
Or add the redirection to the script:
Code:
#!/bin/bash
entry=$@
echo $(date +%R) $entry >>mylog
Last edited by MensaWater; 06-26-2012 at 12:48 PM.
|
|
|
1 members found this post helpful.
|
06-26-2012, 01:23 PM
|
#3
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,806
|
I would try to write a function instead of a script.
Code:
function logthis ()
{
echo $(date +%R) - $@ >> mylog
}
|
|
|
1 members found this post helpful.
|
06-26-2012, 03:04 PM
|
#4
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
Quote:
Originally Posted by pan64
I would try to write a function instead of a script.
|
And put it where? .bashrc? Alias? What happens if he switches users?
My read of OP was that this is not something he wants other things doing - it is something he does on the fly between tasks.
Last edited by MensaWater; 06-26-2012 at 03:06 PM.
|
|
|
|
06-26-2012, 03:51 PM
|
#5
|
|
LQ Newbie
Registered: Aug 2008
Location: San Diego, CA
Distribution: Linux Mint
Posts: 15
Original Poster
Rep:
|
Firstly, thank you MensaWater and pan64 both for your timely replies 
MensaWater, I did what you said and it worked great, so thank you! The only issue I had was that I had to type ./log instead of just log, which is what you did in your example, I made it executable with , is that the problem?
Also, is there a place I could drop my script so that I could access it from any path, like a "real" program (cat, ls, nano, etc).
I only use one user, so that's not a problem.
pan64, thank you for your suggestion, I hope to eventually expand the script to be able to generate new log files (i.e. if it were the first entry of the day it would make a new log with its name based on the date, etc), so I'll most likely use a function then.
Cheers,
Jonathan
Oh, and yes MensaWater, it would just be something that I would do, so it doesn't have to be accessible by another program.
|
|
|
|
06-27-2012, 01:21 PM
|
#6
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
I had written:
Quote:
|
Just name it log, make it executable and insure it is in your path
|
So yes - the chmod +x is what makes it executable. To have it in your path you have insure the location of the executable is in the PATH variable for whichever users you intend to run the command as.
If you type "echo $PATH" for a given user you'll see a semi-colon delimited list of directories. Those are the directories that are searched for a command when you don't specify a path on execution. Good candidates for stuff like this that are usually already in user PATH variables would be /usr/local/bin or /usr/share/bin. (Typically /bin, /usr/bin are also in PATH but those are typically system installed files - you can put things there if you want but might run the risk of them being overwritten when you install a package but only if the package adds a command of the same name.)
If you want all users to have access to the command its directory should be in the PATH statement defined by global profiles (e.g. /etc/profile for bash/ksh). If you want to restrict it to a small set of users (e.g. yourself and root) you can add it to the profile of those users in there home directories(e.g. $HOME/.bashrc, $HOME/.profile, $HOME/.bash_profile) $HOME is a system defined variable set by reading the home directory from /etc/passwd for the given user. Be sure that whatever directory you put the script in is accessible by all the users you intend to have use the command. (That is to say if you make the script executable but put it in a location only readable by one user even if PATH has that directory in it no other user would be able to execute it because they couldn't access the directory itself.)
|
|
|
1 members found this post helpful.
|
06-27-2012, 11:26 PM
|
#7
|
|
LQ Newbie
Registered: Aug 2008
Location: San Diego, CA
Distribution: Linux Mint
Posts: 15
Original Poster
Rep:
|
Thank you so much MensaWater for the thorough explanation! I learned a lot from your post, and it worked of course
Post marked as solved.
cheers,
Jonathan
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:41 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|