Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Good Book to learn Shell Scripting?

I'm looking at O'Reilly's "Classic Shell Scripting". Good choice? No scripting experience here. Beginner. Need to learn.

Suggestions welcomed.

Thanks.

iMac G5 (iSight) 20in 2.1Ghz 1.5 GB RAM 250 GB, Mac OS X (10.5.4), Soundsticks

Posted on Aug 6, 2008 6:51 PM

Reply
Question marked as Best reply

Posted on Aug 6, 2008 7:50 PM

Wendel Cooper's Advanced Bash scripting guide, contrary to the title, is a great place to start:
http://www.tldp.org/LDP/abs/html/

A bunch more links:
http://www.codebox.8m.com/shellscripting.htm

The zsh guide is specific to the OS X of shells:
http://zsh.sunsite.dk/Guide/
9 replies

Aug 6, 2008 8:13 PM in response to McToast

I learned shell scripting back in '85 from examples, the very limited man pages at the time, and "The Unix Programming Environment" Brian W. Kernighan and Rob Pike, 1985. And this book is rather old so it would not be the best source 🙂

However, I would suggest using a POSIX shell for your scripting. If that is not available to you, use bash as your first alternative (which tries to be POSIX complient, and on Mac OS X /bin/sh is really Bash), or ksh, or zsh. I would suggest against csh or tcsh for scripting (I have nothing against them for interactive shells, I just do not think they are good scripting languages).

It might be a good idea to know why you need to learn shell scripting. There are other scripting languages that can substitute for shell scripts. A lot depends on what you need to do. For example Perl is the super deluxe Swis Army Knife of scripting languages. But there is also awk, Python, Ruby, TCL (although I wouldn't recommend TCL). Then again, I almost always reach first for /bin/sh with a touch of awk before I consider one of the other scripting languages 🙂

Knowing what you want to do, would help.

And finally, spend some time in a bookstore browsing the UNIX books available. There are several several available that talk about shell scripting. Find one that speaks to you.

Also find example shell scripts and analyze them. You might try the following command to find examples of Bourne shell scripts on your system:

find / -type f -print0 | xargs -0 file | grep -i bourne

Aug 6, 2008 9:26 PM in response to BobHarris

I work in Desktop Support, specializing in Macs, natch, but I want to get out of Desktop and move up to Systems Administration. I was just passed over for an interview for a position because I didn't meet two requirements: experience with Linux administration and scripting experience (they were willing to train on the rest of what they needed). Lack of scripting skills is currently the *big black hole* on my resume. I need to pick up some scripting skills, and I feel basic shell scripting would be the best place for me to start, and the most useful, as it can be used across OS X, Linux, or Unix.

P.S. Anyone have any comments on the O'Reilly book?

P.P.S. I do use Bash as my default shell in the terminal.

Aug 7, 2008 6:42 AM in response to McToast

Reading is good, but you have to use it as well. So start writing simple scripts and expand upon them. My most frequent starter script tends to be something like:

#!/bin/sh
while read file
process $file
maybe a rename operation,
or copy, or differences, etc...
do
done <<EOD
...a list of files
...that I want to
...process, which
...may have been generated
...by 'ls' or 'find' or 'grep -l'
EOD

I have started scripts that way, which have then grown into some rather large and useful scripts.

For sysadmin type work, you want to get comfortable with ssh and scp, especially the ssh-keygen. Also utilities like 'find' and 'xargs', 'tar', 'rsync' are useful to know about. Learn to use 'rsync -e ssh' for doing networked incremental backups.

For sysadmin work at your company, see if you can find and analyze scripts that your existing sysadmins are using to see what kinds of tasks they are using scripts for. That will help focus your study and practice scripts on areas they need.

Learn about

${variable#pattern}
${variable##pattern}
${variable%pattern}
${variable%%pattern}

I find that these are extremely useful and end up using them constantly. But their power is not always obvious. Learn them, know them inside and out. You will find them useful.

There is so much I could go on about, but I would start to ramble. Just analyze existing scripts, and write your own, especially ones that access other systems via ssh, scp, rsync -e ssh.

Good luck

Aug 13, 2008 5:22 PM in response to BobHarris

Python has become the system/administration de facto programming language of Ubuntu (most widespread Linux desktop distro) and IBM is pushing a lot of systems/command line administration towards Python (see IBM Developer Centre for tutorials, example code, helpful tips).

If you want to stay in bash scripting get good at making full use of all the unix shell utilities and chaining them together to accomplish your tasks.

If you find yourself a bit cramped for features or want to move into a massive standard library of modules to move into distributed/network applications and monitoring then Python will be a good choice to learn. Sockets programming in Python is brain dead easy.

IMHO learn both and use both where they shine for the specific problem domain.

Aug 15, 2008 6:37 PM in response to McToast

I'll second the bash scripting guide website. For O'Reilly books, I'd pickup the sed & awk book, and the perl "camel" book. Python is nice, but perl is still "the" tool of choice. With a good understanding of shell scripting (bash or zsh) and these tools, you'll be setup very well. I'd also start learning regular expressions. A good command of regular expressions is essential to making any tool powerful.

Good Book to learn Shell Scripting?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.