[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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
shell scripting vs. perl, etc. for simple text script
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:
14:02 - debugging game.Events class
Well, I wanted to do
date +%R >> log.dat
and something like
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:
so I thought it would be nice to make my own command, call it 'log', so that I could type:
log 'debugging game.Events class'
and it would append
14:02 - debugging game.Events class
without me having to cd over to it or anything.
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
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.
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.)