Lectures 4 & 5 Git recap + Shell Scripting · Bash is a scripting language – Each line is...
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/1.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/2.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/4.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/9.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/10.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/12.jpg)
` ` 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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/15.jpg)
![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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/16.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022041101/5eda850afebf237c0c3b6ee2/html5/thumbnails/17.jpg)
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.