Classnotes | UNIX01 | RecentChanges | Preferences UNIX does not determine if a file is executable by that file's extension. UNIX tells if a file is executable depending upon it's exectutable permission setting we discussed earlier.
Thus, you actually have very fine control over who can execute a file and how they execute it. Consider the following file
-rwxr-xr-- 1 sam users 3575 May 25 19:21 makeflim
This file can be executed by the user "sam", and the members of the group "users", but everyone else can simply read the file.
The executable could be a binary executable or a text file containing some sort of scripting information. UNIX is clever enough to be able to tell the difference.
Beginning a Script
Under UNIX the way to begin a script is with a declaration telling UNIX what program will interpret it. The basic format is
#!/path/to/interpretor/executable [options]
For example, if our interpretor was Perl, which was located in "/usr/bin" and we wanted the Perl script to run with the "-w" option, our Perl script would start with the line:
#!/usr/bin/perl -w
If we wanted to write a Bash shell script, then our script would begin with
#!/bin/sh
Contents of a Shell Script
Any utility or function you would have access to from the shell command line is a completely valid command for the script. For example, if we wanted to write a shell script that would view the last ten lines of the dmesg log file in /var/log, our script would simply contain the command:
#!/bin/sh
tail /var/log/dmesg
Variables
You define a variable by specifying an "=" operator, for example
FRIEND=Dog
would define a variable "FRIEND" with the value "Dog". To dereference that variable, you would use the "$" modifier:
echo $FRIEND
would print "Dog" on the screen. A variable can be used anywhere in the script, even for command substitution:
LS="ls -la"
echo "Fetching a directory"
$LS
Several environmental variables exist which are automatically set when the script is run. These include things like HOME (the user's home directory), PATH (the path for the script), SHELL (the shell in use), and USERNAME (the user who ran the script). You can find a more complete listing of environmental variables on page 195 of the book.
One special class of environmental variables worth mentionning are those specifying command-line arguments. $1 gives the first command line argument, $2 gives the second, etc. For example, if I ran the program "args" thusly:
$ args fred bob linda
Then these variables would be set thusly:
$1 = fred
$2 = bob
$3 = linda
You can also access the argument list all together using $* and obtain the total number of arguments with $#.