Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Presentation - Bash Scripting - Monday, Week 11

Presentation given at EASV, in SDM on Bash Scripting (monday, week 11 - 2015).
by

Anders Skarby

on 9 March 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Presentation - Bash Scripting - Monday, Week 11

Bash Scripting
Today's Plan
Learn about Bash Scripting
The basic constructs
Do some scripting exercises
Re-iterating
What is BASH?
BASH Allows Scripting
Awesome, cause that means we can truly automate things, due to:
Support for control structures
Support for loops
Support for functions
etc.
BASH Script Example
#!/bin/bash
counter=0
while [[ $counter -lt 10 ]; do
echo "The counter is $counter"
counter=counter+1
done
Some Basics
Every executable script must start with two specific bytes:

#!

Also known as she bang

Followed by the absolute path to the interpreter, which in case on BASH will be:

#!/bin/bash
Variables
Are simple to assign:

counter=42

And are referenced by prefixing with a $ (dollar sign), eg.:

echo "The counter now has the value: $counter"

There are no naming conventions, but since environment variables are written in all-caps, try with all lower-case.
If Statements
Act as expected, but their syntax are different:

if [[ some condition ]]
then
do stuff
fi

or shorter:

if [[ some condition ]]; then
do stuff
fi
Loops
There are a bunch of different loop constructs in BASH scripting, common for those are that they work on strings (or string output):

The one's will look at are:
The
for-in
loop
The
while
loop
The
until
loop
The
c-like for
loop
For-in looop
while Loop
Resembles the syntax of the if-statement:

while [[ some condition ]]
do
echo "The condition is true, ergo - I spam the console"
done

or shorter:

while [[ some condition ]]; do
echo "The condition is true, ergo - I spam the console"
done
until Loop
c-like for Loop
Consisting of 2 expressions and a condition:

for (( initialize expression; run condition; post iterate expression ))
do
echo "C-like for loop running"
done

and an example, with shorter syntax:

for ((i=0; i<10; i++)); do
echo "C like for loop running - iteration ${i + 1}"
done
Use for looping on "an array":

for item in "this is a test"
do
echo $item
done

or shorter:

for item in "this is a test"; do
echo $item
done
Like the while loop, this will however loop until a condition is met:

until [[ some condition ]]
do
echo "The condition is false, ergo - I spam the console"
done

or shorter:

until [[ some condition ]]; do
echo "The condition is false, ergo - I spam the console"
done

Basic conditions
All should be inclosed in double square brackets, eg. [[ some condition ]]

-eq
-ne
-lt
-le
-gt
-ge

Where variables are: a=1 and b=2

I.e. these are all arithmetic comparators
More conditions
==
!=
<
>
-a
or
-e
-f
-d

Eg.:
[[ "Something" == "Something different" ]]
All conditions
Don't try to remember them all, look them up instead:

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
Functions
... are also available in BASH scripting, but they are more limited than you would expect (and hope for).

They cannot return anything, other than a string (which is a hack). Except from an exit code (which is common for functions and linux executables as well).

They do take parameters, but there's are only referenced by position, and cannot be named.
Example of a function taking input
function be_polite() {
echo "Dear sir, please $1"
}

$1
is the first parameter taken as argument.

The function can be executed as:

be_polite "don't defecate in the auditorium"

Which will output:

"Dear sir, please don't defecate in the auditorium"
Workaround to name parameters
Assign parameters to (local) variables:

function print_profile() {
local name=$1
local age=$2
local email=$3
echo "Hello, my name is $name"
echo "I'm $age years old"
echo "...and my e-mail address is: $email"
}

Notice the
local
keyword
Variable number of parameters
Referenced by the
$@
variable
@
is short for everything.

Simpler to do than you would expect:

for var in "$@"; do
echo "$var"
done


Capturing output from functions
Since functions can return exit codes, we need a workaround for that.

So, we can capture output of a function (or a program) and store the output in a variable:

stored_output=$(ls -lha)

So, with a function:

function printGreeting() {
echo $name
}
myGreeting=$(printGreeting "Anders")
Reading User Input
One simple command:

read variable

or with more flexibility:

read -p "Enter the path to the file: " filepath

Read more information on using read using the man-page.
Reading Script Input
We already know how to do this, remember how to read input to a function, it's the same thing with reading arguments passed to a script.

./myScript.sh arg1 arg2 arg3

Can be read as:

$1
for arg1,
$2
for arg2
$3
for arg3
etc.
Arrays
Can also be constructed, however their syntax is again "different":

myArray=(first second third)
myArr[4]=fourth

Iterating them can be done as follows:

for var in "${myArray[@]}"; do
echo "${var}"
done


equal to, eg:
[[ $a -eq $b ]]
not equal to, eg:
[[ $a -ne $b ]]
lesser than, eg:
[[ $a -lt $b ]]
lesser than or equal to, eg:
[[ $a -le $b ]]
greater than, eg:
[[ $a -gt $b ]]
greater than or equal to, eg:
[[ $a -ge $b ]]
If two strings are equal
If two strings are not equal
If the first string sorts before the second string
If the first string sorts after the second string
If a file (of any type) exists
If a file exists and is a regular file
If a file exists and is a directory
All expressions can be negated with a ! (bang)
Assignment #1
Create the game "Hangman" in BASH scripting

Assignment #2 (for the brave)
Re-invent the game "Hangman" but instead of guessing words, guess a path to a file on the file system.
Still has to be implemented in BASH scripting
Rules / Hints may have to be re-invented

Assignment
Create the Game "Hangman"
The Rules for Hangman can be found here:
http://www.wikihow.com/Play-Hangman

HINT: If you need to generate a random number, use:
$((RANDOM%10))
Full transcript