What Is a Shell?
A shell is a program that provides an interface between a user and an operating system (OS) kernel. An OS starts a shell for each user when the user logs in or opens a terminal or console window.
What Is a Shell Script?
A shell script is a file that contains shell and UNIX commands. Similar to compiled executable programs, the shell script has a specific purpose and is reusable. When the commands in the shell script are ordered and error-free, you can execute the shell script to carry out the tasks efficiently.
All scripts, programs, and procedures use the following rules:
- They should execute without error.
- They perform the task for which they are intended.
- They ensure that the program logic is clearly defined or apparent.
- They should not incorporate unnecessary work.
Creating Shell Scripts
A shell script is a text file that contains a sequence of UNIX commands. The shell script is often a command sequence for which you have a repeated use. Typically, you execute the sequence of commands in the shell script by entering the name of the shell script on the command line.
To create a shell script, create a file, and put UNIX commands into it. UNIX commands include standard utilities, user programs, and the names of other scripts that you need to accomplish your task.
A script file can have any name following the conventions of regular file names in the operating-system environment. When naming your shell programs you should avoid using names that conflict with existing UNIX commands or shell functions.
The following example script uses the echo command to inform the user about the output to expect before the actual command that creates the output is called. It is good practice when writing scripts to inform users about what is happening so they have an idea of whether the script is running correctly.
# vi my_script.sh #!/bin/sh clear echo "SCRIPT BEGINS" echo "Hello $LOGNAME!"
Executing a Shell Script
To execute a shell script as you do other commands on the system, first assign it the execute permission with the chmod command; for example :
# chmod +x my_script.sh
This is the preferred and most common method for executing a shell script. A subshell is created to execute the specified script. Give execute permission only to users who need to execute the script.
When a script is executed in a subshell, the variables, aliases, and functions created and used in the script are known only in the subshell. After the script finishes and control returns to the parent shell, the variables, functions, and other changes to the state of the shell made by the script are no longer known.
The following example runs the script in the current shell:
# . ./my_script.sh
The following example shows executing a script with debugging:
# sh -x my_script.sh
Let us execute the script we created in the section above and see if it outputs the correct result.
sh my_script.sh SCRIPT BEGINS Hello root!
Starting a Script With the #! Characters
When running a script that has execute permission by typing the script name on the command line, you should know the subshell (sh, csh, ksh, bash, or other subshells) that will run the script.
The first line of the script determines the shell that is created as the interpreter of the script. The first two characters on the first line in a script are #!. The current shell interprets what follows as the path name for the subshell to execute the script.
In the following examples, the specified shell is forked as the subshell to run the script.
#!/bin/sh #!/bin/csh #!/bin/ksh
Putting Comments in a Script
The persons who write scripts usually are not the only persons who read them. Many persons run scripts written by others. If they view the contents of the script file to see what the script does, it is helpful if the author of the script included some explanations in comments.
It is a good practice to put comments into programs and shell scripts. The comments should explain the purpose of the script and should explain any specific lines that might be especially confusing.
The example script, my_script.sh shown below, has a comment at the beginning of the script explaining what the script does when it runs.
# cat my_script.sh #!/bin/sh # This script clears the window, greets the user. clear echo "SCRIPT BEGINS" echo "Hello $LOGNAME!"
The addition of comments does not affect the execution of the script unless a syntactical error is introduced when the comments are added. The comments are there for documentation purposes, so someone reading the script will have an idea of what will occur when the script is executed.
Comments are sometimes the first lines entered in a shell script. The comments can list the step-by-step operations that need to take place. Follow each comment line by the operating system commands to carry out the operation.