Skip to content

Independent notes on FreeBSD · published since 1999

The FreeBSD 'zine Features, news and community notes for FreeBSD

Issue 6 · July 2000

Reader written notes on FreeBSD, from the command line to the server room.

Open the current issue

April 1999 · Featured article

Shell Scripting

An Introduction

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 in another issue.

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

John

On the web

Further reading from the FreeBSD community and the projects named in this article.

Issue contents

Issue 4 · Apr 1999

FreeBSD vs. Linux

The Results of our Testing

By Rob Garrett

Well, it was my intention in this article to provide *clear* benchmark results of FreeBSD vs Linux, but it seems that the only thing that I was...

Read the article

Issue 4 · Apr 1999

Sound

Setting Up a PnP Soundcard

By Anonymous

Make sure that your soundcard is plug and play before you try any of the following. Make sure your bios is set to off for plug and play. Make sure...

Read the article

Issue 4 · Apr 1999

qmail

Migrating from Sendmail on a FreeBSD System

By Nathan Underwood

Intro As we talked about in last month's issue, qmail is "a secure, reliable, efficient, and simple message transfer agent." If you need a refresher...

Read the article