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

Post on 31-May-2020

10 views 0 download

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

Lectures 4 & 5

GIT recap + Shell Scripting

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.

Shell Scripting

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

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!

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.

Programming features

– Conditional statements

– Flow control while/for loops

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

` ` quotes execute the contained command

Functions

Functions can be sub divided from your main code

Helps with debugging

Often functions are written for you

Functions as separate files

Sub dividing a difficult problemHelps both coding and your thinking process

Example of propositional parameters

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.