## Shell Scripting for Newbies ## John Shafer About a month ago I was on IRC with Jim and some of his contributors, and had this crazy idea that I'd volunteer to write an article. I'm not sure where I came up with this idea, because I'd been using FreeBSD for around a month, and am still quite a newbie. Somehow it was agreed upon that I would write an article on a subject I didn't know about yet, but wanted to learn. I settled on shell scripting, which I will now attempt to explain. Now, close to a month later, panic is setting in. I've got one paragraph of that article done, and still don't have a clue how to write shell scripts. So I guess I'll have to write about what I do know, the raw basics, and fill in some of the more advanced concepts next month. A shell script is a text file that contains a series of commands to be executed. You can create quite complicated shell scripts, but for now I'm just going to discuss the steps involved in making a very simple one. The first step is to create the text file. You can use whatever editor you are comfortable with. I prefer vim myself: % vim ~/bin/startrc5 I'm not quite sure why, but the sources I read said that shell scripts should begin with the line: #!/bin/sh (Actually, that only applies to Bourne shell scripts. Unless you have a good reason, you should probably stick to using the Bourne shell.) Then the text of your script: rm /usr/local/d.net/exitrc5.now /usr/local/d.net/rc5des And that's it. Save the file, and mark it as executable: % chmod a+x ~/bin/startrc5 (In case you were wondering, ~/bin stands for the bin directory in your home directory. In my case, ~/bin would translate to /usr/home/jshafer/bin. So there should be a directory called bin in your home directory, and it should be a part of your path.) Next, if you are using csh or tcsh you either need to log off and log on again, or type the command % rehash And that's all there is to it. You now have a text file in your ~/bin directory that contains: % cat ~/bin/startrc5 #!/bin/sh rm /usr/local/d.net/exitrc5.now /usr/local/d.net/rc5des There is much more to learn, and my little shell script here may not be that good, but it demonstrates the basics. Shell Scripting References and Links "How to write a shell script" http://www.ocean.odu.edu/ug/shell_help.html A short course from Rice University: http://riceinfo.rice.edu/Computer/Documents/Unix/UNIX20/script.html A few sample scripts you can look through: http://www.ict.pwr.wroc.pl/doc/Unixhelp/scrpt_scrpt2.x.html John $Id: shell.txt,v 1.1 2000/02/16 08:07:50 jim Exp $