====================== What is scripting ? ====================== => Scripting means set of commands we are keeping in a file for execution. => Scripting is used to automate our daily routine work. => For example, i want to execute below commands on daily basis whoami pwd date cal ls -l Note: instead of executing these commands one after other manually we can keep them inside a file and we can execute that file which is called as Scripting. => The process of executing script file using shell is called as shell scripting. => Shell scripting is used to automate our daily routine works in the project. a) take backup b) delete temp files c) analyze log files d) system health check => shell script files will have .sh extension Ex : backup.sh, health-checks.sh, log-analyzer.sh => Shell Script file will start with sha-bang. ============================ what is sha-bang in linux ? ============================ => sha-bang is used to specify which shell we should use to process our script file. Syntax : #! /bin/bash Note: Writing sha-bang is not mandatory but recommended. ============= 01 : Shell Script =========== #! /bin/bash echo "@@@@ script start @@@@" whoami pwd date cal echo "@@@@ script end @@@@" ============= 02 : Shell Script =========== #! /bin/bash echo " Enter Your Name " read FNAME echo "Good Evening, $FNAME" ============= 03 : Shell Script =========== #! /bin/bash echo "Enter your first name" read FNAME echo "Enter your last name" read LNAME echo "Your Fullname : $FNAME $LNAME" ==================== Scripting Concepts ==================== 1) Variables 2) Operators 3) Conditional Statements (if-else) 4) Looping Statements 5) Command Line Args 6) Functions =========== Variables =========== => Variables are used to store the values => Variables will represent data in key-value format a=20 b=30 name=ashok gender=male age=30 Note: We don't have data types in shell scripting. => We have 2 types of variables 1) System Variables / Environment Variables 2) User Defind Variables Note: We can access all the environment variables using below command $ env => The variables which are already defined and using by our system are called as System variables. => The variables which we are creating based on our requirement are called as 'User Defined Variables'. name = ashok id = 101 age = 25 gender = male Note : To access value of variable we will use below syntax $ echo $VARIABLE_NAME # create variable using terminal export course=devops # get variable value echo $course # remove variable unset variable_name Note: If we use export command in terminal for setting variables then those variables will be removed once we close our terminal. These are called as temporary variables. ==================================== How to set variables permanently ? ==================================== => We will use ".bashrc" file to set variables permanently for the user. $ ls -la $ cat -n .bashrc # open .bashrc vi .bashrc # add variables at end of the file COURSE=devops TRAINER=ashok # apply .bashrc changes source .bashrc # Access variables echo $COURSE echo $TRAINER Note: In linux machine, every user will contain their own .bashrc file in user home directory. ========================== Rules for Variables name ========================== => Variable name should not start with digit Ex: 123name (invalid) name1 (valid) => Variable name should not contain below 3 special symbols Ex: - (hyper), @, # Note: It is recommended to use uppercase characters for variable names. name1 ==> NAME1 age ==> AGE ============ Operators ============ => Operators are used to perform some operation on the variables. 10 + 20 ==> 30 10 > 20 ==> false 25 == 25 ==> true ======================== Arithematic Operators ======================== Addition : + Substraction : - Multiplication : * Division : / Modulas : % Syntax to perform Arithematic Operations : $((VAR1 + VAR2)) ================== 04 : Script (addition) ================ #! /bin/bash echo "Enter First Number" read FNUM echo "Enter Second Number" read SNUM echo "Result : $((FNUM+SNUM))" =================================== Relational/Comparision Operators =================================== Equals (== (or) eq) Not Equals (!=) Greater Than (> (or) gt (or) ge) Less Than :: < (or) lt (or) le =================== Conditional Stmts =================== => Conditional statements are used to execute commands based on condition. Ex : read AGE if age is above 18 years then print msg as "eligible for vote" if age is below 18 years then print msg as "not-elgible for vote" => To implement conditional stmts we will use "if-elif-else" Syntax : if [ condition-1 ]; then // stmts elif [ condition-2 ]; then //stmts else // stmts ================== 05 : Script ( if-else) ================ #! /bin/bash echo "Enter age" read AGE if [ $AGE -ge 18 ]; then echo "Eligible For Vote" else echo "Not Eligible for Vote" fi ==================================================================================== Requirement : Take a number from user and check given number is even or odd Requirement : Take a number from user and check weather it is positive or negative or zero ==================================================================================== #! /bin/bash echo "Enter number" read NUM if (( $NUM % 2 == 0 ));then echo "$NUM is EVEN NUM" else echo "$NUM is ODD NUM" fi =================================================================================== #! /bin/bash echo "Enter Number" read A if [ $A -gt 0 ]; then echo "positive num" elif [ $A -lt 0 ]; then echo "negative num" else echo "It is zero" fi ================================================================================== echo "good evening" ===> execute it for 10 times (10 is the range) ==================== Looping Statements ==================== => Loops are used to execute statements multiple times. => In scripting we can use 2 types of loops 1) Range based loop (Ex: for) 2) Conditional based loop (Ex: while) ------------------ for loop syntax : ------------------ for(( initialization; condition; modification )) do //stmts done ================================================ For loop example - Print Numbers from 1 to 10 ================================================ #! /bin/bash for((i=1; i<=10; i++)) do echo $i done ================================================ For loop example - Print Numbers from 10 to 1 ================================================ #! /bin/bash for ((i=10; i>=1; i--)) do echo $i done ============= While Loop ============= => While loop is used to execute statements until condition is true. syntax : while [ condition ] do //stmts done ------------------------------------------ print nums from 1 to 10 using while loop ------------------------------------------ #! /bin/bash N=1 while [ $N -le 10 ] do echo $N let N++ done ------------------------------------------ print nums from 10 to 1 using while loop ------------------------------------------ #! /bin/bash N=10 while [ $N -ge 1] do echo $N let N-- done ======================== Command Line Arguments ======================== => cmd args are used to supply values to script file at the time of script execution. ex : sh while-demo.sh 30 20 10 => We can read cmd args in script like below $# : Total no.of cmd args $1 : Read first cmd arg $2 : Read second cmd arg $3 : Read third cmd arg $* : Read all cmd args ------------------------------------- #! /bin/bash echo "Total Args : $#" echo "======================" echo "First Arg : $1" echo "Second Arg : $2" echo "======================" echo "All args : $*" ----------- sh ashok it ------- ============================================================================= Requirement: Write shell script to perform sum of two numbers using cmd args ============================================================================= #! /bin/bash echo "Result : $(($1+$2))" Run : sh 10 20 ========================= Assignments ===================================== 1) Write a shell script to check given number is prime or not 2) Write a shell script to check given text is palindrome or not Ex: madam, liril, ashok 3) Write a shell script to take backup of current working directory 4) Write a shell script to check with given name file is available or not, if not available then create that file. ====================== Functions / Methods ====================== => Functions are used to perform some action / task. => Using Functions we can divide big task into multiple small tasks => Using functions we can divide our work logically => Functions are re-usable (write once and call many times) syntax : # create a function function welcome(){ // body } # calling function welcome ====================== shell script with Function =============== #! /bin/bash function welcome(){ echo "Welcome to ashokit" echo "welcome to devops" echo "welcome to aws" } welcome ================================================================ function backup(){ // body } function deleteTempFiles(){ // body } function sysHealthChecks(){ // body } backup deleteTempFiles sysHealthChecks ================================================================== ==================== What is Scheduling ==================== shell script file : system-health-check.sh Requriement :: Everyday @9:00 AM we have to run above shell script file. => Instead of running that file manually, we can use scheduling. => Scheduling means configuring the tasks to be executed automatically. => In linux, we will use CRON to schedule jobs/scripts execution. => CRON is an utility in linux to schedule jobs execution. => In real-time we will use several jobs on daily/weekly/monthly/yearly basis to automate our work. - Delete Temp files - Take backup of files - System health checks ================ CRON JOB Syntax ================ Syntax : * * * * * => First * will represent minutes (0-59) => Second * will represent hour (0-23) => Third * will represent day of month (1-31) -> Fourth * will represent month of year (1-12) => Fifth * will represent day of week (0-6 / sun-sat) ======================= Sample CRON Schedules ======================= # Run for every 15 minutes */15 * * * * /path/to/script.sh # Run everyday @5:00 AM (IST) 0 5 * * * /path/to/script.sh # Run everyday @5:00 PM (IST) 0 17 * * * /path/to/script.sh # Run every month on first day @9:00 AM 0 9 1 * * /path/to/script.sh # Run Jan 1st day @ 11 AM 0 11 1 1 * /path/to/script.sh # Run on May 20th @8:30 PM 30 20 20 5 * /path/to/script.sh # Run on Dec 31st @11:25 AM 25 11 31 12 * /path/to/script.sh # Run script on your birthday date at 7:15 AM (20th May) 15 7 20 5 * /path/to/script.sh # Run job everyday @4:15 PM Monday - Friday 15 16 * * mon-fri /path/to/script.sh Note: We can write cron expression using below website ####### URL : https://crontab.guru/ ###### ====================================== Where to configure cronjob in linux ? ====================================== => "crontab file" is used to configure cronjobs in linux # open crontab file crontab -e # display cronjobs schedules crontab -l # remove crontab file crontab -r =================== Cronjob practice =================== 1) Launch "Linux Ubuntu AMI" machine 2) Connect with Linux VM using ssh client 3) Create shell script file $ vi task.sh Note: Write below commands in shell script file touch /home/ubuntu/f1.txt touch /home/ubuntu/f2.txt 4) Provide execute permission for script file $ chmod +x task.sh 5) Open crontab file and configure job schedule $ crontab -e Note: Configure below job scheule */1 * * * * /home/ubuntu/task.sh 6) Save and close crontab file (ctrl + x + y + enter) 7) After 1 minue check files got created. $ ls -l ======================================= 1) What is Shell Scripting and Why ? 2) What is sha-bang ? 3) How to write script files and how to execute ? 4) Variables (env variables & user-defined variables) 5) Variable Rules 6) What is .bashrc file ? 7) Operators 8) Conditional Statements (if-elif-else) 9) Looping Statements (for, while) 10) functions 11) Commandline Arguments 12) Cronjobs scheduling =======================================