gravityboy ([info]gravityboy) wrote,
@ 2008-03-01 09:41:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Shell Scripting
I've never bothered to learn the gory details of shell scripting. This is somewhat embarrassing for me, although at the same time I don't feel like I've really suffered for the decision. I know enough of the basics to do some cool things (for loops, if tests and whatnot) but the the syntax is fraught with such problems that it feels like a waste of time to do more. Obviously, others have felt similarly, which is why perl was invented.

I've become more interested in it again recently though, mainly because I've been reading a lot of Kernighan. I don't want to deal with Bourne syntax though, which eliminates the only two shells I've ever spent serious time with in my life, bash and zsh, as well as ksh. I know csh scripting is considered harmful 'n shit, so that's right out.

Since I only want to shell script for local use, I don't need to worry about portability so I can try using more exotic shells. I'm also willing to script in a different language from the shell that I use, so the doors are really open. There's scsh if I want to write scheme, zoidberg if I want to write perl, rush if I want to write ruby.

What looked most appealing is rc though, which has a very nice Bourne-like syntax that's not nearly so warty. It was reimplemented before plan9 was released, and the reimplementation is in Debian, although I ran in to some undocumented differences with the plan9 version very fast. It may be worth trying out the plan9 version at this point to see how that goes.

Any other suggestions from the lazyweb are greatly appreciated. It'd be nice to have this aspect of my toolbox be more solid.



(Post a new comment)

IPython
(Anonymous)
2008-03-01 03:21 pm UTC (link)

IPython with the ipipe extension is also worth a look

Kai Schroeder

(Reply to this) (Thread)

Re: IPython
[info]gravityboy
2008-03-01 04:44 pm UTC (link)
Cool, I'll have a look. Thank you!

(Reply to this) (Parent)


[info]nescafe
2008-03-03 05:01 am UTC (link)
Learining bash is really worth your while -- true, the language is crufty, but it excels at tying together all the little tools that make up Unix.

(Reply to this) (Thread)


[info]gravityboy
2008-03-03 06:55 pm UTC (link)
Sure, but why use bash when I can use a less crufty language to do the same thing? rc's syntax cleans up a great deal of the problems in the Bourne syntax.

(Reply to this) (Parent)(Thread)


[info]nescafe
2008-03-03 08:17 pm UTC (link)
So does bash. :) Bash also has the great advantage of being installed in virtually every Linux (and most Unix) systems out of the box, whereas rc never really got beyond Plan 9. For personal scripts, this does not matter, but it will if you ever want to make your scripts more widely accessible or are writing something that someone else will eventaully have to maintain.

Then again, I am a bit biased.

(Reply to this) (Parent)(Thread)


[info]gravityboy
2008-03-03 09:51 pm UTC (link)
Meh, I don't think bash cleans up all that much :-)

The thing is, I don't plan on making anything remotely complicated in shell. If I want to distribute it as a real application I'd stick to something like python or ruby.

I pretty much just want this for the quick one-off scripts that don't leave my box, or are very simple utility scripts that don't grow beyond 15 lines. At that point, dealing with the overhead of python isn't worth it (perl or ruby is probably better here though) but I'd still like as nice a syntax as possible that's also geared towards constructing pipelines of classic UNIX tools.

(Reply to this) (Parent)

On Bash
[info]druiid
2008-03-03 07:13 pm UTC (link)
I have to say.. that if you're not going to work with bash scripting, you might as well just stick with a non-shell derived scripting language like python or perl... the good thing about bash scripts is that they're portable to 99.999% of the default system installs out there, while being fairly powerful when coupled with the built-in regex abilities. Due to the ability to chain directly to system tools like grep, cat, cut, etc. sometimes I can write in a few lines in bash what would cost twice that in some situations with python... That said, for extended or complex scripts python will win every time.

(Reply to this) (Thread)

Re: On Bash
[info]gravityboy
2008-03-03 09:54 pm UTC (link)
Yeah, this has been my current working model. If it's simple enough to do in a pipeline or three, do it in shell (not even bash, straight up sh). If it gets any bigger, go directly to perl/python/ruby, with python being my current favorite. Python is slightly clunky for constructing pipelines, which is probably my biggest annoyance with it in this role though, but it gets the job done fine.

(Reply to this) (Parent)(Thread)

Re: On Bash
[info]druiid
2008-03-04 02:01 am UTC (link)
That's not always going to be the best method, though. I think part of the best way to look at shell scripting is that if you do any file and text manipulation that requires more than one step, bash is usually going to be a good candidate for this. Because of the built-in expr support (read: $(expr ) ), it is going to allow for some fairly decent regex matching and manipulation. After a certain level of complexity, certainly, python or perl are going to serve you better, but.. for instance, here's a simple regex to grab the running kernel release:

kernelrelease=$(uname -r)
kernelver=$(expr "$kernelrelease" : "\(^[0-9]\+\.[0-9]\+\.[0-9]\+\)")

There are of course easier and better regex expressions, but it's just an example. For instance.. use bash to check the running kernel release and see if it matches the installed package on a system.. or if there's a newer release.. things like that. Try doing that in python and I bet as soon as you've gotten to the os.system() line you're probably twice as many lines in python... and that's where bash becomes useful.. in the ability to work directly with the 'shell' and commands.

(Reply to this) (Parent)(Thread)

Re: On Bash
[info]gravityboy
2008-03-04 05:32 pm UTC (link)
I agree, which is actually why I wrote this post to begin with. Python is clunky for interacting with the shell. Perl and Ruby actually do a lot better because you can just put things in backticks. I've been trying to move away from both these languages, although I've been very happy with using them as a better shell.

(Reply to this) (Parent)


[info]ameilajehox
2008-07-16 08:49 am UTC (link)
You may not even have been aware that there is more than one shell available. Yet it's easy for you to determine which shell you are using.

(Reply to this) (Parent)


[info]katyajorec
2008-07-11 06:05 am UTC (link)
Don't get me wrong: C++ surely gets the job done, but I think it could be better and cleaner - mostly by removing some C compability and rethinking some other issues.

(Reply to this) (Parent)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…