LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   shell scripting vs. perl, etc. for simple text script (http://www.linuxquestions.org/questions/linux-software-2/shell-scripting-vs-perl-etc-for-simple-text-script-4175413491/)

Jonathanius 06-26-2012 12:21 PM

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

MensaWater 06-26-2012 12:46 PM

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


pan64 06-26-2012 01:23 PM

I would try to write a function instead of a script.
Code:

function logthis ()
{
    echo $(date +%R) - $@ >> mylog
}


MensaWater 06-26-2012 03:04 PM

Quote:

Originally Posted by pan64 (Post 4712655)
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.

Jonathanius 06-26-2012 03:51 PM

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.

MensaWater 06-27-2012 01:21 PM

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.)

Jonathanius 06-27-2012 11:26 PM

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


All times are GMT -5. The time now is 07:18 PM.