Day 5 - Advanced Linux Shell Scripting for DevOps Engineers with User management
Creating n number of directories using shell script
#!/bin/bash
for num in {1..4}
do
mkdir folder$num
done
echo "Directories created sucessfully"
#!/bin/bash
for num in {1..4}
do
echo $num #prints the count
done
Creating n number of directories using for loop iteration
vi forloopiteration.sh raju
Script to Backup files
nano backupscript.sh
#!/bin/bash src=/c/Users/Rj/Downloads/nginx tgt=/c/Users/Rj/Downloads/shell filename=nginxbackup tar -czvf $tgt/$filename.tar.gz $src echo "backup done sucessfully"
xargs command is used to bring entire arguments in a single line
cat <filename> | xargs
awk command to print the specified columns from date
We can also take a backup by using the date
nano backupscript.sh
#!/bin/bash src=/c/Users/Rj/Downloads/nginx tgt=/c/Users/Rj/Downloads/shell filename=$(date | xargs | awk '{print $2"-"$3"-"$6}') tar -czvf $tgt/$filename.tar.gz $src echo "backup done sucessfully"
Crontab, to automate the backup Script
Open the Terminal
$ crontab -e
*/5 * * * * is to run the script every 5 mins
control+X and y enter
your script is ready for automation for every 5 mins
Creating Multiple Users
sudo useradd tom
sudo useradd jerry
id tom
id jerry
To view all the users
$ sudo cat /etc/passwd
Loop to find a file or directory in the present working directory
nano loop.sh
#!/bin/bash
for num in /*
do
echo $num
done
nano loop1.sh
#!/bin/bash
for num in /c/Users/Rj/Downloads/shell/*
do
echo $num
done
#!/bin/bash
for num in $1/*
do
echo $num
done
sh loop1.sh /c/Users/Rj/Downloads/shell
Some Linux commands to check
nproc # No of VMs
df -h # Free space
df -a # Free space
free -h # Free Space
top # Memory utilization
uname -m # arm configuration
last # Complete details of user histroy
lastlogs # Give the lastlogin user details
df -h # Free space
df -h | awk '{print $2,$4}'
df -h | xargs | awk '{print $1 "-" $2}'
Shell scripting Mini Project
#!/bin/bash
echo "#################Shell Script assignment###################"
echo "Welcome to Devops, by user: $(whoami)"
echo "Today date is:"
date | awk '{print $3"-"$2"-"$6}'
echo "Time is:"
date | awk '{print $4}'
echo "************************************************************"
echo "Server uptime is:"
uptime
echo "Top $1 last logins:"
last -a | head -$1
echo "*************************************************************"
echo "My Server Utilization:"
df -h | xargs | awk '{print "Free Disk space is:" $11}'
df -h | xargs | awk '{print "Total Disk space is:" $9}'
df -h | xargs | awk '{print "Used Disk space is:" $10}'
echo "*************************************************************"
echo "My Server RAM(Memory) Utilization:"
free -h | xargs | awk '{print "Free Memory is:" $10}'
free -h | xargs | awk '{print "Total Memory is:" $8}'
free -h | xargs | awk '{print "Used Memory is:" $9}'
echo "*************************************************************"
echo "Top $2 CPU Processess running:"
top -b | head -$2
Output:
You can also write like this alternatively