2443 Views 9 Replies Latest reply: Aug 17, 2008 4:54 AM by 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
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.
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:
while read file
maybe a rename operation,
or copy, or differences, etc...
...a list of files
...that I want to
...may have been generated
...by 'ls' or 'find' or 'grep -l'
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.
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.
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.
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.