Unix commands can be placed into a file just like DOS commands. The commands in the file are executed in sequence as though you were typing each one from the command line and pressing enter after the command. The commands become a user-defined "Unix command".

To create a shell script,

  1. Open your favorite editor (nano or vi) and create a file with any name you choose. The filename will become the shell script's name and thus the name of a user-defined unix utility.
  2. Good scripting practice suggests that the first line of the script be:
    #!/bin/sh
    This Unix command tells the interpreter that is running the command, which Unix shell to use. Different shells (Bash, bourne, cshell) have different syntax. For the Bash and bourne shell use /bin/sh, for the cshell use /bin/csh/. A new shell will begin executing and the commands in your script will be executed in this new shell.
  3. type in
    echo hello
    echo my name
    echo "my name"
    echo $1
  4. Save the file
  5. Change the permissions on the file so that you can execute the file.
    chmod u+x file
  6. Issue the command file
  7. Issue the command file bye
  8. Help with the example

Give this a try ... write a script that outputs:
Hi username happy day of the week

Example Scripts
  1. Using the PID
  2. Built in shell variables
  3. Better use of PID
  4. Use /tmp
  5. Cleaning up tempfiles(and read)
  6. Setting variables to result of commands
  7. the set command
  8. more of the set command
Wouldn't it be nice to have a script of your own to copy Whitfield's samples during class? Write one! The first argument should be the file name. A second optional argument could be a partial path.