Creating and running a shell script

A shell script is a file that contains one or more commands. Shell scripts provide an easy way to carry out tedious commands, large or complicated sequences of commands, and routine tasks. When you enter the name of a shell script file, the system executes the command sequence contained by the file.

You can create a shell script by using a text editor. Your script can contain both operating system commands and shell built-in commands.

The following steps are general guidelines for writing shell scripts:
  1. Using a text editor, create and save a file. You can include any combination of shell and operating system commands in the shell script file. By convention, shell scripts that are not set up for use by many users are stored in the $HOME/bin directory.
    Note: The operating system does not support the setuid or setgid subroutines within a shell script.
  2. Use the chmod command to allow only the owner to run (or execute) the file. For example, if your file is named script1, type the following:
    chmod u=rwx script1
  3. Type the script name on the command line to run the shell script. To run the script1 shell script, type the following:
    script1
    Note: You can run a shell script without making it executable if a shell command (ksh, bsh, or csh) precedes the shell script file name on the command line. For example, to run a nonexecutable file named script1 under the control of the Korn shell, type the following:
    ksh script1