[Novalug] script question (was Text User Interface...)

Peter Larsen plarsen@famlarsen.homelinux.com
Tue Mar 23 21:34:41 EDT 2010


On Tue, 2010-03-23 at 11:00 -0500, Beartooth wrote:
> On Mon, 22 Mar 2010, Peter Larsen wrote:
> 
> > [....] I want to save my procedure to a script [...]
> 
>  	I didn't know that could be done.
> 
>  	Maybe I shouldn't ask, since I'm not really competent to 
> write any script; but I do find occasionally that the one way I 
> can do a task involves completely or almost completely repetitive 
> keystrokes for some tedious interval. Is this something I might 
> could use?

Scripts sound tricky, but in their basic shape, they're no more than
just a "macro". That said, there's a ton of "additional" script-language
functions to make advanced scripts possible. But you don't need to do
that.

To create a script, simply take your favorite editor and create a text
file with the commands as you would otherwise have written them on the
command line - one line after another. The very first line should read:

#!/bin/bash

This is really a comment - every line that starts with # is ignored, but
the first line has special meaning. It allows the shell to determine
what "interpretor" to load to execute the script. It's not really
important to understand why it needs to be there, just make it the first
line.

Then you save your file. And that's it. You can execute the file by
typing:

 $ bash ./scriptname

If you want to avoid typing "bash" every time you run the script, set
the "execute" bit on the script by using:

 $ chmod +x scriptname

If you place the script in ~/bin you can simply execute it by typing:

 $ scriptname

If you don't put it in ~/bin you need to give the path to the file. If
it's in the current directory, use the ./ notation.

>  	Prithee (or pray somebody) expatiate discursively, or 
> toss me a clue!

The above is the basic/simple part of a script. But usually we do a
little more intelligent stuff than just reading static command lines
from a file. With bash we have variables, "if" statements, loops,
pattern matching, traps, etc. etc. etc.

#!/bin/bash
DEST=${1}:/backup
SRC=$HOME
rsync -a ${SRC} ${DEST}

#EOF

The use of $ indicates that the next is a variable. The { } is a good
practice, but not necessary. Ie. $DEST and ${DEST} does the same thing.
In a few situations you HAVE to use { } when you don't have a space
after the variable name for instance. So using them all the time is a
good idea.

When you say DEST= it means that the value on the right side will be
assigned to the variable DEST. 

The ${1} is a little "tricky" - it simply refers to the first parameter
sent to the command. So if you name the script "backup" and put it in
~/bin - by simply typing: $ backup remote.backup.com 
You run a backup that copies your home directory and content to an
external server called "remote.backup.com".

Do a "man bash" to see all script language options. It can be a little
overwhelming at first. Just remember that the basic script simply is a
list of commands as typed on the command line. The rest is "programming
techniques". 

-- 
Best Regards
  Peter Larsen

Wise words of the day:
Real Men don't make backups.  They upload it via ftp and let the world mirror it.
	-- Linus Torvalds
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <https://lists.firemountain.net/pipermail/novalug/attachments/20100323/eb7d1657/attachment.asc>


More information about the Novalug mailing list