[SOLVED] shell scripting vs. perl, etc. for simple text script
Linux - SoftwareThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.