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

Michael Henry lug-user at drmikehenry.com
Tue Mar 23 19:55:59 EDT 2010


On 03/23/2010 12:00 PM, Beartooth wrote:
> On Mon, 22 Mar 2010, Peter Larsen wrote:
>
>> [....] I want to save my procedure to a script [...]
>
>      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 are great.  Anything you can type interactively at the
Bash prompt can be saved in a "script" file and executed at a
later time.  In many cases, all you really need to know is how
to use a text editor to create a file, and how to make the file
executable, and you are in business.

For example, at my house we have four people using Thunderbird.
We have an admittedly weak method for sharing a single common
address book.  All changes are done by me with my copy of
Thunderbird, then I copy my address book file to the Thunderbird
directory for the others in my family.  It's tedious to re-type
the copy commands, so I've made a script.  Here's a simplified
version of that script:

  #!/bin/bash

  cp /home/mike/.thunderbird/default/abook.mab  \
     /home/beth/.thunderbird/default/abook.mab

  cp /home/mike/.thunderbird/default/abook.mab  \
     /home/andrew/.thunderbird/default/abook.mab

  cp /home/mike/.thunderbird/default/abook.mab  \
     /home/sarah/.thunderbird/default/abook.mab

The end-of-line backslashes are for continuing a long line onto
the next line in the file.  There are three commands in the
above script.  I've saved the script as the file ``copy-abook``,
and placed it in my executable PATH.  Now, whenever I update my
address book, I just go to a Bash prompt and type the following
to update the other three address books:

  copy-abook

Because the script is in my PATH, Bash knows where to find it,
so I can run it by just typing its name.  Often, the directory
``~/bin`` is already in your PATH, so that's a good place to
store such a file.  You may create it if it's not already there.
The first line of the script should be a so-called "shebang"
line (the "she" comes from the "hash" symbol, and the "bang"
comes from the "bang", or exclamation point).  Text files
beginning with a shebang line are scripts.  The kernel can tell
it's a script, and will parse the rest of the line to see what
kind of script it is.  To write a script of Bash commands, you
put the full path to Bash on the shebang line (no spaces after
"#!"), like you see above.

Also, for the kernel to consider the file to be executable, the
executable permission bit has to be set on the script file.  You
can see if the bits are set using ``ls -l``.  For my
``copy-abook`` script, the bits are set:

  ls -l copy-abook
  -rwxr-xr-x 1 mike mike 245 Feb  3 07:06 copy-abook

Notice the "rwx" bits, which mean "Read", "Write", and
"eXecute".  You need the "x" bit to be set, or the kernel won't
try to execute your script.

The ``chmod`` command changes the mode bits (permission bits) on
files.  See below for an example.

Note that script files generally use "#" to mark the start of a
comment, so you can put notes to yourself in the file as long as
you start the line with "#".  In fact, the shebang line is just
a comment as far as Bash is concerned, but the kernel treats it
specially.

To give this a whirl, try out the following.  First, make sure
the ~/bin directory exists by running this command:

  mkdir -p ~/bin

Then, use a text editor to create a script file in the ``~/bin``
directory.  Name the file ``helloworld`` (the traditional name).
Put the following lines in that file:

  #!/bin/bash
  # Say hello to the world.  This line is just a comment.
  echo "Hello, World!"

Now, make sure the new script is executable:

  chmod +x ~/bin/helloworld

You can check the bits using ``ls`` as before, by typing:

  ls -l ~/bin/helloworld

It should look something like this:

-rwxr-xr-x 1 mike mike 54 Mar 23 19:52 /home/mike/bin/helloworld

Notice the "x" bits on the left; this script is now executable.

You should now be able to execute the script at the prompt by
simply typing its name like this:

  helloworld

If you get this error:

  bash: helloworld: command not found

It means that ``~/bin`` is not in your PATH after all.  If so,
let us know and we can help you get that setup right on your
system.  You can still execute the script, but you'll need to
type the path to the script, like this:

  ~/bin/helloworld

There is much more that can be done with scripts.  It's an art
form well worth learning.  Sophisticated scripts that can check
for various conditions and modify their behavior based on what
they find out can be very powerful, but you don't need anything
fancy to get immediate benefit from script files, and you can
learn more as your desire for automation increases.

As another simple example, suppose you store your Garmin maps in
a particular directory.  When you launch your terminal, you are
probably in your home directory.  You might want to make a
script that changes to your Garmin directory and then shows you
what maps are available (of course, I'm making all this up based
on some of your previous posts, so consider it motivational
fiction).  In any case, if you'd normally execute these two
commands to do that task:

  cd ~/garmin/maps
  ls -l *.map

The you could make a script file named ``maps`` with a shebang
line and those two commands and store it in ``~/bin``.  Then,
whenever you wanted to check out your maps, you'd just type:

  maps

And you'd be in your maps directory with your maps files already
listed.

Michael Henry




More information about the Novalug mailing list