130 likes | 145 Views
Explore shell programming exercises covering directory creation, file handling, functions, variables, regular expressions, and more. Dive into Linux system programming with Bash syntax and commands.
E N D
Project: Bash Shell System Programming I Alex Jamieson, Ezequiel Jackson, & Chris Haley Team BAmazon Apprentice Program Center for Info. Assurance Education (CIAE) (NSA/DHS CAE-CDE)
Agenda • Introduce team • Discuss scope of project • Exercises overview • What we learned
Alex Jamieson - Exercises • Chapter 1 Exercise 1 • Create a main directory and two subdirectories • /home/ubuntu/bpl • /home/ubuntu/bpl/bin • /home/ubuntu/bpl/scripts • Using the mkdir command • Chapter 3 Exercise 2 • Check whether a file exists • Use a while loop and nested if loop • Continues to prompt user until user input is a valid file
Alex Jamieson - Exercises • Chapter 4 Exercise 1 • Question: How many arguments are there on this command line? sa $# $(date "+%Y %m %d") John\ Doe • Answer: 3 • sa is a command and $# expands the number of arguments • $(date "+%Y %m %d"), John\, and Doe are arguments • Chapter 6 Exercise 1 • IFS=. vs ${1//./} • IFS (Internal Field Separator) - By default is the IFS is a space • Parameter Expansion - ${var//pattern/string}
Alex Jamieson - Exercises • Chapter 7 Exercise 3 • Created a function called palindrome • Retrieved user input • Ran function against user input
Ezequiel Jackson – Exercises •year=$( date +%Y ) •month=$( date +%m ) •day=$( date +%d ) •hour=$( date +%H ) •minute=$( date +%M ) •second=$( date +%S ) This would give you an incorrect date, and you are writing date 6 times when you only need to write it once. 5.2: For Single Variable names are reasonable: when used, within the space up to three lines. Index variables when iterating over a list e.g. I,n,k. Equations: var prices = [50, 20, 30]; var discount = .9; _.map(prices, function (p) {return p * discount;}); // -> [45, 18, 27]
Ezequiel Jackson – Exercises 1.2 & 7.2 exercises
Chris Haley – Exercises • Chapter 2 Exercise 2 • Generate numbers using $RANDOM • Output numbers to a file and a variable • Chapter 3 Exercise 1 • Question: Enter a number between 20 and 30 and ask again if invalid • Until loop with nested if statements
Chris Haley – Exercises • Chapter 5 Exercise 3 • Extract 168 using parameter expansion -- var=192.168.0.123 • echo ${var:4:3}
Chris Haley - Exercises • Chapter 6 Exercise 2 • Add ability to verify a variable is a valid name to max3() • Chapter 7 Exercise 4 • Write two functions: ltrim & rtrim • Regular expresions • [[ "$ltrim" =~ ^' '*([characters])(.*) ]] • [[ "$rtrim" =~ ^(' '*)(.*)([a-z,A-Z,0-9])' '*$ ]]
What we learned • Regular expressions • Linux system programming • Bash syntax and commands
Key references • Johnson, C. F. A. & Varma, J. (2015). Pro Bash Programming: Scripting the GNU/Linux Shell Second Edition. Apress. (9781484201220)