├── 1. Course Syllabus Overview ├── 869px-Tux.svg.png ├── Class image.jpg ├── CourseImage-01292020.jpg ├── CourseImage-12062019.jpg ├── CourseImage.pptx ├── Shell-Scripting-Syllabus.docx └── code-coding-connection-css.jpg ├── 5. Basic Shell Scripts ├── Exit-status.txt └── Handouts │ ├── Basic-Shell-Scripts.doc │ ├── Unix-Shell-Scripting.docx │ ├── case-Scripts.doc │ ├── do-while-Scripts.doc │ ├── for-loop-Scripts.doc │ └── if-then-Scripts.doc ├── 6. Real Life Scripts ├── Central-Logging.txt ├── Check-process-status-and-kill-it.txt ├── Copying-a-file-to-a-list-of-remote-hosts.txt ├── Create-System-Inventory.txt ├── Create-User-Accounts.txt ├── Create-backup.txt ├── Delete-old-files.txt ├── Disable-Inactive-Users.txt ├── Disk-Space-Status.txt ├── For-loops-Scripts-for-File-System-1.txt ├── For-loops-Scripts-for-File-System-2.txt ├── Homework.txt ├── List-of-Users-Logged-in-Today.txt ├── Section 6 - Handouts │ ├── PDF │ │ └── Script for Pre-Defined Info.pdf │ ├── Real Life Scripts.ppt │ └── Word │ │ ├── Script for Pre-Defined Info.docx │ │ └── ping-hosts.txt ├── Section 6 - Homework │ └── Homework.txt ├── Status-on-Total-Number-of-Files.txt ├── User-Directory-Assignment.txt └── ping-hosts.txt ├── 7. Additional Resources ├── Creating-NIC-Bonding.docx └── rhel-5-6-7-cheatsheet.pdf ├── LICENSE └── README.md /1. Course Syllabus Overview/869px-Tux.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/1. Course Syllabus Overview/869px-Tux.svg.png -------------------------------------------------------------------------------- /1. Course Syllabus Overview/Class image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/1. Course Syllabus Overview/Class image.jpg -------------------------------------------------------------------------------- /1. Course Syllabus Overview/CourseImage-01292020.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/1. Course Syllabus Overview/CourseImage-01292020.jpg -------------------------------------------------------------------------------- /1. Course Syllabus Overview/CourseImage-12062019.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/1. Course Syllabus Overview/CourseImage-12062019.jpg -------------------------------------------------------------------------------- /1. Course Syllabus Overview/CourseImage.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/1. Course Syllabus Overview/CourseImage.pptx -------------------------------------------------------------------------------- /1. Course Syllabus Overview/Shell-Scripting-Syllabus.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/1. Course Syllabus Overview/Shell-Scripting-Syllabus.docx -------------------------------------------------------------------------------- /1. Course Syllabus Overview/code-coding-connection-css.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/1. Course Syllabus Overview/code-coding-connection-css.jpg -------------------------------------------------------------------------------- /5. Basic Shell Scripts/Exit-status.txt: -------------------------------------------------------------------------------- 1 | Return Successful Exist Status 2 | 3 | ls -l 4 | echo $? 5 | 6 | pwd 7 | echo $? 8 | 9 | pwdd 10 | echo ? 11 | 12 | 13 | 14 | #!/bin/bash 15 | 16 | echo hello 17 | echo $? 18 | 19 | 20 | 21 | Return Un-Successful Exist Status 22 | 23 | #!/bin/bash 24 | 25 | echi hello 26 | echo $? 27 | 28 | 29 | 30 | 31 | Return exist status if file exist 32 | 33 | #!/bin/bash 34 | 35 | ls -l /home/iafzal/check 36 | 37 | if [ $? -eq 0 ] 38 | then 39 | echo File exist 40 | else 41 | echo File does not exist 42 | fi 43 | -------------------------------------------------------------------------------- /5. Basic Shell Scripts/Handouts/Basic-Shell-Scripts.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/5. Basic Shell Scripts/Handouts/Basic-Shell-Scripts.doc -------------------------------------------------------------------------------- /5. Basic Shell Scripts/Handouts/Unix-Shell-Scripting.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/5. Basic Shell Scripts/Handouts/Unix-Shell-Scripting.docx -------------------------------------------------------------------------------- /5. Basic Shell Scripts/Handouts/case-Scripts.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/5. Basic Shell Scripts/Handouts/case-Scripts.doc -------------------------------------------------------------------------------- /5. Basic Shell Scripts/Handouts/do-while-Scripts.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/5. Basic Shell Scripts/Handouts/do-while-Scripts.doc -------------------------------------------------------------------------------- /5. Basic Shell Scripts/Handouts/for-loop-Scripts.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/5. Basic Shell Scripts/Handouts/for-loop-Scripts.doc -------------------------------------------------------------------------------- /5. Basic Shell Scripts/Handouts/if-then-Scripts.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/5. Basic Shell Scripts/Handouts/if-then-Scripts.doc -------------------------------------------------------------------------------- /6. Real Life Scripts/Central-Logging.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/6. Real Life Scripts/Central-Logging.txt -------------------------------------------------------------------------------- /6. Real Life Scripts/Check-process-status-and-kill-it.txt: -------------------------------------------------------------------------------- 1 | Check if a process is running and kill it 2 | =========================================== 3 | 4 | 5 | 6 | vi sleep600 7 | 8 | #!/bin/bash 9 | 10 | sleep 600 11 | 12 | 13 | ----------------------------------------------------------- 14 | 15 | 16 | Run commands 17 | ps -ef | grep "sleep 600" 18 | 19 | 20 | ps -ef | grep "sleep 600" | grep -v grep 21 | 22 | 23 | ps -ef | grep "sleep 600" | grep -v grep | awk '{print $2}' 24 | 25 | 26 | ps -ef | grep "sleep 600" | grep -v grep | awk '{print $2}' | xargs -I{} echo {} 27 | 28 | 29 | ps -ef | grep "sleep 600" | grep -v grep | awk '{print $2}' | xargs -I{} kill {} 30 | 31 | ----------------------------------------------------------- 32 | 33 | 34 | 35 | Create a script 36 | 37 | #!/bin/bash 38 | 39 | 40 | ps -ef | grep "sleep 600" | grep -v grep | awk '{print $2}' | xargs -I{} kill {} 41 | 42 | echo All sleeping processes are killed 43 | 44 | 45 | -------------------------------------------------------------------------------- /6. Real Life Scripts/Copying-a-file-to-a-list-of-remote-hosts.txt: -------------------------------------------------------------------------------- 1 | Copying a file to a list of remote hosts 2 | ============================================== 3 | 4 | #!/bin/bash 5 | 6 | 7 | 8 | for HOST in ubuntu01 fedora02 centos03 rhel06 9 | 10 | 11 | do 12 | 13 | 14 | scp somefile $HOST:/var/tmp/ 15 | 16 | 17 | done -------------------------------------------------------------------------------- /6. Real Life Scripts/Create-System-Inventory.txt: -------------------------------------------------------------------------------- 1 | Create System Inventory 2 | ========================= 3 | 4 | # Create a file name database 5 | 6 | touch /home/iafzal/ps/database 7 | 8 | ------------------------------------------------------------- 9 | 10 | 11 | # Create 1st script to add record 12 | 13 | vi add-record 14 | 15 | #!/bin/bash 16 | 17 | echo Please enter hostname? 18 | read hostname 19 | echo 20 | 21 | grep -q $hostname /home/iafzal/ps/database 22 | if [ $? -eq 0 ] 23 | then 24 | echo ERROR -- Hostname $hostname already exist 25 | echo 26 | exit 0 27 | fi 28 | 29 | echo Please enter IP address? 30 | read IP 31 | echo 32 | 33 | grep -q $IP /home/iafzal/ps/database 34 | if [ $? -eq 0 ] 35 | then 36 | echo ERROR -- IP $IP already exist 37 | echo 38 | exit 0 39 | fi 40 | 41 | 42 | echo Please enter Description? 43 | read description 44 | echo 45 | 46 | echo $hostname $IP $description >> database 47 | 48 | 49 | 50 | ------------------------------------------------------------- 51 | 52 | 53 | # Create 2nd script to delete record 54 | 55 | vi del-record 56 | 57 | #!/bin/bash 58 | 59 | echo Please enter hostname or IP address? 60 | read host 61 | echo 62 | 63 | grep -q $host /home/iafzal/ps/database 64 | if [ $? -eq 0 ] 65 | then 66 | echo 67 | sed -i '/'$host'/d' /home/iafzal/ps/database 68 | echo $host has been deleted 69 | else 70 | echo Record does not exist 71 | fi 72 | 73 | ------------------------------------------------------------- 74 | 75 | Create a 3rd script to give option of adding or deleting a record 76 | 77 | 78 | vi inv-management 79 | 80 | #!/bin/bash 81 | echo 82 | echo Please select one of the following options: 83 | echo 84 | echo 'a = Add a record' 85 | echo 'd = Delete a record' 86 | echo 87 | read choice 88 | 89 | case $choice in 90 | a) /home/iafzal/ps/add-record;; 91 | d) /home/iafzal/ps/del-record;; 92 | *) echo Invalid choice - Bye. 93 | 94 | esac 95 | -------------------------------------------------------------------------------- /6. Real Life Scripts/Create-User-Accounts.txt: -------------------------------------------------------------------------------- 1 | 1 - Create User Account 2 | 3 | #!/bin/bash 4 | 5 | echo "Please provide a username?" 6 | read u 7 | echo 8 | 9 | useradd $u 10 | echo $u account has been created 11 | 12 | ==================================================== 13 | 14 | 2 - Error out if user exist 15 | #!/bin/bash 16 | 17 | echo "Please provide a username?" 18 | read u 19 | echo 20 | 21 | grep -q $u /etc/passwd 22 | if [ $? -eq 0 ] 23 | then 24 | echo ERROR -- User $u already exist 25 | echo Please choose another username 26 | echo 27 | exit 0 28 | fi 29 | 30 | useradd $u 31 | echo $u account has been created 32 | 33 | ==================================================== 34 | 35 | 3 - Add user description 36 | #!/bin/bash 37 | 38 | echo "Please provide a username?" 39 | read u 40 | echo 41 | 42 | grep -q $u /etc/passwd 43 | if [ $? -eq 0 ] 44 | then 45 | echo ERROR -- User $u already exist 46 | echo Please choose another username 47 | echo 48 | exit 0 49 | fi 50 | 51 | 52 | echo "Please provide user description?" 53 | read d 54 | echo 55 | 56 | useradd $u -c "$d" 57 | echo $u account has been created 58 | 59 | ==================================================== 60 | 61 | 4 - Add User ID 62 | #!/bin/bash 63 | 64 | echo "Please provide a username?" 65 | read u 66 | echo 67 | 68 | grep -q $u /etc/passwd 69 | if [ $? -eq 0 ] 70 | then 71 | echo ERROR -- User $u already exist 72 | echo Please choose another username 73 | echo 74 | exit 0 75 | fi 76 | 77 | 78 | echo "Please provide user description?" 79 | read d 80 | echo 81 | 82 | echo "Do you want to specify user ID (y/n)?" 83 | read ynu 84 | echo 85 | if [ $ynu == y ] 86 | then 87 | echo "Please enter UID?" 88 | read uid 89 | 90 | useradd $u -c "$d" -u $uid 91 | echo 92 | echo $u account has been created 93 | 94 | elif [ $ynu == n ] 95 | then 96 | echo No worries we will assign a UID 97 | useradd $u -c "$d" 98 | echo 99 | echo $u account has been created 100 | fi 101 | 102 | ==================================================== 103 | 104 | 5 - Add User ID check 105 | #!/bin/bash 106 | 107 | echo "Please provide a username?" 108 | read u 109 | echo 110 | 111 | grep -q $u /etc/passwd 112 | if [ $? -eq 0 ] 113 | then 114 | echo ERROR -- User $u already exist 115 | echo Please choose another username 116 | echo 117 | exit 0 118 | fi 119 | 120 | 121 | echo "Please provide user description?" 122 | read d 123 | echo 124 | 125 | echo "Do you want to specify user ID (y/n)?" 126 | read ynu 127 | echo 128 | if [ $ynu == y ] 129 | then 130 | echo "Please enter UID?" 131 | read uid 132 | grep -q $uid /etc/passwd 133 | if [ $? -eq 0 ] 134 | then 135 | echo ERROR -- UserID $uid already exist 136 | echo 137 | exit 0 138 | else 139 | useradd $u -c "$d" -u $uid 140 | echo 141 | echo $u account has been created 142 | fi 143 | elif [ $ynu == n ] 144 | then 145 | echo No worries we will assign a UID 146 | useradd $u -c "$d" 147 | echo 148 | echo $u account has been created 149 | fi 150 | -------------------------------------------------------------------------------- /6. Real Life Scripts/Create-backup.txt: -------------------------------------------------------------------------------- 1 | # Create backup 2 | 3 | 4 | tar cvf /tmp/backup.tar /etc /var 5 | 6 | 7 | 8 | 9 | 10 | # Compress backup 11 | 12 | 13 | gzip backup.tar 14 | 15 | 16 | 17 | 18 | 19 | #Check backup status and transfer 20 | 21 | 22 | 23 | #!/bin/bash 24 | 25 | tar cvf /tmp/backup.tar /etc /var 26 | gzip backup.tar 27 | 28 | 29 | 30 | find backup.tar.gz -mtime -1 -type f -print &> /dev/null 31 | 32 | 33 | if [ $? -eq 0 ] 34 | 35 | 36 | then 37 | 38 | 39 | echo Backup was created 40 | echo 41 | echo Archiving backup 42 | 43 | 44 | scp /tmp/backup.tar.gz root@192.168.1.x:/path 45 | 46 | 47 | else 48 | 49 | 50 | echo Backup failed 51 | 52 | 53 | fi -------------------------------------------------------------------------------- /6. Real Life Scripts/Delete-old-files.txt: -------------------------------------------------------------------------------- 1 | Create files with older timestamp 2 | 3 | 4 | touch -d "Thu, 1 March 2018 12:30:00" a 5 | 6 | 7 | 8 | 9 | Find and delete files older than 90 days 10 | 11 | 12 | find /path-to-dir -mtime +90 -exec ls -l {} \; 13 | 14 | 15 | 16 | 17 | Find and rename old files 18 | 19 | find . -mtime +90 -exec mv {} {}.old \; -------------------------------------------------------------------------------- /6. Real Life Scripts/Disable-Inactive-Users.txt: -------------------------------------------------------------------------------- 1 | Script to disable users accounts 2 | 3 | 4 | 5 | Test Commands 6 | 7 | lastlog -b 90 8 | 9 | lastlog -b 90 | tail -n+2 10 | 11 | lastlog -b 90 | tail -n+2 12 | 13 | lastlog | tail -n+2 | grep 'test' | awk '{print $1}' 14 | 15 | 16 | 17 | 18 | Disable account with For loop 19 | 20 | #!/bin/bash 21 | 22 | a=`lastlog | tail -n+2 | grep 'test' | awk '{print $1}'` 23 | 24 | for i in $a 25 | do 26 | usermod -L $i 27 | done 28 | 29 | 30 | 31 | 32 | Disable using xargs 33 | 34 | lastlog | tail -n+2 | grep 'test' | awk '{print $1}' | 35 | xargs -I{} echo {} 36 | 37 | 38 | lastlog | tail -n+2 | grep 'test' | awk '{print $1}' | 39 | xargs -I{} usermod -L {} -------------------------------------------------------------------------------- /6. Real Life Scripts/Disk-Space-Status.txt: -------------------------------------------------------------------------------- 1 | Disk Space Check 2 | ================= 3 | 4 | 5 | # Check the filesystem 6 | 7 | df -h 8 | 9 | 10 | # Remove un-wanted rows 11 | 12 | df -h | egrep -v "Filesystem|tmpfs" 13 | 14 | 15 | 16 | # Get 5th and 6th column 17 | 18 | df -h | egrep -v "Filesystem|tmpfs" | awk '{print $5, $6}' 19 | 20 | 21 | df -h | egrep -v "Filesystem|tmpfs" | awk '{print $5}' | cut -d'%' -f1` 22 | 23 | -------------------------------------------- 24 | 25 | First For loop script 26 | 27 | vi checkdisk 28 | 29 | 30 | #!/bin/bash 31 | 32 | 33 | a=`df -h | egrep -v "tmpfs|devtmpfs" | tail -n+2 | awk '{print $5}' | cut -d'%' -f1` 34 | 35 | for i in $a 36 | do 37 | if [ $i -ge 50 ] 38 | then 39 | echo Check disk space $i `df -h | grep $i` 40 | fi 41 | done 42 | 43 | 44 | -------------------------------------------- 45 | 46 | Second do while loop script 47 | 48 | vi checkdisk1 49 | 50 | 51 | 52 | Another way: 53 | #!/bin/sh 54 | 55 | df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $5,$1}' | while read output 56 | do 57 | usep=$(echo $output | awk '{print $1}' | cut -d'%' -f1 ) 58 | partition=$(echo $output | awk '{print $2}' ) 59 | 60 | if [ $usep -ge 90 ] 61 | then 62 | echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" 63 | fi 64 | done 65 | 66 | 67 | -------------------------------------------- 68 | 69 | Or Simply 70 | 71 | Write a script to awk only those rows with the value 72 | 73 | 74 | df -h | awk '0+$5 >= 10 {print}' 75 | 76 | 77 | To make it presentable 78 | 79 | echo 80 | echo Following is the disk space status 81 | echo 82 | df -h | awk '0+$5 >= 10 {print}' | awk '{print $5, $6}' 83 | 84 | -------------------------------------------------------------------------------- /6. Real Life Scripts/For-loops-Scripts-for-File-System-1.txt: -------------------------------------------------------------------------------- 1 | For loops Scripts for File System - 1 2 | ========================================== 3 | 4 | 5 | 1 - Simple counting: 6 | 7 | 8 | 9 | #!/bin/bash 10 | 11 | 12 | for i in {1..25} 13 | 14 | 15 | do 16 | 17 | 18 | sleep 1 19 | 20 | echo $i 21 | 22 | 23 | done 24 | 25 | 26 | 27 | 28 | -------------------------------------------------- 29 | 30 | 31 | 2 - Create multiple files with different names 32 | 33 | 34 | 35 | #!/bin/bash 36 | 37 | 38 | for i in {1..10} 39 | 40 | 41 | do 42 | 43 | 44 | touch imran.$i 45 | 46 | 47 | done 48 | 49 | 50 | 51 | -------------------------------------------------- 52 | 53 | 3 - Create multiple files upon input 54 | 55 | 56 | 57 | #!/bin/bash 58 | 59 | 60 | echo How many files do you want? 61 | 62 | 63 | read t 64 | 65 | 66 | echo 67 | 68 | 69 | echo Files names should start with? 70 | 71 | 72 | read n 73 | 74 | 75 | for i in $(seq 1 $t) 76 | 77 | 78 | do 79 | 80 | 81 | touch $n.$i 82 | 83 | 84 | done 85 | 86 | 87 | -------------------------------------------------- 88 | 89 | 90 | 91 | 4 - Assign write permissions to files 92 | 93 | 94 | 95 | #!/bin/bash 96 | 97 | 98 | for i in imran* 99 | 100 | 101 | do 102 | 103 | 104 | echo Assigning write permissions to $i 105 | 106 | 107 | chmod a+w $i 108 | 109 | 110 | sleep 1 111 | 112 | 113 | done 114 | 115 | -------------------------------------------------- 116 | 117 | 118 | 119 | 120 | 5 - Assign write permissions to files with total time it will take 121 | 122 | 123 | 124 | #!/bin/bash 125 | 126 | 127 | total=`ls -l imran* | wc -l` 128 | 129 | 130 | echo It will take $total seconds to assign permissions... 131 | 132 | 133 | echo 134 | 135 | 136 | for i in imran* 137 | 138 | 139 | do 140 | 141 | 142 | echo Assigning write permissions to $i 143 | 144 | 145 | chmod a+w $i 146 | 147 | 148 | sleep 1 149 | 150 | 151 | done -------------------------------------------------------------------------------- /6. Real Life Scripts/For-loops-Scripts-for-File-System-2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/6. Real Life Scripts/For-loops-Scripts-for-File-System-2.txt -------------------------------------------------------------------------------- /6. Real Life Scripts/Homework.txt: -------------------------------------------------------------------------------- 1 | Section 6 - Homework 2 | ==================================== 3 | 4 | Write a script to check if directory exist 5 | 6 | 7 | #!/bin/bash 8 | directory="./Test" 9 | 10 | # bash check if directory exists 11 | if [ -d $directory ]; then 12 | echo "Directory exists" 13 | else 14 | echo "Directory does not exists" 15 | fi 16 | 17 | --------------------------------------------------------- 18 | 19 | 20 | List Files and Directories with Description 21 | 22 | 23 | If you want to list the files in /home directory, you can use the for loop like this: 24 | 25 | 26 | 27 | #!/bin/bash 28 | 29 | 30 | for obj in /home/yourname/* 31 | 32 | 33 | do 34 | 35 | if [ -d $obj ] 36 | 37 | 38 | then 39 | 40 | echo $obj is a folder 41 | 42 | 43 | elif [ -f "$obj" ] 44 | 45 | 46 | then 47 | 48 | echo $obj is a file 49 | 50 | 51 | fi 52 | 53 | 54 | done 55 | 56 | --------------------------------------------------------- 57 | 58 | 59 | 60 | 61 | Create infinite loop 62 | 63 | 64 | 65 | #!/bin/bash 66 | 67 | 68 | while true 69 | 70 | 71 | do 72 | 73 | 74 | echo This is infinite loop 75 | 76 | 77 | done 78 | 79 | 80 | --------------------------------------------------------- 81 | 82 | 83 | 84 | 85 | Create a script for car inventory 86 | 87 | 88 | Add a new car 89 | It should ask user to add serial #, make, model, year and color of the car 90 | 91 | 92 | 93 | Delete a car 94 | It should delete a car by serial # 95 | 96 | Combine above tasks into one script using case statement 97 | -------------------------------------------------------------------------------- /6. Real Life Scripts/List-of-Users-Logged-in-Today.txt: -------------------------------------------------------------------------------- 1 | List of Users Logged in Today 2 | ================================ 3 | 4 | 5 | 6 | #!/bin/bash 7 | 8 | 9 | today=`date | awk '{print $1,$2,$3}'` 10 | 11 | 12 | #last | grep $today | awk '{print $1}' 13 | 14 | 15 | last | grep "$today" 16 | 17 | 18 | ------------------------------------------------------------- 19 | 20 | 21 | 22 | 23 | Ask for Input 24 | 25 | 26 | #!/bin/bash 27 | 28 | 29 | echo "please enter day (e.g. Mon)" 30 | 31 | 32 | read d 33 | 34 | 35 | echo "please enter month (e.g. Aug)" 36 | 37 | 38 | read m 39 | 40 | 41 | echo "please enter date (e.g. 28)" 42 | 43 | 44 | read da 45 | 46 | 47 | last | grep "$d $m $da" -------------------------------------------------------------------------------- /6. Real Life Scripts/Section 6 - Handouts/PDF/Script for Pre-Defined Info.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/6. Real Life Scripts/Section 6 - Handouts/PDF/Script for Pre-Defined Info.pdf -------------------------------------------------------------------------------- /6. Real Life Scripts/Section 6 - Handouts/Real Life Scripts.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/6. Real Life Scripts/Section 6 - Handouts/Real Life Scripts.ppt -------------------------------------------------------------------------------- /6. Real Life Scripts/Section 6 - Handouts/Word/Script for Pre-Defined Info.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/6. Real Life Scripts/Section 6 - Handouts/Word/Script for Pre-Defined Info.docx -------------------------------------------------------------------------------- /6. Real Life Scripts/Section 6 - Handouts/Word/ping-hosts.txt: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ping -c1 192.168.1.1 4 | if [ $? -eq 0 ] 5 | then 6 | echo OK 7 | else 8 | echo NOT OK 9 | fi 10 | 11 | Change the IP to 192.168.1.235 12 | 13 | 14 | Don't show the output 15 | ping -c1 192.168.1.1 &> /dev/null 16 | if [ $? -eq 0 ] 17 | then 18 | echo OK 19 | else 20 | echo NOT OK 21 | fi 22 | 23 | 24 | Define variable 25 | #!/bin/bash 26 | 27 | hosts="192.168.1.1" 28 | ping -c1 $hosts &> /dev/null 29 | if [ $? -eq 0 ] 30 | then 31 | echo $hosts OK 32 | else 33 | echo $hosts NOT OK 34 | fi 35 | 36 | Change the IP to 192.168.1.235 37 | 38 | 39 | 40 | 41 | Multiple IPs 42 | #!/bin/bash 43 | 44 | IPLIST="path_to_the_Ip_list_file" 45 | 46 | 47 | for ip in $(cat $IPLIST) 48 | 49 | do 50 | ping -c1 $ip &> /dev/null 51 | if [ $? -eq 0 ] 52 | then 53 | echo $ip ping passed 54 | else 55 | echo $ip ping failed 56 | fi 57 | done 58 | -------------------------------------------------------------------------------- /6. Real Life Scripts/Section 6 - Homework/Homework.txt: -------------------------------------------------------------------------------- 1 | Section 6 - Homework 2 | ==================================== 3 | 4 | Write a script to check if directory exist 5 | 6 | 7 | #!/bin/bash 8 | directory="./Test" 9 | 10 | # bash check if directory exists 11 | if [ -d $directory ]; then 12 | echo "Directory exists" 13 | else 14 | echo "Directory does not exists" 15 | fi 16 | 17 | --------------------------------------------------------- 18 | 19 | 20 | List Files and Directories with Description 21 | 22 | 23 | If you want to list the files in /home directory, you can use the for loop like this: 24 | 25 | 26 | 27 | #!/bin/bash 28 | 29 | 30 | for obj in /home/yourname/* 31 | 32 | 33 | do 34 | 35 | if [ -d $obj ] 36 | 37 | 38 | then 39 | 40 | echo $obj is a folder 41 | 42 | 43 | elif [ -f "$obj" ] 44 | 45 | 46 | then 47 | 48 | echo $obj is a file 49 | 50 | 51 | fi 52 | 53 | 54 | done 55 | 56 | --------------------------------------------------------- 57 | 58 | 59 | 60 | 61 | Create infinite loop 62 | 63 | 64 | 65 | #!/bin/bash 66 | 67 | 68 | while true 69 | 70 | 71 | do 72 | 73 | 74 | echo This is infinite loop 75 | 76 | 77 | done 78 | 79 | 80 | --------------------------------------------------------- 81 | 82 | 83 | 84 | 85 | Create a script for car inventory 86 | 87 | 88 | Add a new car 89 | It should ask user to add serial #, make, model, year and color of the car 90 | 91 | 92 | 93 | Delete a car 94 | It should delete a car by serial # 95 | 96 | Combine above tasks into one script using case statement 97 | -------------------------------------------------------------------------------- /6. Real Life Scripts/Status-on-Total-Number-of-Files.txt: -------------------------------------------------------------------------------- 1 | Status on Total Number of Files (Send alert if files are less than 20) 2 | ======================================================================= 3 | 4 | 5 | # First create 20 files 6 | 7 | touch file{1..20}.txt 8 | 9 | 10 | --------------------------------------------------- 11 | 12 | Now create a script: 13 | 14 | #!/bin/bash 15 | 16 | a=`ls -l file* | wc -l` 17 | 18 | if [ $a -eq 20 ] 19 | then 20 | echo Yes there are $a files 21 | else 22 | echo Files are less than 20 23 | fi 24 | 25 | 26 | -------------------------------------------------------------------------------- /6. Real Life Scripts/User-Directory-Assignment.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/6. Real Life Scripts/User-Directory-Assignment.txt -------------------------------------------------------------------------------- /6. Real Life Scripts/ping-hosts.txt: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ping -c1 192.168.1.1 4 | if [ $? -eq 0 ] 5 | then 6 | echo OK 7 | else 8 | echo NOT OK 9 | fi 10 | 11 | Change the IP to 192.168.1.235 12 | 13 | 14 | Don't show the output 15 | ping -c1 192.168.1.1 &> /dev/null 16 | if [ $? -eq 0 ] 17 | then 18 | echo OK 19 | else 20 | echo NOT OK 21 | fi 22 | 23 | 24 | Define variable 25 | #!/bin/bash 26 | 27 | hosts="192.168.1.1" 28 | ping -c1 $hosts &> /dev/null 29 | if [ $? -eq 0 ] 30 | then 31 | echo $hosts OK 32 | else 33 | echo $hosts NOT OK 34 | fi 35 | 36 | Change the IP to 192.168.1.235 37 | 38 | 39 | 40 | 41 | Multiple IPs 42 | #!/bin/bash 43 | 44 | IPLIST="path_to_the_Ip_list_file" 45 | 46 | 47 | for ip in $(cat $IPLIST) 48 | 49 | do 50 | ping -c1 $ip &> /dev/null 51 | if [ $? -eq 0 ] 52 | then 53 | echo $ip ping passed 54 | else 55 | echo $ip ping failed 56 | fi 57 | done 58 | -------------------------------------------------------------------------------- /7. Additional Resources/Creating-NIC-Bonding.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/7. Additional Resources/Creating-NIC-Bonding.docx -------------------------------------------------------------------------------- /7. Additional Resources/rhel-5-6-7-cheatsheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples-/0eeb7fb08734903560d8710e36b91bd9c1950401/7. Additional Resources/rhel-5-6-7-cheatsheet.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # A-Complete-Course-on-Linux-Bash-Shell-Scripting-with-Real-Life-Examples- 5 | A Complete Course on Linux Bash Shell Scripting with Real-Life Examples, published by Packt publishing 6 | --------------------------------------------------------------------------------