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

Command Line Interfaces & Simple Scripts

Linux Commands

Linux Commands 2

Linux File Permissions

PowerShell Commands

PowerShell Commands 2

PowerShell Commands 3

File Output

Special Directories

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

Double 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"

Review

Select Statement

#!/bin/bash
#simplemenu.sh
OPTIONS="Build Run Clean Quit"
select opt in $OPTIONS; do
  if [ "$opt" = "Build" ]; then
    echo "Building project..."
  elif [ "$opt" = "Run" ]; then
    echo "Running project..."
  elif [ "$opt" = "Clean" ]; then
    echo "Cleaning project..."
  elif [ "$opt" = "Quit" ]; then
    echo "Exiting..."
    break
  else
    echo "Invalid Input"
  fi
done
exit 0

Select Statement

User Input

#!/bin/bash
#simpleinput.sh
echo "Input your name and press [ENTER]"
read name
echo "Welcome $name!"
exit 0

User Input

Scheduling Tasks - Cron

Cron allows you to schedule tasks to run a specific times

Very useful for tasks that need done regularly

Cron HowTo

$ crontab -e

Edit the current cron schedule

$ crontab -l

View the current cron schedule

$ sudo crontab -e

Edit the root cron schedule

Crontab

Crontab Timing

PowerShell Menu

$title = "Select Options"
$message = "Choose an option to perform"
$build = New-Object `
  System.Management.Automation.Host.ChoiceDescription `
  "&Build", "Build the project"
$run = New-Object `
  System.Management.Automation.Host.ChoiceDescription `
  "&Run", "Run the project"
$clean = New-Object `
  System.Management.Automation.Host.ChoiceDescription `
  "&Clean", "Clean the project"
$quit = New-Object `
  System.Management.Automation.Host.ChoiceDescription `
  "&Quit", "Quit"
$options = `
  [System.Management.Automation.Host.ChoiceDescription[]] `
  ($build, $run, $clean, $quit)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
    0 {"You selected Build"}
    1 {"You selected Run"}
    2 {"You selected Clean"}
    3 {"You selected Quit"}
}

PowerShell Menu

PowerShell Input

$name = Read-Host `
    "Input your name and press [ENTER]"
Write-Host "Welcome $name!"

PowerShell Input

Windows Task Scheduler

Windows Create Task

Windows Create Task

Windows Create Task

Reading

See Class Website:

http://people.cis.ksu.edu/~russfeld/cis225fall2015/

Assignments