Lesson 10 D Identify Basics of Scripting Flashcards
Coding
means writing a series of instructions in the syntax of a particular language so that a computer will execute a series of tasks. There are many types of coding language
There are many types of coding language and many ways of categorizing them, but three helpful distinctions are as follows:
- A shell scripting language uses commands that are specific to an operating system.
- A general-purpose scripting language uses statements and modules that are independent of the operating system. This type of script is executed by an interpreter. The interpreter implements the language for a particular OS.
- A programming language is used to compile an executable file that can be installed to an OS and run as an app.
script
!/bin/bash
You can develop a script in any basic text editor, but using an editor with script support is more productive. Script support means the editor can parse the syntax of the script and highlight elements of it appropriately. For complex scripts and programming languages, you might use an integrated development environment (IDE). This will provide autocomplete features to help you write and edit code and debugging tools to help identify whether the script or program is executing correctly.
A Linux shell script uses the .SH extension by convention. Every shell script starts with a shebang line that designates which interpreter to use, such as Bash or Ksh. Each statement comprising the actions that the script will perform is then typically added on separate lines. For example, the following script instructs the OS to execute in the Bash interpreter and uses the echo command to write “Hello World” to the terminal:
echo ‘Hello World’
Remember that in Linux, the script file must have the execute permission set to run. Execute can be set as a permission for the user, group, or world (everyone). If a PATH variable to the script has not been configured, execute it from the working directory by preceding the filename with ./ (for example, ./hello.sh ), or use the full path.
scripting languages
To develop a script in a particular language, you must understand the syntax of the language. Most scripting languages share similar constructs, but it is important to use the specific syntax correctly. A syntax error will prevent the script from running, while a logical error could cause it to operate in a way that is different from what was intended.
Comments
!/bin/bash
It is best practice to add comments in code to assist with maintaining it. A comment line is ignored by the compiler or interpreter. A comment line is indicated by a special delimiter. In Bash and several other languages, the comment delimiter is the hash or pound sign ( # ).
Greet the world
echo ‘Hello World’
Variables
A variable is a label for some value that can change as the script executes. For example, you might assign the variable FirstName to a stored value that contains a user’s first name. Variables are usually declared, defined as a particular data type (such as text string or number), and given an initial value at the start of the routine in which they are used.
An argument or parameter is a variable that is passed to the script when it is executed. In Bash, the values $1, $2 , and so on are used to refer to arguments by position (the order in which they are entered when executing the script). Other languages support passing named arguments.
Branches and Loops
A script contains one or more statements. In the normal scheme of execution, each statement is processed in turn from top to bottom. Many tasks require more complex structures, however. You can change the order in which statements are executed based on logical conditions evaluated within the script. There are two main types of conditional execution: branches and loops.
Branches
!/bin/bash
A branch is an instruction to execute a different sequence of instructions based on the outcome of some logical test. For example, the following code will display “Hello Bobby” if run as ./hello.sh Bobby , executing the statement under “else”. If run with no argument, it prints “Hello World”:
Demonstrate If syntax in Bash
if [ -z “$1” ]
then
echo ‘Hello World’
else
echo “Hello $1”
fi
-z tests whether the first positional parameter ( $1 ) is unset or empty.
Loops
!/bin/bash
A loop allows a statement block to be repeated based on some type of condition. A “For” loop can be used when the number of iterations is predictable. The following command executes the ping command for each host address in 192.168.1.0/24:
Demonstrate For syntax in Bash
for i in {1..254}
do
ping -c1 “192.168.1.$i”
done
As well as “For” structures, loops can also be implemented by “While” statements. A “While” or “Until” loop repeats an indeterminate number of times until a logical condition is met. The following script pings the address supplied as an argument until a reply is received:
Demonstrate Until syntax in Bash
until ping -c1 “$1” &>/dev/null
do
echo “192.168.1.$1 not up”
done
echo “192.168.1.$1 up”
The condition executes the ping command and tests the result. When a reply is received, ping returns true. The &>/dev/null part stops the usual ping output from being written to the terminal by redirecting it to a null device.
Operators
Looping and branching structures depend on logical tests to determine which branch to follow or whether to continue the loop. A logical test is one that resolves to a TRUE or FALSE value. You need to be familiar with basic comparison and logical operators:
look at pic
Windows supports several distinct shell coding environments. The three commonly used are
PowerShell, Visual Basic Script, and the CMD interpreter.
Windows PowerShell (PS)
Windows PowerShell (PS) combines a script language with hundreds of prebuilt modules called cmdlets that can access and change most components and features of Windows and Active Directory. Cmdlets use a Verb-Noun naming convention. For example, Write-Host sends output to the terminal, while Read-Host prompts for user input.
Microsoft provides the Windows PowerShell Integrated Scripting Environment (ISE) for rapid development. PowerShell script files are identified by the .PS1 extension.
VBScript
VBScript is a scripting language based on Microsoft’s Visual Basic programming language. VBScript predates PowerShell. VBScript files are identified by the .VBS extension. VBScript is executed by the wscript.exe interpreter by default. Wscript.exe displays any output from the script in a desktop window or dialog. A script can also be run with cscript.exe to show output in a command prompt.
Batch Files
A shell script written for the basic Windows CMD interpreter is often described as a batch file. Batch files use the .BAT extension.
Bash and PowerShell/VBScript are closely tied
to the Linux and Windows operating systems respectively. There are many other platform-independent scripting and programming languages.