Executing Basic Unix Shell Scripts
Writing Your First Shell Script in Unix
So far in this course you have learned a lot about shells and shell scripts, but you have not actually created one.
You have learned what types of shells are available, how shell scripts can be used compared to other scripting languages,
and what components make up a shell script.
This module will discuss creating and executing your first shell script.
By the end of this module you will be able to:
- Define what makes a text file a shell script
- Write information to the screen using a script command
- Read keyboard input using a script command
- Add explanatory comments in your script
- Set the UNIX file permissions to allow your script file to be executed
- Run a script file from a command line
What is a shell script?
Shell scripts are short programs that are written in a shell programming language and interpreted by a shell process.
They are extremely useful for automating tasks on Linux and other Unix-like operating systems.
A shell is a program that provides the traditional, text-only user interface for Unix-like operating systems.
Its primary function is to read commands that are typed into a console, which uses a text display mode, (also known as a terminal window).
The default shell on Linux is the very commonly used and highly versatile bash.
A feature of bash and other shells used on Unix-like operating systems is that each contains a built-in programming language, referred to as a shell programming language or
shell scripting language, which is used to create shell scripts.
Among the advantages of using shell scripts are that they can be very easy to create and that a large number are already available in books and on the
Internet for use with or without modification for a wide variety of tasks.
Shell scripts are also employed extensively in the default installations of Unix-like operating systems.
Brief History of Computing
In the early days of personal computing, the BASIC language enabled anyone reasonably computer proficient to write programs on an early generation of microcomputers. Decades later, the Bash scripting language enables anyone with a rudimentary knowledge of Linux or UNIX to do the same on modern machines.
We now have miniaturized single-board computers with amazing capabilities, such as the Raspberry Pi. Bash scripting provides a way to explore the capabilities of these fascinating devices.
A shell script is a quick-and-dirty method of prototyping a complex application. Getting even a limited subset of the functionality to work in a script is often a useful first stage in project development.
In this way, the structure of the application can be tested and tinkered with, and the major pitfalls found before proceeding to the final coding in C, C++, Java, Perl, or Python.
Shell scripting hearkens back to the classic UNIX philosophy of breaking complex projects into simpler subtasks, of chaining together components and utilities. Many consider this a better, or at least more
esthetically pleasing approach to problem solving than using one of the new generation of high-powered all-in-one languages, such as Perl, which attempt to be all things to all people, but at the cost of forcing you to
alter your thinking processes to fit the tool.