Changes between Version 13 and Version 14 of Csle2022/Agenda/scriptingandgithub


Ignore:
Timestamp:
Nov 12, 2022, 8:07:16 PM (2 years ago)
Author:
deepthi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Csle2022/Agenda/scriptingandgithub

    v13 v14  
    11= 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
     4Shell Scripting is the language of the linux terminal, sometimes referred to as “shebang” which is derived from the “#!” notation.
     5
     6Shell scripts are executed by interpreters present in the linux kernel.
    57 Typical operations performed by shell scripts include file manipulation, program execution, and printing text.
    68
     
    1719Let's start to have some experience on scripting
    1820
    19 ===== Simple Script for newbies =====
     21== Simple Script for newbies ==
    2022
    2123First we have to create a text file
     
    4749Make yourself familiar with `man man` and `man apropos` on the shell. It will need for your self-tutoring.
    4850
    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 ===
    5254
    5355Variables can be defined using the syntax ` variable_name=value`. To get the value of the variable, add `$` before the variable
     
    6062}}}
    6163
    62 ===== Arithmetic Expressions =====
     64=== Arithmetic Expressions ===
    6365
    6466These are the operstors supported by bash for mathematical calculations.
     
    8486}}}
    8587
    86 ===== How to read user input =====
     88=== How to read user input ===
    8789
    8890Sometimes user input are needed to perform relevant operations.
     
    9698`read -p "Enter your age" variable_name`
    9799
    98 ===== Numeric Comparison logical operators =====
     100=== Numeric Comparison logical operators ===
    99101
    100102Comparison is used to check if statements evaluate to true or false.
     
    118120}}}
    119121
    120 ===== Conditional Statements (Decision Making) =====
     122=== Conditional Statements (Decision Making) ===
    121123
    122124Conditions 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.
    123125
    124 ===== Looping and skipping =====
     126Conditional Statements:
     127
     128There 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 ====
     137This block will process if specified condition is true.
     138
     139Syntax:
     140{{{
     141if [ expression ]
     142then
     143   statement
     144fi
     145}}}
     146
     147==== if-else statement ====
     148If specified condition is not true in if part then else part will be execute.
     149
     150Syntax
     151
     152{{{
     153if [ expression ]
     154then
     155   statement1
     156else
     157   statement2
     158fi
     159}}}
     160
     161=== if..elif..else..fi statement (Else If ladder) ====
     162
     163To use multiple conditions in one if-else block, then elif keyword is used in shell.
     164
     165{{{
     166if [ expression1 ]
     167then
     168   statement1
     169   statement2
     170   .
     171   .
     172elif [ expression2 ]
     173then
     174   statement3
     175   statement4
     176   .
     177   .
     178else
     179   statement5
     180fi
     181}}}
     182
     183==== if..then..else..if..then..fi..fi..(Nested if) ====
     184
     185Nested if-else block can be used when, one condition is satisfies then it again checks another condition.
     186
     187Syntax:
     188
     189{{{
     190if [ expression1 ]
     191then
     192   statement1
     193   statement2
     194   .
     195else
     196   if [ expression2 ]
     197   then
     198      statement3
     199      .
     200   fi
     201fi
     202}}}
     203
     204==== switch statement ====
     205
     206case statement works as a switch statement if specified value match with the pattern then it will execute a block of that particular pattern
     207When a match is found all of the associated statements until the double semicolon (;;) is executed.
     208A case will be terminated when the last command is executed.
     209If there is no match, the exit status of the case is zero.
     210
     211Syntax:
     212{{{
     213case  in
     214   Pattern 1) Statement 1;;
     215   Pattern n) Statement n;;
     216esac
     217
     218}}}
     219
     220=== Looping and skipping ===
    125221
    126222For loops allow you to execute statements a specific number of times.
     
    148244}}}
    149245
    150 ====== While loop ======
     246=== While loop ===
    151247
    152248While 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.
     
    177273}}}
    178274
    179 ===== How to execute commands with back ticks =====
     275=== How to execute commands with back ticks ===
    180276
    181277If you need to include the output of a complex command in your script, you can write the statement inside back ticks.
     
    196292}}}
    197293
    198 ===== How to Automate Scripts by Scheduling via cron Jobs =====
     294=== How to Automate Scripts by Scheduling via cron Jobs ===
    199295
    200296Cron 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.