Skip to content

Latest commit

 

History

History
1962 lines (1500 loc) · 87 KB

File metadata and controls

1962 lines (1500 loc) · 87 KB

Script Language Syntax

TODO: need syntax examples for Bash and Windows Batch scripting (issue #22)

  • Add syntax examples for Bash and Windows Batch scripting
  • Add example output for all

Basic syntax examples for Python, PowerShell, Bash, and Windows cmd.exe batch

Variables

{% tabs %} {% tab title="Python" %}

Type Code Examples
Standard Variable var = "Hello"
Global Variable

global var

var = "Hello"

Environment Variables
Retrieving Variable Contents
{% endtab %}

{% tab title="PowerShell" %}

Type Code Examples
Standard Variable $var = "Hello"
Global Variable $global:var = "Hello"
Environment Variables
Retrieving Variable Contents
{% endtab %}

{% tab title="Bash" %} TODO: this

Type Code Examples
Standard Variable
Global Variable
Environment Variables
Retrieving Variable Contents
{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Type Code Examples
Standard Variable
Global Variable
Environment Variables
Retrieving Variable Contents

Set Command

The other way in which variables can be initialized is via the ‘set’ command. Following is the syntax of the set command.

Syntax

set /A variable-name=value

where,

  • variable-name is the name of the variable you want to set.
  • value is the value which needs to be set against the variable.
  • /A – This switch is used if the value needs to be numeric in nature.

The following example shows a simple way the set command can be used.

Example

@echo off 
set message=Hello World 
echo %message%

Working with Numeric Values

In batch script, it is also possible to define a variable to hold a numeric value. This can be done by using the /A switch.

The following code shows a simple way in which numeric values can be set with the /A switch.

@echo off 
SET /A a = 5 
SET /A b = 10 
SET /A c = %a% + %b% 
echo %c%

Local vs Global Variables

In any programming language, there is an option to mark variables as having some sort of scope, i.e. the section of code on which they can be accessed. Normally, variable having a global scope can be accessed anywhere from a program whereas local scoped variables have a defined boundary in which they can be accessed.

DOS scripting also has a definition for locally and globally scoped variables. By default, variables are global to your entire command prompt session. Call the SETLOCAL command to make variables local to the scope of your script. After calling SETLOCAL, any variable assignments revert upon calling ENDLOCAL, calling EXIT, or when execution reaches the end of file (EOF) in your script. The following example shows the difference when local and global variables are set in the script.

Example

@echo off 
set globalvar = 5
SETLOCAL
set var = 13145
set /A var = %var% + 5
echo %var%
echo %globalvar%
ENDLOCAL

Few key things to note about the above program.

  • The ‘globalvar’ is defined with a global scope and is available throughout the entire script.
  • The ‘var‘ variable is defined in a local scope because it is enclosed between a ‘SETLOCAL’ and ‘ENDLOCAL’ block. Hence, this variable will be destroyed as soon the ‘ENDLOCAL’ statement is executed.

Working with Environment Variables

If you have variables that would be used across batch files, then it is always preferable to use environment variables. Once the environment variable is defined, it can be accessed via the % sign. The following example shows how to see the JAVA_HOME defined on a system. The JAVA_HOME variable is a key component that is normally used by a wide variety of applications.

@echo off 
echo %JAVA_HOME%

The output would show the JAVA_HOME directory which would depend from system to system. Following is an example of an output.

C:\Atlassian\Bitbucket\4.0.1\jre

{% endtab %} {% endtabs %}

Strings

{% tabs %} {% tab title="Python" %}

Method Code Examples
Normal String

"Hello World"

'Hello World'

Empty String

""

''

Multiline String

"""Hello

World"""

Select Character from String

str = 'Hello'

str[1]

# 'e'

Get Length

str = 'Hello'

len(str)

# 5

Remove whitespace at front and back

str = ' Hello World '

str.strip()

# 'Hello World'

To Lowercase

str = 'HELLO WORLD'

str.lower()

# 'hello world'

To Uppercase

str = 'hello world'

str.upper()

# 'HELLO WORLD'

Replace

str = 'Hello'

str.replace('H', 'Y')

# 'Yello'

Split

str = 'Hello, World'

str.split(',')

# ['Hello', ' World']

Join

list = ["Hello", "World"]

", ".join(list)

# 'Hello World'

Formatting

price = 42

txt = "The price is {} dollars"

print(txt.format(price))

# The price is 42 dollars

Formatting by Index

price = 42

txt = "The price is {0} dollars"

print(txt.format(price))

# The price is 42 dollars

Formatting Strings

price = 42

f"The price is {price} dollars"

# The price is 42 dollars

{% endtab %}

{% tab title="PowerShell" %}

Method Code Examples
Normal String

"Hello World"

'Hello World'

Empty String

""

''

Multiline String

"Hello

World

"

Select Character from String

$str = 'Hello'

$str[1]

# e

Get Length

$str = 'Hello'

$str.Length

# 5

Remove whitespace at front and back

$str = ' Hello World '

$str.Trim()

# 'Hello World'

To Lowercase

$str = 'HELLO WORLD'

$str.ToLower()

# hello world

To Uppercase

$str = 'hello world'

$str.ToUpper()

# HELLO WORLD

Replace

$str = 'Hello'

$str.Replace('H', 'Y')

# Yello

Split

'Hello, World' -split ','

# @('Hello', ' World')

Join

$array = @("Hello", "World")

$array -join ", "

[String]::Join(', ', $array)

# Hello World

Formatting

$price = 42

$txt = "The price is {0} dollars"

$txt -f $price

# The price is 42 dollars

Formatting by Index

$price = 42

$txt = "The price is {0} dollars"

$txt -f $price

# The price is 42 dollars

Formatting Strings

$price = 42

$txt = "The price is $price dollars"

# The price is 42 dollars

{% endtab %}

{% tab title="Bash" %} TODO: this

Method Code Examples
Normal String
Empty String
Multiline String
Select Character from String
Get Length
Remove whitespace at front and back
To Lowercase
To Uppercase
Replace
Split
Join
Formatting
Formatting by Index
Formatting Strings

Basics

name="John"
echo ${name}
echo ${name/J/j}    #=> "john" (substitution)
echo ${name:0:2}    #=> "Jo" (slicing)
echo ${name::2}     #=> "Jo" (slicing)
echo ${name::-1}    #=> "Joh" (slicing)
echo ${name:(-1)}   #=> "n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake}  #=> $food or "Cake"
length=2
echo ${name:0:length}  #=> "Jo"

See: Parameter expansion

STR="/path/to/foo.cpp"
echo ${STR%.cpp}    # /path/to/foo
echo ${STR%.cpp}.o  # /path/to/foo.o
echo ${STR%/*}      # /path/to

echo ${STR##*.}     # cpp (extension)
echo ${STR##*/}     # foo.cpp (basepath)

echo ${STR#*/}      # path/to/foo.cpp
echo ${STR##*/}     # foo.cpp

echo ${STR/foo/bar} # /path/to/bar.cpp
STR="Hello world"
echo ${STR:6:5}   # "world"
echo ${STR: -5:5}  # "world"
SRC="/path/to/foo.cpp"
BASE=${SRC##*/}   #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE}  #=> "/path/to/" (dirpath)

Default values

${FOO:-val} $FOO, or val if unset (or null)
${FOO:=val} Set $FOO to val if unset (or null)
${FOO:+val} val if $FOO is set (and not null)
${FOO:?message} Show error message and exit if $FOO is unset (or null)

Omitting the : removes the (non)nullity checks, e.g. ${FOO-val} expands to val if unset otherwise $FOO.

Substitution

${FOO%suffix} Remove suffix
${FOO#prefix} Remove prefix
${FOO%%suffix} Remove long suffix
${FOO##prefix} Remove long prefix
${FOO/from/to} Replace first match
${FOO//from/to} Replace all
${FOO/%from/to} Replace suffix
${FOO/#from/to} Replace prefix

Substrings

${FOO:0:3} Substring (position, length)
${FOO:(-3):3} Substring from the right

Length

${#FOO} Length of $FOO

Manipulation

STR="HELLO WORLD!"
echo ${STR,}   #=> "hELLO WORLD!" (lowercase 1st letter)
echo ${STR,,}  #=> "hello world!" (all lowercase)

STR="hello world!"
echo ${STR^}   #=> "Hello world!" (uppercase 1st letter)
echo ${STR^^}  #=> "HELLO WORLD!" (all uppercase)

String quotes

NAME="John"
echo "Hi $NAME"  #=> Hi John
echo 'Hi $NAME'  #=> Hi $NAME

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Method Code Examples
Normal String
Empty String
Multiline String
Select Character from String
Get Length
Remove whitespace at front and back
To Lowercase
To Uppercase
Replace
Split
Join
Formatting
Formatting by Index
Formatting Strings

An empty string can be created in DOS Scripting by assigning it no value during it’s initialization as shown in the following example.

Set a=

To check for an existence of an empty string, you need to encompass the variable name in square brackets and also compare it against a value in square brackets as shown in the following example.

[%a%]==[]

The following example shows how an empty string can be created and how to check for the existence of an empty string.

Example

@echo off 
SET a= 
SET b=Hello 
if [%a%]==[] echo "String A is empty" 
if [%b%]==[] echo "String B is empty "

A string can be created in DOS in the following way.

Example

@echo off 
:: This program just displays Hello World 
set message = Hello World 
echo %message%

{% endtab %} {% endtabs %}

Type Casting

{% tabs %} {% tab title="Python" %}

Type Code Examples
As Integer i = int("10")
As Float i = float("10.5")
As String i = str(10)
As Char
{% endtab %}

{% tab title="PowerShell" %}

Type Code Examples
As Integer $i = [int]"10"
As Float $i = [float]"10.5"
As String $i = [string]10
As Char
{% endtab %}

{% tab title="Bash" %}

Type Code Examples
As Integer
As Float
As String
As Char
{% endtab %}

{% tab title="CMD .bat" %}

Type Code Examples
As Integer
As Float
As String
As Char
{% endtab %}
{% endtabs %}

Arrays

{% tabs %} {% tab title="Python" %}

Activity Code examples
Define ['Hello', 'World']
Access Elements

arr = ['Hello', 'World']

arr[0]

# 'Hello'

Get Length

arr = ['Hello', 'World']

len(arr)

Adding Elements

arr = ['Hello', 'the']

arr.append('World')

Removing Elements

arr = ['Hello', 'World']

arr.pop(1)

Remove Element by Value

arr = ['Hello', 'World']

arr.remove('Hello')

{% endtab %}

{% tab title="PowerShell" %}

Activity Code examples
Define @('Hello', 'World')
Access Elements

$arr = @('Hello', 'World')

$arr[0]

# Hello

Get Length

$arr = @('Hello', 'World')

$arr.Length

Adding Elements

$arr = @('Hello', 'the')

$arr += "World"

Removing Elements

$arr = [System.Collections.ArrayList]@('Hello', 'World')

$arr.RemoveAt($arr.Count - 1)

Remove Element by Value

$arr = [System.Collections.ArrayList]@('Hello', 'World')

$arr.Remove("Hello")

{% endtab %}

{% tab title="Bash" %} TODO: this

Activity Code examples
Define
Access Elements
Get Length
Adding Elements
Removing Elements
Remove Element by Value

Working with arrays

echo ${Fruits[0]}           # Element #0
echo ${Fruits[-1]}          # Last element
echo ${Fruits[@]}           # All elements, space-separated
echo ${#Fruits[@]}          # Number of elements
echo ${#Fruits}             # String length of the 1st element
echo ${#Fruits[3]}          # String length of the Nth element
echo ${Fruits[@]:3:2}       # Range (from position 3, length 2)
echo ${!Fruits[@]}          # Keys of all elements, space-separated

Operations

Fruits=("${Fruits[@]}" "Watermelon")    # Push
Fruits+=('Watermelon')                  # Also Push
Fruits=( ${Fruits[@]/Ap*/} )            # Remove by regex match
unset Fruits[2]                         # Remove one item
Fruits=("${Fruits[@]}")                 # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`)                 # Read from file

Iteration

for i in "${arrayName[@]}"; do
  echo $i
done

Defining arrays

Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Activity Code examples
Define
Access Elements
Get Length
Adding Elements
Removing Elements
Remove Element by Value

Arrays are not specifically defined as a type in Batch Script but can be implemented. The following things need to be noted when arrays are implemented in Batch Script.

  • Each element of the array needs to be defined with the set command.
  • The ‘for’ loop would be required to iterate through the values of the array.

Creating an Array

An array is created by using the following set command.

set a[0]=1

Where 0 is the index of the array and 1 is the value assigned to the first element of the array.

Another way to implement arrays is to define a list of values and iterate through the list of values. The following example show how this can be implemented.

Example

@echo off 
set list = 1 2 3 4 
(for %%a in (%list%) do ( 
   echo %%a 
))

Output

The above command produces the following output.

1
2
3
4

Accessing Arrays

You can retrieve a value from the array by using subscript syntax, passing the index of the value you want to retrieve within square brackets immediately after the name of the array.

Example

@echo off 
set a[0]=1 
echo %a[0]%

In this example, the index starts from 0 which means the first element can be accessed using index as 0, the second element can be accessed using index as 1 and so on. Let's check the following example to create, initialize and access arrays −

@echo off
set a[0] = 1 
set a[1] = 2 
set a[2] = 3 
echo The first element of the array is %a[0]% 
echo The second element of the array is %a[1]% 
echo The third element of the array is %a[2]%

The above command produces the following output.

The first element of the array is 1 
The second element of the array is 2 
The third element of the array is 3

Modifying an Array

To add an element to the end of the array, you can use the set element along with the last index of the array element.

Example

@echo off 
set a[0] = 1  
set a[1] = 2  
set a[2] = 3 
Rem Adding an element at the end of an array 
Set a[3] = 4 
echo The last element of the array is %a[3]%

The above command produces the following output.

The last element of the array is 4

You can modify an existing element of an Array by assigning a new value at a given index as shown in the following example −

@echo off 
set a[0] = 1 
set a[1] = 2  
set a[2] = 3 
Rem Setting the new value for the second element of the array 
Set a[1] = 5 
echo The new value of the second element of the array is %a[1]%

The above command produces the following output.

The new value of the second element of the array is 5

Iterating Over an Array

Iterating over an array is achieved by using the ‘for’ loop and going through each element of the array. The following example shows a simple way that an array can be implemented.

@echo off 
setlocal enabledelayedexpansion 
set topic[0] = comments 
set topic[1] = variables 
set topic[2] = Arrays 
set topic[3] = Decision making 
set topic[4] = Time and date 
set topic[5] = Operators 

for /l %%n in (0,1,5) do ( 
   echo !topic[%%n]! 
)

Following things need to be noted about the above program −

  • Each element of the array needs to be specifically defined using the set command.
  • The ‘for’ loop with the /L parameter for moving through ranges is used to iterate through the array.

Output

The above command produces the following output.

Comments 
variables 
Arrays 
Decision making 
Time and date 
Operators

Length of an Array

The length of an array is done by iterating over the list of values in the array since there is no direct function to determine the number of elements in an array.

@echo off 
set Arr[0] = 1 
set Arr[1] = 2 
set Arr[2] = 3 
set Arr[3] = 4 
set "x = 0" 
:SymLoop 

if defined Arr[%x%] ( 
   call echo %%Arr[%x%]%% 
   set /a "x+=1"
   GOTO :SymLoop 
)
echo "The length of the array is" %x%

Output

Output The above command produces the following output.

The length of the array is 4

{% endtab %} {% endtabs %}

Conditionals

{% tabs %} {% tab title="Python" %}

Switch Code Examples
If / ElseIf / Else

a = 42

b = 420

if b > a:

print("b is greater than a")

elif a == b:

print("a and b are equal")

else:

print("a is greater than b")

Case
{% endtab %}

{% tab title="PowerShell" %}

Switch Code Examples
If / ElseIf / Else

$a = 42

$b = 420

if ($b -gt $a)

{

Write-Host "b is greater than a"

}

elseif ($a -eq $b)

{

Write-Host "a and b are equal"

}

else

{

Write-Host "a is greater than b"

}

Case
{% endtab %}

{% tab title="Bash" %} TODO: this

Switch Code Examples
If / ElseIf / Else
Case

Case/switch

case "$1" in
  start | up)
    vagrant up
    ;;

  *)
    echo "Usage: $0 {start|stop|ssh}"
    ;;
esac
# String
if [[ -z "$string" ]]; then
  echo "String is empty"
elif [[ -n "$string" ]]; then
  echo "String is not empty"
else
  echo "This never happens"
fi

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Switch Code Examples
If / ElseIf / Else
Case

The first decision-making statement is the ‘if’ statement. The general form of this statement in Batch Script is as follows −

if(condition) do_something

The general working of this statement is that first a condition is evaluated in the ‘if’ statement. If the condition is true, it then executes the statements. The following diagram shows the flow of the if statement.

Checking Variables

One of the common uses for the ‘if’ statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the ‘if’ statement can be done for both strings and numbers.

Checking Integer Variables

The following example shows how the ‘if’ statement can be used for numbers.

Example

@echo off 
SET /A a = 5 
SET /A b = 10 
SET /A c = %a% + %b% 
if %c%==15 echo "The value of variable c is 15" 
if %c%==10 echo "The value of variable c is 10"

The key thing to note about the above program is −

  • The first ‘if’ statement checks if the value of the variable c is 15. If so, then it echo’s a string to the command prompt.
  • Since the condition in the statement - if %c% == 10 echo "The value of variable c is 10 evaluates to false, the echo part of the statement will not be executed.

Output

The above command produces the following output.

15

Checking String Variables

The following example shows how the ‘if’ statement can be used for strings.

Example

@echo off 
SET str1 = String1 
SET str2 = String2 
if %str1%==String1 echo "The value of variable String1" 
if %str2%==String3 echo "The value of variable c is String3"

The key thing to note about the above program is −

  • The first ‘if’ statement checks if the value of the variable str1 contains the string “String1”. If so, then it echo’s a string to the command prompt.
  • Since the condition of the second ‘if’ statement evaluates to false, the echo part of the statement will not be executed.

Output

The above command produces the following output.

"The value of variable String1"

Note − One key thing to note is that the evaluation in the ‘if’ statement is "case-sensitive”. The same program as above is modified a little as shown in the following example. In the first statement, we have changed the comparison criteria. Because of the different casing, the output of the following program would yield nothing.

@echo off 
SET str1 = String1 
SET str2 = String2 
if %str1%==StrinG1 echo "The value of variable String1" 
if %str2%==String3 echo "The value of variable c is String3"

{% endtab %} {% endtabs %}

Loops

{% tabs %} {% tab title="Python" %}

Loop Type Code Examples
For

fruits = ["apple", "banana", "cherry"]

for x in fruits:

print(x)

While

i = 1

while i < 6:

print(i)

i += 1

Break

i = 1

while i < 6:

print(i)

if i == 3:

break

i += 1

Continue

i = 1

while i < 6:

print(i)

if i == 3:

continue

i += 1

{% endtab %}

{% tab title="PowerShell" %}

Loop Type Code Examples
For

$fruits = @("apple", "banana", "cherry")

foreach($x in $fruits)

{

Write-Host $x

}

While

$i = 1

while ($i -lt 6)

{

Write-Host $i

$i++

}

Break

$i = 1

while ($i -lt 6)

{

Write-Host $i

if ($i -eq 3)

{

break

}

$i++

}

Continue

$i = 1

while ($i -lt 6)

{

Write-Host $i

if ($i -eq 3)

{

continue

}

$i++

}

{% endtab %}

{% tab title="Bash" %} TODO: this

Loop Type Code Examples
For
While
Break
Continue

Basic for loop

for i in /etc/rc.*; do
  echo $i
done

C-like for loop

for ((i = 0 ; i < 100 ; i++)); do
  echo $i
done

Ranges

for i in {1..5}; do
    echo "Welcome $i"
done

With step size

for i in {5..50..5}; do
    echo "Welcome $i"
done

Reading lines

cat file.txt | while read line; do
  echo $line
done

Forever

while true; do
  ···
done

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Loop Type Code Examples
For
While
Break
Continue

Loops

In the decision making chapter, we have seen statements which have been executed one after the other in a sequential manner. Additionally, implementations can also be done in Batch Script to alter the flow of control in a program’s logic. They are then classified into flow of control statements.

While Statement Implementation

There is no direct while statement available in Batch Script but we can do an implementation of this loop very easily by using the if statement and labels.

There is no direct while statement available in Batch Script but we can do an implementation of this loop very easily by using the if statement and labels.

The first part of the while implementation is to set the counters which will be used to control the evaluation of the ‘if’ condition. We then define our label which will be used to embody the entire code for the while loop implementation. The ‘if’ condition evaluates an expression. If the expression evaluates to true, the code block is executed. If the condition evaluates to false then the loop is exited. When the code block is executed, it will return back to the label statement for execution again.

Following is the syntax of the general implementation of the while statement.

Syntax

Set counters
:label
If (expression) (
   Do_something
   Increment counter
   Go back to :label
)
  • The entire code for the while implementation is placed inside of a label.
  • The counter variables must be set or initialized before the while loop implementation starts.
  • The expression for the while condition is done using the ‘if’ statement. If the expression evaluates to true then the relevant code inside the ‘if’ loop is executed.
  • A counter needs to be properly incremented inside of ‘if’ statement so that the while implementation can terminate at some point in time.
  • Finally, we will go back to our label so that we can evaluate our ‘if’ statement again.

Following is an example of a while loop statement.

Example

@echo off
SET /A "index = 1"
SET /A "count = 5"
:while
if %index% leq %count% (
   echo The value of index is %index%
   SET /A "index = index + 1"
   goto :while
)

In the above example, we are first initializing the value of an index integer variable to 1. Then our condition in the ‘if’ loop is that we are evaluating the condition of the expression to be that index should it be less than the value of the count variable. Till the value of index is less than 5, we will print the value of index and then increment the value of index.

For Statement - List Implementations

The "FOR" construct offers looping capabilities for batch files. Following is the common construct of the ‘for’ statement for working with a list of values.

Syntax

FOR %%variable IN list DO do_something

The classic ‘for’ statement consists of the following parts −

  • Variable declaration – This step is executed only once for the entire loop and used to declare any variables which will be used within the loop. In Batch Script, the variable declaration is done with the %% at the beginning of the variable name.
  • List – This will be the list of values for which the ‘for’ statement should be executed.
  • The do_something code block is what needs to be executed for each iteration for the list of values.

Following is an example of how the ‘goto’ statement can be used.

Example

@echo off 
FOR %%F IN (1 2 3 4 5) DO echo %%F

The key thing to note about the above program is −

  • The variable declaration is done with the %% sign at the beginning of the variable name.
  • The list of values is defined after the IN clause.
  • The do_something code is defined after the echo command. Thus for each value in the list, the echo command will be executed.

Looping through Ranges

The ‘for’ statement also has the ability to move through a range of values. Following is the general form of the statement.

Syntax

FOR /L %%variable IN (lowerlimit,Increment,Upperlimit) DO do_something

Where

  • The /L switch is used to denote that the loop is used for iterating through ranges.
  • Variable declaration – This step is executed only once for the entire loop and used to declare any variables which will be used within the loop. In Batch Script, the variable declaration is done with the %% at the beginning of the variable name.
  • The IN list contains of 3 values. The lowerlimit, the increment, and the upperlimit. So, the loop would start with the lowerlimit and move to the upperlimit value, iterating each time by the Increment value.
  • The do_something code block is what needs to be executed for each iteration.

Following is an example of how the looping through ranges can be carried out.

Example

@ECHO OFF 
FOR /L %%X IN (0,1,5) DO ECHO %%X

Classic for Loop Implementation

Following is the classic ‘for’ statement which is available in most programming languages.

Syntax

for(variable declaration;expression;Increment) {
   statement #1
   statement #2
   …
}

The Batch Script language does not have a direct ‘for’ statement which is similar to the above syntax, but one can still do an implementation of the classic ‘for’ loop statement using if statements and labels.

Let’s look at the general syntax implementation of the classic for loop in batch scripting.

Set counter
:label

If (expression) exit loop
Do_something
Increment counter
Go back to :label
  • The entire code for the ‘for’ implementation is placed inside of a label.
  • The counters variables must be set or initialized before the ‘for’ loop implementation starts.
  • The expression for the ‘for’ loop is done using the ‘if’ statement. If the expression evaluates to be true then an exit is executed to come out of the loop.
  • A counter needs to be properly incremented inside of the ‘if’ statement so that the ‘for’ implementation can continue if the expression evaluation is false.
  • Finally, we will go back to our label so that we can evaluate our ‘if’ statement again.

Following is an example of how to carry out the implementation of the classic ‘for’ loop statement.

Example

@echo off 
SET /A i = 1 
:loop 

IF %i%==5 GOTO END 
echo The value of i is %i% 
SET /a i=%i%+1 
GOTO :LOOP 
:END

Looping through Command Line Arguments

The ‘for’ statement can also be used for checking command line arguments. The following example shows how the ‘for’ statement can be used to loop through the command line arguments.

Example

@ECHO OFF 
:Loop 

IF "%1"=="" GOTO completed 
FOR %%F IN (%1) DO echo %%F 
SHIFT 
GOTO Loop 
:completed

Output

Let’s assume that our above code is stored in a file called Test.bat. The above command will produce the following output if the batch file passes the command line arguments of 1,2 and 3 as Test.bat 1 2 3.

1 
2 
3

Break Statement Implementation

The break statement is used to alter the flow of control inside loops within any programming language. The break statement is normally used in looping constructs and is used to cause immediate termination of the innermost enclosing loop.

The break statement is used to alter the flow of control inside loops within any programming language. The break statement is normally used in looping constructs and is used to cause immediate termination of the innermost enclosing loop.

The Batch Script language does not have a direct ‘for’ statement which does a break but this can be implemented by using labels. The following diagram shows the diagrammatic explanation of the break statement implementation in Batch Script.

Example

@echo off 
SET /A "index=1" 
SET /A "count=5" 
:while 
if %index% leq %count% ( 
   if %index%==2 goto :Increment 
      echo The value of index is %index% 
:Increment 
   SET /A "index=index + 1" 
   goto :while 
)

The key thing to note about the above implementation is the involvement of two ‘if’ conditions. The second ‘if’ condition is used to control when the break is implemented. If the second ‘if’ condition is evaluated to be true, then the code block is not executed and the counter is directly implemented.

Following is an example of how to carry out the implementation of the break statement.

The key thing to note about the above program is the addition of a label called :Increment. When the value of index reaches 2, we want to skip the statement which echoes its value to the command prompt and directly just increment the value of index. {% endtab %} {% endtabs %}

Functions

{% tabs %} {% tab title="Python" %}

Functions Code Examples
Definition

def hello_function():

print("Hello from my function!")

<code></code>

hello_function()

Arguments

def my_name(fname, lname):

print("My name is " + fname + " " + lname)

<code></code>

my_function("Wolf", "Zweiler")

Variable Arguments

def second_arg(*children):

print("The youngest child is " + children[1])

<code></code>

my_function("Sarah", "Emily", "Tom")

Named Arguments

def young_child(child3, child2, child1):

print("The youngest child is " + child3)

<code></code>

my_function(child1 = "Sarah", child2 = "Emily", child3 = "Tom")

Default Values

def my_country(country = "Wakanda"):

print("I am from " + country)

<code></code>

my_country()

Return Values

def five_times(x):

return 5 * x

{% endtab %}

{% tab title="PowerShell" %}

Functions Code Examples
Definition

function hello_function()

{

Write-Host "Hello from my function!"

}

hello_function

Arguments

function my_name($fname, $lname)

{

Write-Host "My name is $fname $lname"

}

my-function -fname "Wolf" -lname "Zweiler"

Variable Arguments

function second_arg()

{

Write-Host "The youngest child is $($args[1])"

}

my-function "Sarah" "Emily" "Tom"

Named Arguments

function young_child($child3, $child2, $child1)

{

Write-Host "The youngest child is $child3"

}

my-function -child1 "Sarah" -child2 "Emily" -child3 "Tom"

Default Values

function my_country

{

param(

$country = "Wakanda"

)

<code></code>

Write-Host "I am from $country"

}

my_country

Return Values

function five_times($x)

{

5 * $x

}

{% endtab %}

{% tab title="Bash" %} TODO: this

Functions Code Examples
Definition
Arguments
Variable Arguments
Named Arguments
Default Values
Return Values

Arguments

$# Number of arguments
$* All arguments
$@ All arguments, starting from first
$1 First argument
$_ Last argument of the previous command

Returning values

myfunc() {
    local myresult='some value'
    echo $myresult
}
result="$(myfunc)"

Defining Functions

myfunc() {
    echo "hello $1"
}
# Same as above (alternate syntax)
function myfunc() {
    echo "hello $1"
}
myfunc "John"

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Functions Code Examples
Definition
Arguments
Variable Arguments
Named Arguments
Default Values
Return Values
{% endtab %}
{% endtabs %}

Classes

{% tabs %} {% tab title="Python" %}

Activity Code Examples
Class Definition

class MyClass:

x = 5

Object Creation MyClass()
Using Class Constructors

class Person:

def init(self, name, age):

self.name = name

self.age = age

<code></code>

p1 = Person("Bob", 42)

Defining and using Methods

class Person:

def init(self, name, age):

self.name = name

self.age = age

<code></code>

def myfunc(self):

print("Hello my name is " + self.name)

<code></code>

p1 = Person("Bob", 42)

p1.myfunc()

{% endtab %}

{% tab title="PowerShell" %}

Activity Code Examples
Class Definition

class MyClass {

$x = 5

}

Object Creation [MyClass]::new()
Using Class Constructors

class Person {

Person($Name, $Age) {

$this.Name = $Name

$this.Age = $Age

}

<code></code>

$Name = ''

$Age = 0

}

[Person]::new('Bob', 42)

Defining and using Methods

class Person {

Person($Name, $Age) {

$this.Name = $Name

$this.Age = $Age

}

<code></code>

[string]myfunc() {

return "Hello my name is $($this.Name)"

}

<code></code>

$Name = ''

$Age = 0

}

<code></code>

[Person]::new('Bob', 42)

{% endtab %}

{% tab title="Bash" %}

Activity Code Examples
Class Definition
Object Creation
Using Class Constructors
Defining and using Methods
{% endtab %}

{% tab title="CMD .bat" %}

Activity Code Examples
Class Definition
Object Creation
Using Class Constructors
Defining and using Methods
{% endtab %}
{% endtabs %}

Comments

{% tabs %} {% tab title="Python" %}

Comment Type Code Examples
Single line # Hello, world!
Multiline

"""

Hello, world!

"""

{% endtab %}

{% tab title="PowerShell" %}

Comment Type Code Examples
Single line # Hello, world!
Multiline

<#

Hello, world!

#>

{% endtab %}

{% tab title="Bash" %} TODO: this

Comment Type Code Examples
Single line
Multiline

Comments

# Single line comment
: '
This is a
multi line
comment
'

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Comment Type Code Examples
Single line
Multiline

Comments Using the Rem Statement

There are two ways to create comments in Batch Script; one is via the Rem command. Any text which follows the Rem statement will be treated as comments and will not be executed. Following is the general syntax of this statement.

Syntax

Rem Remarks

where ‘Remarks’ is the comments which needs to be added.

The following example shows a simple way the Rem command can be used.

Example

@echo off 
Rem This program just displays Hello World 
set message=Hello World 
echo %message%

Output

The above command produces the following output. You will notice that the line with the Rem statement will not be executed.

Hello World

Comments Using the :: Statement

The other way to create comments in Batch Script is via the :: command. Any text which follows the :: statement will be treated as comments and will not be executed. Following is the general syntax of this statement.

Syntax

:: Remarks

where ‘Remarks’ is the comment which needs to be added.

The following example shows the usage of the "::" command.

Example

@echo off 
:: This program just displays Hello World 
set message = Hello World 
echo %message%

Output

The above command produces the following output. You will notice that the line with the :: statement will not be executed.

Hello World

Note − If you have too many lines of Rem, it could slow down the code, because in the end each line of code in the batch file still needs to be executed. {% endtab %} {% endtabs %}

Data Types

{% tabs %} {% tab title="Python" %}

Action Code Examples
Get Object's Type

var = 1

type(var)

{% endtab %}

{% tab title="PowerShell" %}

Action Code Examples
Get Object's Type

$var = 1

$var

{% endtab %}

{% tab title="Bash" %} TODO: this

Action Code Examples
Get Object's Type
{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Action Code Examples
Get Object's Type
{% endtab %}
{% endtabs %}

Dictionaries

{% tabs %} {% tab title="Python" %}

Activity Code Examples
Defining

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

print(thisdict)

Accessing Elements

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

thisdict['brand']

Updating Elements

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

thisdict['brand'] = 'Chevy'

Enumerating Keys

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

for x in thisdict:

print(x)

Enumerating Values

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

for x in thisdict.values():

print(x)

Check if key exists

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

if "model" in thisdict:

print("Yes, 'model' is one of the keys in the thisdict dictionary")

Adding items

thisdict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

thisdict["color"] = "red"

{% endtab %}

{% tab title="PowerShell" %}

Activity Code Examples
Defining

$thisdict = @{

brand = "Ford"

model = "Mustang"

year = 1964

}

Accessing Elements

$thisdict = @{

brand = "Ford"

model = "Mustang"

year = 1964

}

$thisdict.brand

<code></code>

or

<code></code>

$thisdict['brand']

Updating Elements

$thisdict = @{

brand = "Ford"

model = "Mustang"

year = 1964

}

$thisdict.brand = 'Chevy'

Enumerating Keys

$thisdict = @{

brand = "Ford"

model = "Mustang"

year = 1964

}

$thisdict.Keys

Enumerating Values

$thisdict = @{

brand = "Ford"

model = "Mustang"

year = 1964

}

$thisdict.Values

Check if key exists

$thisdict = @{

brand = "Ford"

model = "Mustang"

year = 1964

}

if ($thisdict.ContainsKey("model"))

{

Write-Host "Yes, 'model' is one of the keys in the thisdict dictionary"

}

Adding items

$thisdict = @{

brand = "Ford"

model = "Mustang"

year = 1964

}

$thisdict.color = 'red'

{% endtab %}

{% tab title="Bash" %} TODO: this

Activity Code Examples
Defining
Accessing Elements
Updating Elements
Enumerating Keys
Enumerating Values
Check if key exists
Adding items

Defining

declare -A sounds
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"

Declares sound as a Dictionary object (aka associative array).

Working with dictionaries

echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]}   # All values
echo ${!sounds[@]}  # All keys
echo ${#sounds[@]}  # Number of elements
unset sounds[dog]   # Delete dog

Iteration

Iterate over values

for val in "${sounds[@]}"; do
  echo $val
done

Iterate over keys

for key in "${!sounds[@]}"; do
  echo $key
done

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Activity Code Examples
Defining
Accessing Elements
Updating Elements
Enumerating Keys
Enumerating Values
Check if key exists
Adding items

Creating Structures in Arrays

Structures can also be implemented in batch files using a little bit of an extra coding for implementation. The following example shows how this can be achieved.

Example

@echo off 
set len = 3 
set obj[0].Name = Joe 
set obj[0].ID = 1 
set obj[1].Name = Mark 
set obj[1].ID = 2 
set obj[2].Name = Mohan 
set obj[2].ID = 3 
set i = 0 
:loop 

if %i% equ %len% goto :eof 
set cur.Name= 
set cur.ID=

for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do ( 
   set cur.%%k=%%l 
) 
echo Name = %cur.Name% 
echo Value = %cur.ID% 
set /a i = %i%+1 
goto loop

The following key things need to be noted about the above code.

  • Each variable defined using the set command has 2 values associated with each index of the array.
  • The variable i is set to 0 so that we can loop through the structure will the length of the array which is 3.
  • We always check for the condition on whether the value of i is equal to the value of len and if not, we loop through the code.
  • We are able to access each element of the structure using the obj[%i%] notation.

Output

The above command produces the following output.

Name = Joe 
Value = 1 
Name = Mark 
Value = 2 
Name = Mohan 
Value = 3

{% endtab %} {% endtabs %}

Lambdas

{% tabs %} {% tab title="Python" %}

Lambda Code Examples
Lambda

x = lambda a : a + 10

print(x(5))

{% endtab %}

{% tab title="PowerShell" %}

Lambda Code Examples
Lambda

$x = { param($a) $a + 10 }

& $x 5

{% endtab %}

{% tab title="Bash" %} TODO: this

Lambda Code Examples
Lambda
{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Lambda Code Examples
Lambda
{% endtab %}
{% endtabs %}

Math Operators

TODO: Add other operator types

{% tabs %} {% tab title="Python" %}

Operator Code Examples
Addition var = 1 + 1
Subtraction var = 1 - 1
Multiplication var = 1 * 1
Division var = 1 / 1
Modulus var = 1 % 1
Floor var = 10 // 3
Exponent var = 10 ** 3
{% endtab %}

{% tab title="PowerShell" %}

Operator Code Examples
Addition $var = 1 + 1
Subtraction $var = 1 - 1
Multiplication $var = 1 * 1
Division $var = 1 / 1
Modulus $var = 1 % 1
Floor $var = [Math]::Floor(10 / 3)
Exponent $var = [Math]::Pow(10, 3)
{% endtab %}

{% tab title="Bash" %} TODO: this

Operator Code Examples
Addition
Subtraction
Multiplication
Division
Modulus
Floor
Exponent
{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Operator Code Examples
Addition
Subtraction
Multiplication
Division
Modulus
Floor
Exponent

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

In batch script, the following types of operators are possible.

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Bitwise operators

Arithmetic Operators

Batch script language supports the normal Arithmetic operators as any language. Following are the Arithmetic operators available.

Show Example

Operator Description Example
+ Addition of two operands 1 + 2 will give 3
Subtracts second operand from the first 2 − 1 will give 1
* Multiplication of both operands 2 * 2 will give 4
/ Division of the numerator by the denominator 3 / 2 will give 1.5
% Modulus operator and remainder of after an integer/float division 3 % 2 will give 1

Relational Operators

Relational operators allow of the comparison of objects. Below are the relational operators available.

Show Example

Operator Description Example
EQU Tests the equality between two objects 2 EQU 2 will give true
NEQ Tests the difference between two objects 3 NEQ 2 will give true
LSS Checks to see if the left object is less than the right operand 2 LSS 3 will give true
LEQ Checks to see if the left object is less than or equal to the right operand 2 LEQ 3 will give true
GTR Checks to see if the left object is greater than the right operand 3 GTR 2 will give true
GEQ Checks to see if the left object is greater than or equal to the right operand 3 GEQ 2 will give true

Logical Operators

Logical operators are used to evaluate Boolean expressions. Following are the logical operators available.

The batch language is equipped with a full set of Boolean logic operators like AND, OR, XOR, but only for binary numbers. Neither are there any values for TRUE or FALSE. The only logical operator available for conditions is the NOT operator.

Show Example

Operator Description
AND This is the logical “and” operator
OR This is the logical “or” operator
NOT This is the logical “not” operator

Assignment Operators

Batch Script language also provides assignment operators. Following are the assignment operators available.

Show Example

Operator Description Example
+= This adds right operand to the left operand and assigns the result to left operand

Set /A a = 5

a += 3

Output will be 8

-= This subtracts the right operand from the left operand and assigns the result to the left operand

Set /A a = 5

a -= 3

Output will be 2

*= This multiplies the right operand with the left operand and assigns the result to the left operand

Set /A a = 5

a *= 3

Output will be 15

/= This divides the left operand with the right operand and assigns the result to the left operand

Set /A a = 6

a/ = 3

Output will be 2

%= This takes modulus using two operands and assigns the result to the left operand

Set /A a = 5

a% = 3

Output will be 2

Bitwise Operators

Bitwise operators are also possible in batch script. Following are the operators available.

Show Example

Operator Description
& This is the bitwise “and” operator
| This is the bitwise “or” operator
^ This is the bitwise “xor” or Exclusive or operator

Following is the truth table showcasing these operators.

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
{% endtab %}
{% endtabs %}

Error Handling

{% tabs %} {% tab title="Python" %}

Error Handling Code Examples
Try/Except

try:

print(x)

except:

print("An exception occurred")

Else

try:

print("Hello")

except:

print("Something went wrong")

else:

print("Nothing went wrong")

Finally

try:

f = open("file.txt") f.write("Lorum Ipsum")

except:

print("Something went wrong when writing to the file")

finally:

f.close()

Raise

x = -1

if x < 0:

raise Exception("Sorry, no numbers below zero")

{% endtab %}

{% tab title="PowerShell" %}

Error Handling Code Examples
Try/Catch

try {

Write-Host $x

} catch {

Write-Host "An exception ocurred"

}

{% endtab %}

{% tab title="Bash" %} TODO: this

Error Handling Code Examples
Try/Catch

Trap errors

trap 'echo Error at about $LINENO' ERR

or

traperr() {
  echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}

set -o errtrace
trap traperr ERR

Raising errors

myfunc() {
  return 1
}
if myfunc; then
  echo "success"
else
  echo "failure"
fi

{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Error Handling Code Examples
Try/Catch
{% endtab %}
{% endtabs %}

Shell Command Execution

{% tabs %} {% tab title="Python" %}

{% endtab %}

{% tab title="PowerShell" %} To execute regular Windows shell commands (from cmd.exe) in PowerShell, simply type the command the same way you would in the Windows command shell. Some commands may not work in the same way, and some may need the full filename (example: to se a directory listing in cmd.exe dir is the command. To use this in PowerShell you would need to specify dir.exe.

IEX (Invoke-Expression) {% endtab %}

{% tab title="Bash" %}

Shell execution

pwd
echo "I'm in $(pwd)"
echo "I'm in `pwd`"

{% endtab %}

{% tab title="CMD .bat" %}

{% endtab %} {% endtabs %}

Output Redirection

{% tabs %} {% tab title="Python" %}

{% endtab %}

{% tab title="PowerShell" %}

Redirect Standard Error to the nether

2>null

Many cmdlets also have an ErrorAction property

-ErrorAction Silent

{% endtab %}

{% tab title="Bash" %} Redirect Standard Error to the nether

2>/dev/null

{% endtab %}

{% tab title="CMD .bat" %} There are three universal “files” for keyboard input, printing text on the screen and printing errors on the screen. The “Standard In” file, known as stdin, contains the input to the program/script. The “Standard Out” file, known as stdout, is used to write output for display on the screen. Finally, the “Standard Err” file, known as stderr, contains any error messages for display on the screen.

Each of these three standard files, otherwise known as the standard streams, are referenced using the numbers 0, 1, and 2. Stdin is file 0, stdout is file 1, and stderr is file 2.

Redirecting Output (Stdout and Stderr)

One common practice in batch files is sending the output of a program to a log file. The > operator sends, or redirects, stdout or stderr to another file. The following example shows how this can be done.

Dir C:\ > list.txt

In the above example, the stdout of the command Dir C: is redirected to the file list.txt.

If you append the number 2 to the redirection filter, then it would redirect the stderr to the file lists.txt.

Dir C:\ 2> list.txt

One can even combine the stdout and stderr streams using the file number and the ‘&’ prefix. Following is an example.

DIR C:\ > lists.txt 2>&1

Suppressing Program Output

The pseudo file NUL is used to discard any output from a program. The following example shows that the output of the command DIR is discarded by sending the output to NUL.

Dir C:\ > NUL

Stdin

To work with the Stdin, you have to use a workaround to achieve this. This can be done by redirecting the command prompt’s own stdin, called CON.

The following example shows how you can redirect the output to a file called lists.txt. After you execute the below command, the command prompt will take all the input entered by user till it gets an EOF character. Later, it sends all the input to the file lists.txt.

TYPE CON > lists.txt

{% endtab %} {% endtabs %}

HERE docs

{% tabs %} {% tab title="Python" %}

{% endtab %}

{% tab title="PowerShell" %}

{% endtab %}

{% tab title="Bash" %}

Heredoc

cat <<HERE
hello world
HERE

{% endtab %}

{% tab title="CMD .bat" %}

{% endtab %} {% endtabs %}

Package Management

{% tabs %} {% tab title="Python" %}

Activity Code Examples
Install pip install requests
Import import requests
List pip list
{% endtab %}

{% tab title="PowerShell" %}

Activity Code Examples
Install Install-Module Pester
Import Import-Module Pester
List Get-Module -ListAvailable
{% endtab %}

{% tab title="Bash" %} TODO: this

Activity Code Examples
Install
Import
List
{% endtab %}

{% tab title="CMD .bat" %} TODO: this

Activity Code Examples
Install
Import
List
{% endtab %}
{% endtabs %}

References

If you like this content and would like to see more, please consider buying me a coffee!