Re: shell scripting

From: Alan Connor (zzzzzz_at_xxx.yyy)
Date: 03/01/04


Date: Mon, 01 Mar 2004 21:18:35 GMT

On Mon, 1 Mar 2004 19:32:35 -0000, andrew young <andyyou1@tiscali.co.uk> wrote:
>
>
> would it be possible for someone to show me a simple shell scipt for a
> beginner please
>
>

I use bash and ash, and am assuming an sh-compatible shell here....

The simplest shell script is just a list of commands in a file that
has been made executable with:

$ chmod +x filename

Most scripts require a heading like this:

#!/bin/sh

But the simple list does not.

The commands are executed in order:

command 1 options arguments
command 2 options arguments

etc.

If you want the output of one command to be fed to the input of the next,
do:

command 1 options arguments | command2 options arguments

"|" in this context is called a "pipe".

To redirect the output of the command to a file instead of the screen, do:

command1 options arguments > newfile

That will wipe out any previous contents of newfile, or create the file if
it does not exist. The file will be in your home directory unless you
assign a different path.

To append the output of the command to that file:

command1 options arguments >> newfile.

This also will create the file if it doesn't exist.

Once you create the file and make it executable, then check to make sure
the name is not already taken for some other executable or alias or
function:

$ type filename

If it has, choose another name.

To run the script, do:

$ ./filename

(that is, if you are in the same directory that it is -- that's just a
shorthand for the full path to the script if you are in the same directory
if it isn't, then you need to use the full path to execute it:

$ /home/you/filename # etc.

The "#" above begins a comment, which means that the shell will ignore
anything past it on the same line.

(it's a good idea to put ".sh" at the end of all your scripts, so that you
can tell the file is a script at a glance)

Or, you can put the script in your PATH, which is a list of directories
where the shell looks for executables.

$ echo $PATH

will tell you where these are.

(you will need to be root, usually, to put files in your PATH)

Once it is in your PATH, then just entering the script name will
execute it.

A simple script:

#!/bin/sh

ls -l | sort > lsfile

less lsfile

That will send the output of ls -l through sort to lsfile (creating it first)
and then open the file in the pager less.

(putting the #!/bin/sh at the top is a good habit to develop, even if it
is not strictly necessary here)

To see how to use utilities like ls and sort, call up their manual pages
with:

$ man ls # etc

But keep in mind that the man pages are basically cheat sheets for experts,
and don't be surprised if they are not very helpful at first.

check out comp.unix.shell, and find the FAQ

Get http://rute.sourceforge.net to learn the basics.

(the first 9 chapters are enough for now :-)

HTH

AC

-- 
ed(1) Check out the original tutorials by Brian W.
Kernighan at the Ed Home Page  http://tinyurl.com/2aa6g