├── 0-hello-world.sh ├── 01-variables.sh ├── 02-variabes.sh ├── 03-variables.sh ├── 04-variables.sh ├── 05-data-types.sh ├── 06-arrays.sh ├── 07-conditions.sh ├── 08-functions.sh ├── 09-loops.sh ├── README.md ├── application ├── config ├── mysql-connector-5.1.18.jar ├── mysql-connector-java-5.1.40.jar └── student.war ├── conditions.MD ├── datatypes.MD ├── delete-old-logs.sh ├── disk-usage-updated.sh ├── disk-usage.sh ├── docker-install.sh ├── gmail-configure.MD ├── interview-questions.MD ├── mail.sh ├── setup.jpeg ├── student-final.sh ├── student.sh ├── templates └── template.html └── variables.md /0-hello-world.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Hello, I am learning Linux" -------------------------------------------------------------------------------- /01-variables.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | NAME=Ramesh 4 | 5 | #$NAME or ${NAME} -- this is comment 6 | 7 | echo "Hi, ${NAME}" -------------------------------------------------------------------------------- /02-variabes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DATE=$(date +%F) 4 | 5 | echo "Hello, Today's date is ${DATE}" -------------------------------------------------------------------------------- /03-variables.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | NAME=$1 4 | WISH=$2 5 | 6 | echo "Hi ${NAME}, Good ${WISH}" 7 | 8 | #how to know how many args are passed. 9 | 10 | echo "Number of args passed are: $#" 11 | 12 | echo "Number of args passed are: $@" -------------------------------------------------------------------------------- /04-variables.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Please enter username" 4 | 5 | read -s USERNAME 6 | 7 | echo "User is: ${USERNAME}" 8 | 9 | echo "Please enter password" 10 | 11 | read -s PASSWORD 12 | 13 | echo "Password is: ${PASSWORD}" -------------------------------------------------------------------------------- /05-data-types.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # I need to write a program to addition of 2 numbers 4 | # 2+2 = 4 --> example input and output 5 | NUMBER1=$1 6 | NUMBER2=$2 7 | RESULT=$((NUMBER1+NUMBER2)) 8 | 9 | echo "Additon Result = ${RESULT}" -------------------------------------------------------------------------------- /06-arrays.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | FRUITS=("Apple" "Banana" "Orange") 4 | 5 | echo "Fruit at 1st element ${FRUITS[1]}" 6 | 7 | echo "All Fruits: ${FRUITS[@]}" -------------------------------------------------------------------------------- /07-conditions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #getting the user ID. 4 | USERID=$(id -u) 5 | 6 | #checking the user is root or not 7 | if [ $USERID -eq 0 ] #-eq, -ne,-gt, -lt 8 | then 9 | echo "User has root access" 10 | else 11 | echo "You are not root user, Please run with root privelege" 12 | exit 1 13 | fi 14 | 15 | echo "Installing GIT" 16 | 17 | yum install git -y 18 | 19 | #checking the exit status 20 | if [ $? -eq 0 ] 21 | then 22 | echo "Installed Git successfully" 23 | else 24 | echo "Git is not installed" 25 | exit 12 26 | fi -------------------------------------------------------------------------------- /08-functions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | USERID=$(id -u) 4 | DATE=$(date +"%F-%H-%M-%S") 5 | LOG_FILE="$DATE.log" 6 | R="\e[31m" 7 | G="\e[32m" 8 | N="\e[0m" 9 | #check user is root or not 10 | 11 | if [ $USERID -ne 0 ] 12 | then 13 | echo "Please run this script with root user access" 14 | exit 1 15 | fi 16 | 17 | #this is a generic function, we need to pass arguments 18 | VALIDATE(){ 19 | if [ $1 -ne 0 ] 20 | then 21 | echo -e "$2 ... $R FAILED $N" 22 | exit 1 23 | else 24 | echo -e "$2 ... $G SUCCESS $N" 25 | fi 26 | } 27 | 28 | yum install git -y &>>$LOG_FILE 29 | 30 | VALIDATE $? "GIT Installation" 31 | 32 | yum install vimmm -y &>>$LOG_FILE 33 | 34 | VALIDATE $? "VIM Installation" 35 | 36 | yum install wget -y &>>$LOG_FILE 37 | 38 | VALIDATE $? "WGET Installation" 39 | 40 | yum install net-tools -y &>>$LOG_FILE 41 | 42 | VALIDATE $? "NET-TOOLS Installation" -------------------------------------------------------------------------------- /09-loops.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | USERID=$(id -u) 4 | DATE=$(date +"%F-%H-%M-%S") 5 | LOG_FILE="$DATE.log" 6 | R="\e[31m" 7 | G="\e[32m" 8 | Y="\e[33m" 9 | N="\e[0m" 10 | #check user is root or not 11 | if [ $USERID -ne 0 ] 12 | then 13 | echo "Please run this script with root user access" 14 | exit 1 15 | fi 16 | 17 | #this is a generic function, we need to pass arguments 18 | VALIDATE(){ 19 | if [ $1 -ne 0 ] 20 | then 21 | echo -e "$2 ... $R FAILED $N" 22 | exit 1 23 | else 24 | echo -e "$2 ... $G SUCCESS $N" 25 | fi 26 | } 27 | 28 | for PACKAGE in $@ #sample input: git vim net-tools wget 29 | do 30 | yum -q list installed $PACKAGE &>/dev/null 31 | if [ $? -ne 0 ] 32 | then 33 | echo "$PACKAGE ... Not Installed" 34 | yum install $PACKAGE -y &>>$LOG_FILE 35 | VALIDATE $? "$PACKAGE Installation" 36 | else 37 | echo -e "$PACKAGE ... $Y Installed already $N" 38 | fi 39 | done -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Shell Scripting 2 | 3 | Register For course: [Shell Scripting Course](https://bit.ly/shell-siva) 4 | 5 | If you were given a task, instead of running multiple commands to complete the task manually, you can simply put those commands in a file and run it as a script that does the task without human errors. 6 | 7 | ### ENV SetUp 8 | ##### Pre-Requisites 9 | 10 | * AWS Account 11 | * Putty 12 | * WinScp 13 | * IDE (NotePad++/VS Code) 14 | 15 | ![alt text](setup.jpeg) 16 | 17 | 18 | ### **Steps:** 19 | 20 | * Create one security group on default VPC. 21 | * Launch EC2, we use centos 8 22 | * Connect through putty, WinScp 23 | * Setup NotePad++ or VS Code as IDE in WinScp. 24 | 25 | ### Coding 26 | Coding is 2 types. 27 | * **Programming**
28 | Programming can be done by developers. Usually it has complex coding like data base transactions, session management, triggering communications, etc. Developers should write code in an effective way that should consume less memory and less time more speed. 29 | * **Scripting**
30 | Scripting is done by generally system admins, DevOps, Cloud, Database admins, developers, etc. Scripting is usually automating manual tasks like 31 | * Deleting old files 32 | * Installing packages 33 | * Configuring servers 34 | * Adding or removing users 35 | 36 | #### Coding Concepts 37 | Usually coding have universal concepts. 38 | * Variables 39 | * Data types 40 | * Conditions 41 | * Functions 42 | * Loops 43 | 44 | 45 | -------------------------------------------------------------------------------- /application/config: -------------------------------------------------------------------------------- 1 | Below is the config for app server to connect student DB 2 | 3 | 4 | 5 | SELINUX 6 | yum install policycoreutils-python-utils -y 7 | setsebool -P httpd_can_network_connect 1 8 | -------------------------------------------------------------------------------- /application/mysql-connector-5.1.18.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techworldwithsiva/shell-scripting-01/caa2a665777117e4d1a17bdf06c0261ea0e6f92a/application/mysql-connector-5.1.18.jar -------------------------------------------------------------------------------- /application/mysql-connector-java-5.1.40.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techworldwithsiva/shell-scripting-01/caa2a665777117e4d1a17bdf06c0261ea0e6f92a/application/mysql-connector-java-5.1.40.jar -------------------------------------------------------------------------------- /application/student.war: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techworldwithsiva/shell-scripting-01/caa2a665777117e4d1a17bdf06c0261ea0e6f92a/application/student.war -------------------------------------------------------------------------------- /conditions.MD: -------------------------------------------------------------------------------- 1 | ### Conditions 2 | 3 | Conditions are used to take a decision. Usually we use if. A general syntax is 4 | 5 | ``` 6 | if [ expression ] 7 | then 8 | these statements run above expression is true 9 | else 10 | these statements run above expression is true 11 | fi 12 | ``` 13 | 14 | ### Exit Status 15 | 16 | Usually in Linux, script won't stop by default even it face error. It is our responsibility to check the previous command is success or not. 17 | 18 | 0 --> Success code
19 | 1-127 --> Failure -------------------------------------------------------------------------------- /datatypes.MD: -------------------------------------------------------------------------------- 1 | #### Data Types 2 | There is no importance to data types in shell scripting. However we can have dataypes. By default shell consider the values as string format i.e text.
3 | 4 | But we can do arithmetic operations, shell can intelligently convert text to number if it is really number. 5 | 6 | We can have array in shell script. Elements starts from index 0. 7 | 8 | ``` 9 | FRUITS=("Apple" "Orange" "Banana") 10 | 11 | echo "Fruits are: ${FRUITS[@]}" 12 | ``` -------------------------------------------------------------------------------- /delete-old-logs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | directory=/home/centos/logs 4 | 5 | DATE=$(date +%F) 6 | LOG_FILE="$DATE.log" 7 | 8 | INPUT=$(find $directory -name "*.log" -type f -mtime +14) 9 | 10 | while IFS= read line; 11 | do 12 | echo "Deleting log file: $line" &>>$LOG_FILE 13 | rm -rf $line 14 | done <<< "$INPUT" -------------------------------------------------------------------------------- /disk-usage-updated.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #df -hT | grep -vE 'tmpfs|Filesystem' | awk '{print $6 " " $1}' 4 | #df -hT | grep -vE 'tmpfs|Filesystem' | awk '{print $6}' | cut -d "%" -f1 5 | 6 | DISK_THRESHOLD=10 7 | DISK_USAGE=$(df -hT | grep -vE 'tmpfs|Filesystem' | awk '{print $6 " " $1}') 8 | message="" 9 | 10 | while IFS= read line; 11 | do 12 | usage=$(echo $line | cut -d "%" -f1) 13 | partition=$(echo $line | cut -d " " -f2) 14 | echo "usage: $usage" 15 | echo "partition: $partition" 16 | if [ $usage -ge $DISK_THRESHOLD ] 17 | then 18 | message+="High Disk usage on $partition: $usage%\n" 19 | fi 20 | done <<< "$DISK_USAGE" 21 | 22 | echo "message: $message" 23 | 24 | #echo -e "$message" | mail -s "High Disk Usage" info@joindevops.com 25 | 26 | sh mail.sh info@joindevops.com "High Disk Usage" "$message" "DevOps Team" 27 | 28 | #disk,memory,cpu --> any of this or all -------------------------------------------------------------------------------- /disk-usage.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #df -hT | grep -vE 'tmpfs|Filesystem' | awk '{print $6 " " $1}' 4 | #df -hT | grep -vE 'tmpfs|Filesystem' | awk '{print $6}' | cut -d "%" -f1 5 | 6 | DISK_THRESHOLD=1 7 | DISK_USAGE=$(df -hT | grep -vE 'tmpfs|Filesystem' | awk '{print $6 " " $1}') 8 | message="" 9 | 10 | while IFS= read line; 11 | do 12 | usage=$(echo $line | cut -d "%" -f1) 13 | partition=$(echo $DISK_USAGE | cut -d " " -f2) 14 | echo "usage: $usage" 15 | echo "partition: $partition" 16 | if [ $usage -ge $DISK_THRESHOLD ] 17 | then 18 | message+="High Disk usage on $partition: $usage%\n" 19 | fi 20 | done <<< "$DISK_USAGE" 21 | 22 | echo "message: $message" 23 | 24 | echo -e "$message" | mail -s "High Disk Usage" info@joindevops.com 25 | 26 | #disk,memory,cpu --> any of this or all 27 | -------------------------------------------------------------------------------- /docker-install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -eE -o functrace 3 | failure() { 4 | local lineno=$1 5 | local msg=$2 6 | echo "Failed at $lineno: $msg" 7 | } 8 | trap 'failure ${LINENO} "$BASH_COMMAND"' ERR 9 | 10 | USERID=$(id -u) 11 | LOGFILE="docker-install.log" 12 | 13 | R="\e[31m" 14 | N="\e[0m" 15 | G="\e[32m" 16 | 17 | if [ $USERID -ne 0 ] 18 | then 19 | echo -e "$R Please run this script with root access $N" 20 | exit 1 21 | fi 22 | 23 | yum install -y yum-utils &>>$LOGFILE 24 | echo -e "yum-utils ... $G Installed $N" 25 | 26 | yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo &>>$LOGFILE 27 | echo -e "Adding Docker repo ... $G Done $N" 28 | 29 | yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y &>>$LOGFILE 30 | 31 | echo -e "Docker Enginer $G Installed $N" 32 | 33 | echo "Starting Docker" 34 | systemctl start docker &>>$LOGFILE 35 | 36 | echo "Enabling Docker" 37 | systemctl enable docker &>>$LOGFILE 38 | 39 | usermod -aG docker centos 40 | 41 | echo "Dockr installation completed, logout and login again" -------------------------------------------------------------------------------- /gmail-configure.MD: -------------------------------------------------------------------------------- 1 | ### Sending Gmail from CentOS 8 2 | 3 | * switch to root 4 | ``` 5 | sudo su - 6 | ``` 7 | 8 | * Update yum repo 9 | ``` 10 | yum update -y --exclude=kernel* 11 | ``` 12 | 13 | * Install Postfix, the SASL authentication framework, and mailx. 14 | ``` 15 | yum -y install postfix cyrus-sasl-plain mailx 16 | ``` 17 | 18 | * Restart Postfix to detect the SASL framework. 19 | ``` 20 | systemctl restart postfix 21 | ``` 22 | 23 | * Start Postfix on boot 24 | ``` 25 | systemctl enable postfix 26 | ``` 27 | * Open the /etc/postfix/main.cf file. 28 | ``` 29 | vi /etc/postfix/main.cf 30 | ``` 31 | append the following into the file at the end 32 | 33 | ``` 34 | relayhost = [smtp.gmail.com]:587 35 | smtp_use_tls = yes 36 | smtp_sasl_auth_enable = yes 37 | smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd 38 | smtp_sasl_security_options = noanonymous 39 | smtp_sasl_tls_security_options = noanonymous 40 | smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt 41 | ``` 42 | 43 | * Configure Postfix SASL Credentials 44 | 45 | Add the Gmail credentials for authentication. Create a "/etc/postfix/sasl_passwd" file 46 | ``` 47 | touch /etc/postfix/sasl_passwd 48 | ``` 49 | Add the following line to the file: 50 | ``` 51 | vi /etc/postfix/sasl_passwd 52 | ``` 53 | ``` 54 | [smtp.gmail.com]:587 xyz:AppPassword 55 | ``` 56 | **Note: xyz is from xyz@gmail.com, password_app is from google management app password** 57 | 58 | * Create a Postfix lookup table from the sasl_passwd text file by running the following command: 59 | ``` 60 | postmap /etc/postfix/sasl_passwd 61 | ``` 62 | * Sending mail 63 | Run the following command to send mail: 64 | ``` 65 | echo "This is a test mail & Date $(date)" | mail -s "message" info@joindevops.com 66 | ``` 67 | -------------------------------------------------------------------------------- /interview-questions.MD: -------------------------------------------------------------------------------- 1 | #### Interview Questions 2 | 3 | Refer below URL for Shell Scripting Interview Questions. -------------------------------------------------------------------------------- /mail.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | TO=$1 3 | SUBJECT=$2 4 | BODY_CONTENT=$(sed -e 's/[]\/$*.^[]/\\&/g' <<< $3) 5 | echo "escaped content: $BODY_CONTENT" 6 | NAME=$4 7 | ALERT_TYPE=$2 8 | template="/home/centos/templates/template.html" 9 | 10 | final_content=$(sed -e "s/TEAM/$NAME/g" -e "s/BODY_CONTENT/$BODY_CONTENT/g" -e "s/ALERT_TYPE/$ALERT_TYPE/g" "$template") 11 | 12 | echo "final_content: $final_content" 13 | 14 | echo -e "$final_content" | mail -s "$(echo -e "$SUBJECT\nContent-Type: text/html")" $TO -------------------------------------------------------------------------------- /setup.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techworldwithsiva/shell-scripting-01/caa2a665777117e4d1a17bdf06c0261ea0e6f92a/setup.jpeg -------------------------------------------------------------------------------- /student-final.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | USAGE(){ 4 | echo "USAGE: $0 " #$0--> scriptname, $#--> no of args #@--> all args $!--> PID 5 | } 6 | 7 | 8 | USERID=$(id -u) 9 | DATE=$(date +%F) 10 | LOGFILE="$DATE.log" 11 | 12 | #colors 13 | R="\e[31m" 14 | G="\e[32m" 15 | N="\e[0m" 16 | Y="\e[33m" 17 | TOMCAT_VERSION=$1 #9.0.75 18 | 19 | if [ -z $TOMCAT_VERSION ] 20 | then 21 | USAGE 22 | exit 1 23 | fi 24 | 25 | #extracting tomcat major version 26 | TOMCAT_MAJOR_VERSION=$(echo $TOMCAT_VERSION | cut -d "." -f1) 27 | #forming download URL 28 | TOMCAT_URL=https://dlcdn.apache.org/tomcat/tomcat-$TOMCAT_MAJOR_VERSION/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz 29 | #forming tar file 30 | TOMCAT_TAR_FILE=$(echo $TOMCAT_URL | awk -F "/" '{print $NF}') 31 | #forming tomcat dir 32 | TOMCAT_DIR=$(echo $TOMCAT_TAR_FILE | sed -e 's/.tar.gz//g') 33 | #student application file 34 | STUDENT_WAR_FILE=https://raw.githubusercontent.com/techworldwithsiva/shell-scripting-01/master/application/student.war 35 | #MySQl Connector 36 | MYSQL_DRIVER=https://raw.githubusercontent.com/techworldwithsiva/shell-scripting-01/master/application/mysql-connector-5.1.18.jar 37 | 38 | if [ $USERID -ne 0 ] 39 | then 40 | echo -e "$R Please run this script with root access $N" 41 | exit 1 42 | fi 43 | 44 | VALIDATE(){ 45 | if [ $1 -ne 0 ] 46 | then 47 | echo -e "$2 ... $R FAILED $N" 48 | exit 1 49 | else 50 | echo -e "$2 ... $G SUCCESS $N" 51 | fi 52 | } 53 | 54 | # SELINUX to enable traffic from Nginx to tomcat 55 | yum install policycoreutils-python-utils -y &>>$LOGFILE 56 | VALIDATE $? "SELINUX related pacakges" 57 | setsebool -P httpd_can_network_connect 1 &>>$LOGFILE 58 | VALIDATE $? "Allow Nginx to Tomcat" 59 | 60 | #install basic commands 61 | yum install wget vim net-tools java-1.8.0-openjdk-devel -y &>>$LOGFILE 62 | 63 | #install mariadb server 64 | yum install mariadb-server -y &>>$LOGFILE 65 | VALIDATE $? "Installing MariaDB" 66 | 67 | #Starting mariadb server 68 | systemctl start mariadb &>>$LOGFILE 69 | VALIDATE $? "Starting MariaDB" 70 | 71 | #Enabling mariadb server 72 | systemctl enable mariadb &>>$LOGFILE 73 | VALIDATE $? "Enabling MariaDB" 74 | 75 | #creating schema, table and granting priveleges 76 | echo "create database if not exists studentapp; 77 | use studentapp; 78 | 79 | CREATE TABLE if not exists Students(student_id INT NOT NULL AUTO_INCREMENT, student_name VARCHAR(100) NOT NULL, student_addr VARCHAR(100) NOT NULL, student_age VARCHAR(3) NOT NULL, student_qual VARCHAR(20) NOT NULL, student_percent VARCHAR(10) NOT NULL, student_year_passed VARCHAR(10) NOT NULL, PRIMARY KEY (student_id)); 80 | 81 | grant all privileges on studentapp.* to 'student'@'localhost' identified by 'student@1';" > /tmp/student.sql 82 | 83 | mysql < /tmp/student.sql 84 | VALIDATE $? "Creating DB Schema, table, grant priveleges" 85 | 86 | ### Tomcat 87 | mkdir -p /opt/tomcat 88 | cd /opt/tomcat 89 | 90 | if [ -d $TOMCAT_DIR ] 91 | then 92 | echo -e "$Y Tomcat alread exists $N" 93 | else 94 | wget $TOMCAT_URL &>>$LOGFILE 95 | VALIDATE $? "Downloading tomcat" 96 | tar -xf $TOMCAT_TAR_FILE &>>$LOGFILE 97 | VALIDATE $? "Untar tomcat" 98 | fi 99 | 100 | cd $TOMCAT_DIR/webapps 101 | 102 | wget $STUDENT_WAR_FILE &>>$LOGFILE 103 | VALIDATE $? "Downloading Student app" 104 | 105 | cd ../lib 106 | wget $MYSQL_DRIVER &>>$LOGFILE 107 | VALIDATE $? "Downloading MySQL Driver" 108 | 109 | cd ../conf 110 | sed -i '/TestDB/ d' context.xml 111 | VALIDATE $? "Removed existing DB config" 112 | sed -i '$ i ' context.xml 113 | VALIDATE $? "Added DB resource in context.xml" 114 | 115 | cd ../bin 116 | sh shutdown.sh &>>$LOGFILE 117 | sh startup.sh &>>$LOGFILE 118 | VALIDATE $? "Tomcat started" 119 | 120 | #NgINX 121 | yum install nginx -y &>>$LOGFILE 122 | VALIDATE $? "Installing Nginx" 123 | 124 | echo 'location / { 125 | proxy_pass http://127.0.0.1:8080/; 126 | proxy_set_header Host $host; 127 | proxy_set_header X-Real-IP $remote_addr; 128 | }' > /etc/nginx/default.d/student.conf 129 | VALIDATE $? "Added student.conf" 130 | 131 | sed -i '/location \/ {/,/}/d' /etc/nginx/nginx.conf 132 | VALIDATE $? "Removed default location block" 133 | 134 | systemctl restart nginx 135 | VALIDATE $? "NgInx restarted" 136 | 137 | -------------------------------------------------------------------------------- /student.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | USERID=$(id -u) 4 | R="\e[31m" 5 | G="\e[32m" 6 | N="\e[0m" 7 | if [ $USERID -ne 0 ] 8 | then 9 | echo -e "$R Please run this script with root access $N" 10 | exit 1 11 | fi 12 | 13 | VALIDATE(){ 14 | if [ $1 -ne 0 ] 15 | then 16 | echo -e "$2 ... $R FAILED $N" 17 | exit 1 18 | else 19 | echo -e "$2 ... $G SUCCESS $N" 20 | fi 21 | } 22 | 23 | #install mariadb server 24 | yum install mariadb-server -y 25 | VALIDATE $? "Installing MariaDB" 26 | 27 | #Starting mariadb server 28 | systemctl start mariadb 29 | VALIDATE $? "Starting MariaDB" 30 | 31 | #Enabling mariadb server 32 | systemctl enable mariadb 33 | VALIDATE $? "Enabling MariaDB" 34 | 35 | echo "create database if not exists studentapp; 36 | use studentapp; 37 | 38 | CREATE TABLE if not exists Students(student_id INT NOT NULL AUTO_INCREMENT, student_name VARCHAR(100) NOT NULL, student_addr VARCHAR(100) NOT NULL, student_age VARCHAR(3) NOT NULL, student_qual VARCHAR(20) NOT NULL, student_percent VARCHAR(10) NOT NULL, student_year_passed VARCHAR(10) NOT NULL, PRIMARY KEY (student_id)); 39 | 40 | grant all privileges on studentapp.* to 'student'@'localhost' identified by 'student@1';" > /tmp/student.sql 41 | 42 | mysql < /tmp/student.sql 43 | VALIDATE $? "Creating DB Schema, table, grant priveleges" -------------------------------------------------------------------------------- /templates/template.html: -------------------------------------------------------------------------------- 1 |

2 | Dear TEAM, 3 |

4 |
5 |   6 |
7 |
8 | This is auto generated Email.
9 |
10 |
11 |   12 |
13 |
14 | This is to alert you regarding  15 | ALERT_TYPE 16 | . Please find the details below and take appropriate action. 17 |
18 |
19 |   20 |
21 |
22 | 23 | BODY_CONTENT 24 | 25 |
26 |
27 |   28 |
29 |
30 | Regards, 31 |
32 |
33 | Linux Admin team. 34 |
35 |
-------------------------------------------------------------------------------- /variables.md: -------------------------------------------------------------------------------- 1 | ### Variables 2 | 3 | There multiple types to pass the values for variables in shell script. in shell scripting variable declaration is like below. 4 | 5 | ``` 6 | VAR_NAME=SOME_VALUE 7 | ``` 8 | * Directly in the script 9 | * We can run a command inside the script with in $() and get the output stored into variable. 10 | * We can pass from the arguments. 11 | * We can ask user to enter the value through read command. --------------------------------------------------------------------------------