LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-26-2012, 12:21 PM   #1
Jonathanius
LQ Newbie
 
Registered: Aug 2008
Location: San Diego, CA
Distribution: Linux Mint
Posts: 15

Rep: Reputation: 0
Smile 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:
Code:
date +%R
as
Code:
14:02
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
 
Old 06-26-2012, 12:46 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
Old 06-26-2012, 01:23 PM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I would try to write a function instead of a script.
Code:
function logthis ()
{
    echo $(date +%R) - $@ >> mylog
}
 
1 members found this post helpful.
Old 06-26-2012, 03:04 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by pan64 View Post
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.
 
Old 06-26-2012, 03:51 PM   #5
Jonathanius
LQ Newbie
 
Registered: Aug 2008
Location: San Diego, CA
Distribution: Linux Mint
Posts: 15

Original Poster
Rep: Reputation: 0
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
Code:
chmod +x log
, 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.
 
Old 06-27-2012, 01:21 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
Old 06-27-2012, 11:26 PM   #7
Jonathanius
LQ Newbie
 
Registered: Aug 2008
Location: San Diego, CA
Distribution: Linux Mint
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
  


Reply

Tags
command line, scripting, shell script



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
help with simple shell scripting choconlangthang Linux - Newbie 3 05-02-2011 02:42 AM
Shell scripting question (simple) Leon W. Malinofsky *BSD 8 07-31-2008 09:48 AM
simple question regarding shell scripting branche_dude Linux - Newbie 8 07-21-2006 02:35 AM
simple shell scripting problem noir911 Programming 11 03-14-2006 01:27 AM
Simple Shell Scripting Question hellomynameisphil Programming 2 08-27-2005 03:41 AM

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

All times are GMT -5. The time now is 03:55 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