Scripting 101 - Hello World!
This post kicks off the scripting tutorial series. It covers how to create, edit, and execute a simple "Hello World!" script using two popular & widely used languages: Bash & Python. With this core understanding, a user can perform many automated tasks in a local or remote location. After you have a basic understanding of script creation & use, the series will continue with more complex methodologies.
Prerequisites
- Basic Computer Operator Capability
- Python Installation Completed
Introduction
Scripting is the concept of using a computer language to automate a series of commands. Typically, these commands are simple and can be executed one-by-one manually; however, automation offers many benefits. Typically, scripts are able to be ran without having the code be compiled first. Note, we will not cover compiled code in this guide.
A script's scope can be either very simple to complex. Scripts can even be part of a bigger project. The project can be a complex compiled solution; which, then runs scripts similar to how a user would execute the scripts manually.
There are many pros & cons with the use of scripting:
Pros | Cons |
---|---|
|
|
Before worrying too much about complexity, we're going to start off with simple concepts. Start by defining common used coding terminology, then move into conceptualization, and then finally making that concept into reality. By the end, you should be able to write simple scripts on your own.
Scripting Languages
Scripts can be written in many different languages. Each comes with their own pros & cons. Some of the most widely used scripting languages include:
|
|
In this tutorial we will cover how to take a concept for a script & turn it into reality using a few of these scripting examples.
Definitions
There are many common terms used within the coding/scripting world. This post will cover a few of the basic & commonly used terms.
Terminology | Definition |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hello World!
Time to dive into the tried and true method of learning how to code; or, commonly known as the "Hello World!" solution. The idea of the "Hello World!" solution is introduce a user to the syntax & the basic steps to getting output with a selected language. It has been used time and time again to introduce the essentials to a beginning user.
This guide will include examples using two scripting languages, Python & Bash, to perform the same output.
NOTE: The Linux environment used in this guide is: Linux kali 4.15.0.
NOTE: The Windows environment used in this guide is: Microsoft Windows 10 Home
Bash
Bash is a command processor that typically runs in a text window where the user types commands that cause actions. Bash can also read and execute commands from a file, called a shell script. Like all Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration. The keywords, syntax and other basic features of the language are all copied from sh.
Linux Example
Step 1
Start by creating a new file in a desired location. Name the file "helloworld.sh", make sure that the ".sh" file extension is present.
Command Line | Screen Shots |
---|---|
|
Step 2
Using your prefered method, edit the file. In this Linux example, I use the GNU Nano text editor.
Command Line | Screen Shots |
---|---|
|
Step 3
Finally, execute the script VIA command line.
Command Line | Screen Shots |
---|---|
|
Windows Example
NOTE: The following steps require that you have Windows Subsystem for Linux installed. This guide will not cover how up install this service.
Step 1
Start by creating a new file in a desired location. Name the file "helloworld.sh", make sure that the ".sh" file extension is present.
Command Line | Screen Shots |
---|---|
|
Step 2
Using your prefered method, edit the file. In this Windows example, I use the GNU Nano text editor.
Command Line | Screen Shots |
---|---|
|
Step 3
Finally, execute the script VIA command line.
Command Line | Screen Shots |
---|---|
|
Python
Python is an open-source high-level programming language for general-purpose programming. Python supports multiple programming paradigms, including: object-oriented, imperative, functional & procdural. Additionally, there are many code libraries available.
Linux Example
Step 1
Start by created a new file in a desired location. Name the file "helloworld.py", make sure that the ".py" file extension is present.
Command Line | Screen Shots |
---|---|
|
Step 2
Using your prefered method, edit the file. In this Linux example, I use the GNU Nano text editor.
Command Line | Screen Shots |
---|---|
|
Step 3
Finally, execute the script VIA command line.
Command Line | Screen Shots |
---|---|
|
Windows Example
Step 1
Start by created a new file in a desired location. Name the file "helloworld.py", make sure that the ".py" file extension is present.
Command Line | Screen Shots |
---|---|
|
Step 2
Using your prefered method, edit the file. In this Windows example, I echo the command input to the file.
Command Line | Screen Shots |
---|---|
|
Step 3
Finally, execute the script VIA command line.
Command Line | Screen Shots |
---|---|
|
Summary
As this guide comes to a close we'll cover what was taught. We covered how to create a script file, put correct syntax into the file, and execute the script. While these steps are rather simple, the basics covered are critical in any developer's world. Understanding how to use the command line to create & execute a script is very critical knowledge in a Penetration Tester's world.
Up Next
Up next is Scripting 101 - Variables. This topic covers the how to assign variables data & why this is important. It will discuss how to manipulate the data, pass variables as arguments to functions, and how variables can be used to keep clean read-able code.
Comments
Post a Comment