0% found this document useful (0 votes)
14 views5 pages

Local Security Shell Scripting

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Shell Scripting

A Linux Foundation Training Publication


www.training.linuxfoundation.org
(c) Copyright the Linux Foundation 2015. All rights reserved.

Local Security - Shell Scripting

Overview
A shell script is a program executed by the Unix shell. There are many shells to choose from,
the most common of which are the Bourne shell (sh), Bourne Again shell (bash), C shell (csh),
Korn shell (ksh), and Z shell (zsh).
Shell scripts are flexible and can be used to automate long, mundane, or complex tasks. A
great benefit to a shell script is that it uses the same commands and syntax as the those for
the command line, as well some common programming features.
In its simplest form, a shell script is a list of commands run in sequence. Every new line of
a shell script is a new command; indentation is used for large, multi-line commands.The
following basic script will report the logged in user and the present working directory of that
user when the script is run:
#!/bin/bash
whoami
pwd

Key Ideas
Shebang (or hashbang): The #! character set that precedes the file path (eg #!/bin/sh) of
the selected shell. This is the first line of the script and tells the kernel which interpreter to
use for the script.
#: The # is used for comments within the script. Anything on the line after this character will
not be interpreted as part of the script.
script_name.sh: The .sh suffix for a shell script file.
sh script_name.sh: The command for running the script on the command line.
clear: A command that clears the screen.
echo : A command that prints whatever string appears between the quotation marks on a
new line.

Example Scenario
Create a short shell script that clears the screen and prints Hello world! at the top.

Now Do It
1. Create a file called Hello_world.sh and open it in your chosen text editor.

22

Local Security - Shell Scripting

2. Include the bash shebang at the top of script.


3. Write a comment on a new line underneath the shebang that describes the script.
4. On a separate line, write the clear command.
5. On a separate line, write the echo command that will print Hello world!
6. Save and exit the file and run it on the command line.

If you remember nothing else...


A shell script is a list of commands run in sequence. Every new line is a new command.

23

Local Security - Shell Scripting

Answer Key
The hello_world.sh script should look like this:
#!/bin/bash
# This script clears the screen and prints Hello world!.
clear
echo Hello world!

24

Get Certified!
Get more information at http://training.linuxfoundation.org/certification/lfcs

You might also like