Changes between Version 13 and Version 14 of Csle2022/Agenda/scriptingandgithub
- Timestamp:
- Nov 12, 2022, 8:07:16 PM (2 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Csle2022/Agenda/scriptingandgithub
v13 v14 1 1 = Hands-On = 2 == Linux programming (scripting) == 3 4 Shell Scripting is the language of the linux terminal. Shell scripts are sometimes referred to as “shebang” which is derived from the “#!” notation. Shell scripts are executed by interpreters present in the linux kernel. Interpreters include: bash, csh, zsh e.t.c. Most popular of which is bash. 2 = Linux programming (scripting) = 3 4 Shell Scripting is the language of the linux terminal, sometimes referred to as “shebang” which is derived from the “#!” notation. 5 6 Shell scripts are executed by interpreters present in the linux kernel. 5 7 Typical operations performed by shell scripts include file manipulation, program execution, and printing text. 6 8 … … 17 19 Let's start to have some experience on scripting 18 20 19 == === Simple Script for newbies =====21 == Simple Script for newbies == 20 22 21 23 First we have to create a text file … … 47 49 Make yourself familiar with `man man` and `man apropos` on the shell. It will need for your self-tutoring. 48 50 49 == == Let's make this little advanced ====50 51 == === How to define variables =====51 == Let's make this little advanced == 52 53 == How to define variables === 52 54 53 55 Variables can be defined using the syntax ` variable_name=value`. To get the value of the variable, add `$` before the variable … … 60 62 }}} 61 63 62 === == Arithmetic Expressions =====64 === Arithmetic Expressions === 63 65 64 66 These are the operstors supported by bash for mathematical calculations. … … 84 86 }}} 85 87 86 === == How to read user input =====88 === How to read user input === 87 89 88 90 Sometimes user input are needed to perform relevant operations. … … 96 98 `read -p "Enter your age" variable_name` 97 99 98 === == Numeric Comparison logical operators =====100 === Numeric Comparison logical operators === 99 101 100 102 Comparison is used to check if statements evaluate to true or false. … … 118 120 }}} 119 121 120 === == Conditional Statements (Decision Making) =====122 === Conditional Statements (Decision Making) === 121 123 122 124 Conditions are expressions that evaluate to a boolean expression (true or false). To check conditions, we can use `if`,` if-else`,` if-elif-else` and nested conditionals. 123 125 124 ===== Looping and skipping ===== 126 Conditional Statements: 127 128 There are total 5 conditional statements which can be used in bash programming 129 130 - if statement 131 - if-else statement 132 - if..elif..else..fi statement (Else If ladder) 133 - if..then..else..if..then..fi..fi..(Nested if) 134 - switch statement 135 136 ==== if statement ==== 137 This block will process if specified condition is true. 138 139 Syntax: 140 {{{ 141 if [ expression ] 142 then 143 statement 144 fi 145 }}} 146 147 ==== if-else statement ==== 148 If specified condition is not true in if part then else part will be execute. 149 150 Syntax 151 152 {{{ 153 if [ expression ] 154 then 155 statement1 156 else 157 statement2 158 fi 159 }}} 160 161 === if..elif..else..fi statement (Else If ladder) ==== 162 163 To use multiple conditions in one if-else block, then elif keyword is used in shell. 164 165 {{{ 166 if [ expression1 ] 167 then 168 statement1 169 statement2 170 . 171 . 172 elif [ expression2 ] 173 then 174 statement3 175 statement4 176 . 177 . 178 else 179 statement5 180 fi 181 }}} 182 183 ==== if..then..else..if..then..fi..fi..(Nested if) ==== 184 185 Nested if-else block can be used when, one condition is satisfies then it again checks another condition. 186 187 Syntax: 188 189 {{{ 190 if [ expression1 ] 191 then 192 statement1 193 statement2 194 . 195 else 196 if [ expression2 ] 197 then 198 statement3 199 . 200 fi 201 fi 202 }}} 203 204 ==== switch statement ==== 205 206 case statement works as a switch statement if specified value match with the pattern then it will execute a block of that particular pattern 207 When a match is found all of the associated statements until the double semicolon (;;) is executed. 208 A case will be terminated when the last command is executed. 209 If there is no match, the exit status of the case is zero. 210 211 Syntax: 212 {{{ 213 case in 214 Pattern 1) Statement 1;; 215 Pattern n) Statement n;; 216 esac 217 218 }}} 219 220 === Looping and skipping === 125 221 126 222 For loops allow you to execute statements a specific number of times. … … 148 244 }}} 149 245 150 === === While loop ======246 === While loop === 151 247 152 248 While loops check for a condition and loop until the condition remains true. We need to provide a counter statement that increments the counter to control loop execution. … … 177 273 }}} 178 274 179 === == How to execute commands with back ticks =====275 === How to execute commands with back ticks === 180 276 181 277 If you need to include the output of a complex command in your script, you can write the statement inside back ticks. … … 196 292 }}} 197 293 198 === == How to Automate Scripts by Scheduling via cron Jobs =====294 === How to Automate Scripts by Scheduling via cron Jobs === 199 295 200 296 Cron is a job scheduling utility present in Unix like systems. Jobs can be scheduled to execute daily, weekly, monthly or in a specific time of the day. Automation in Linux heavily relies on cron jobs.