Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is...

17
Lectures 4 & 5 GIT recap + Shell Scripting

Transcript of Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is...

Page 1: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Lectures 4 & 5

GIT recap + Shell Scripting

Page 2: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

GIT exercise

Submission of the assignment is via gitlab.com

so a few quick tests before we move on with

shell scripting.

1) Make a new project on gitlab.com

2) Clone the project to your local computer

3) Follow the “edit add files commit

push” cycle to make changes to your project

4) Try and branch, edit and merge changes to

your repository.

Page 3: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Shell Scripting

Page 4: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed
Page 5: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Shell variables

Set variables with var=xRetrieve variables with $var

There are local shell variables and global environment variables:● Local variables are only defined within the current shell

● Environment variables are available to all programs/commands via the shell

● printenv lists all environment variablese.g. echo $PATH (command search path)echo $SHELL (name of default shell)echo $HOME (your home directory)

● Create new environment variables: export EDITOR=/usr/bin/emacs

Page 6: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Bash is a scripting language

– Each line is interpreted sequentially

– Only interpreted when the script is executed

– Until executed a script is simply a text file

– To run the file needs to be given the execute permission

– For reluctant Linux adopters, many windows editors put hidden charterers at the end of each line in many editors. Do not write your scripts in windows!

Page 7: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

A simple shell script

Edit text file

Make file executable

Run the script

Content of file hello.sh

No difference between writing a script and using the command line.A shell script can be written line by line to the command line.

Page 8: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Programming features

– Conditional statements

– Flow control while/for loops

Page 9: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed
Page 10: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed
Page 11: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Conditional statement example

If variable LD_LIBRARY_PATH is zero in size

“;” new line character

A script to append the LD_LIBRARY_PATH environmental variableLD_LIBRARY_PATH is a list of directories searched when a program is looking for a library

Hashbang

Page 12: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

` ` quotes execute the contained command

Page 13: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Functions

Functions can be sub divided from your main code

Helps with debugging

Often functions are written for you

Page 14: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Functions as separate files

Sub dividing a difficult problemHelps both coding and your thinking process

Page 15: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed
Page 16: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Example of propositional parameters

Page 17: Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is interpreted sequentially – Only interpreted when the script is executed – Until executed

Exercise

Write a script that takes exactly one argument, a

directory name. If the number of arguments is

more or less than one, print a usage message. If

the argument is not a directory, print an error

message. For the given directory, print the five

largest files and the five files that were most

recently modified.