Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

CIS 225

Lecture 7: Scripting and Automation 1

Building Shell Scripts

Open a new file using a text editor

$ nano script.sh


Add the following header at the top

#!/bin/bash

Building Shell Scripts

Input commands below (1 per line)

cd ~/bin
ls -al


Save the file (CTRL + X) and run

$ chmod u+x ./script.sh
$ ./script.sh

Shell Hello World

#!/bin/bash

echo "Hello World"
exit 0

Shell Hello World

echo "Hello World"

echo prints text

exit 0

exit stops the command

Simple Script

#!/bin/bash
#batman.sh

if (( $# < 1 || $# > 1 )); then
  echo "Usage: batman.sh <name>"
  exit 1
fi

if [ "$1" = "Batman" ]; then
  echo "Hello Mr. Wayne"
elif [ "$1" = "Robin" ]; then
  echo "Welcome back Mr. Grayson"
else
  echo "Intruder alert!"
fi
exit 0

Shell Script Parameters

$1 - First Parameter
$2 - Second Parameter
...
${10} - Tenth Parameter
${11} - Eleventh Parameter

$# - Number of Parameters
$* - All Parameters (as String)

Conditionals

if (( $# < 1 || $# > 1 )); then

Douple parens for arithmetic evaluation

if [ "$1" = "Batman" ]; then

Square brackets for logical test
(using Linux 'test' command)

Loop Script

#!/bin/bash
#listfiles.sh

files=`ls $* 2> /dev/null`
for afile in "$files"; do 
    echo "$afile"
done
exit 0

Loops

files=`ls $* 2> /dev/null`

Declares a new variable $files
Backticks (`) get output of command

for afile in "$files"; do

Perfom commands below once for each item in $files

Environment Variables

Stores information about the system that can be used in scripts

$ printenv

Prints the current environment variables

PATH Variable

Tells your system where to look for binary files and scripts

$ export PATH=$PATH:$HOME/bin

Add a folder to the path
(Add this line to ~/.bashrc to make it permanent)

Building PowerShell Scripts

Open the PowerShell ISE (search for it on the Start menu)
You can also use a text editor such as Notepad
To be able to run them, open an Administrative Powershell and enter "Set-ExecutionPolicy Unrestricted"

PowerShell Scripts

Write-Host "Hello World"
return

PowerShell Parameters

Param(
  [string]$computerName,
  [string]$userName
)

Simple Script

Param(
  [string]$user
)
if ( -not ($user)){
  Write-Host "Usage: batman.ps1 <name>"
  return
}
if ($user.CompareTo("Batman") -eq 0){
  Write-Host "Hello Mr. Wayne"
}else{ 
    if ($user.CompareTo("Robin") -eq 0){
        Write-Host "Welcome back Mr. Grayson"
    }else{
      Write-Host "Intruder alert!"
    }
}
return

Loop Script

param(
  [string]$path
)

$files = Get-ChildItem $path

foreach($file in $files){
    Write-Host $file.name
}

Environment Variables

Control Panel > System and Security > System > Advanced System Settings > Environment Variables


Add a new variable called PATH to the user variables and give it the value "C:\Users\cis225\bin"

Reading

Assignments