Unix scripts

From NoskeWiki
Jump to: navigation, search


NOTE: This page is a daughter page of: Unix

A Unix script is a text file of commands that can be executed, like a .bat file in DOS. Unix contains a powerful programming language with loops and variables. You should think about scripts whenever you find yourself doing the same thing (like renaming files) over and over again.

How to Make a Script

Here's a useful example which can go through a directory and rename all *.htm files as *.html:

Make a text file (eg with pico) with the following lines. The first line is special. It tells Unix what program or shell should execute the script. Other # lines are comments.

    #! /bin/csh
    # htm2html converts *.htm files to *.html
    foreach f (*.htm)
      set base=`basename $f .htm`
      mv $f $base.html

Save this in your home directory as htm2html (or whatever). Then make it user-executable by typing chmod 700 htm2html. After this a * will appear by the file name when you ls -F, to show that it is executable. Change to a directory with .htm files and type ~/htm2html, and it will do its stuff.

See Also