├── DiskSize.sh ├── InstallSoftware-Positionalarg.sh ├── InstallSoftware.sh ├── Processkill.sh ├── README.md ├── Systemhealthcheck.sh ├── automatedeluseraccount.sh ├── backup.sh ├── cal-func.sh ├── cal.sh ├── curl-weather.sh ├── curl.sh ├── deletelog.sh ├── function.sh ├── get_mysql_process.sh ├── grep.sh ├── root-users.sh ├── servicestatus.sh ├── statuscheck.sh ├── umount.sh └── whiletest.sh /DiskSize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "This program get first 10 biggest file in the file system passed via positional argument" 3 | path="$1" 4 | echo $path 5 | du -ah $path | sort -hr | head -n 5 > /tmp/filesize.txt 6 | echo "This is the list of big files in the file system $path " 7 | cat /tmp/filesize.txt 8 | -------------------------------------------------------------------------------- /InstallSoftware-Positionalarg.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #Author: DevopsTechStack 3 | #Installing mutliple packages 4 | 5 | if [[ $# -eq 0 ]] 6 | then 7 | echo "Usage: please provide software names as command line arguments" 8 | exit 1 9 | fi 10 | 11 | 12 | if [[ $(id -u) -ne 0 ]] 13 | then 14 | echo "Please run from root user or with sudo privilage" 15 | exit 2 16 | fi 17 | 18 | 19 | for softwares in $@ 20 | do 21 | if which $softwares &> /dev/null 22 | then 23 | echo "Already $softwares is installed" 24 | else 25 | echo "Installing $softwares ......" 26 | yum install $softwares -y &> /dev/null 27 | if [[ $? -eq 0 ]] 28 | then 29 | echo "Successfully installed $softwares packages" 30 | else 31 | echo "Unable to install $softwares" 32 | fi 33 | fi 34 | 35 | done 36 | -------------------------------------------------------------------------------- /InstallSoftware.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "script to install git " 3 | echo "Installation started" 4 | if [ "$(uname)" == "Linux" ]; 5 | then 6 | echo "this is linux box,installing git" 7 | yum install git -y 8 | elif [ "$(uname)" == "Darwin" ]; 9 | then 10 | echo "this is not linux box" 11 | echo "this is Macos" 12 | brew install git 13 | else 14 | echo "not installing" 15 | fi 16 | -------------------------------------------------------------------------------- /Processkill.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "This script kill Process which is taking maximum memory" 3 | Processid=`ps au --sort -%mem | head -10 | awk '{print $2}' | awk ' NR==2'` 4 | echo $Processid 5 | kill -9 $Processid 6 | if [[ $? -eq 0 ]]; 7 | then 8 | echo "Process is killed successfully" 9 | else 10 | echo "some issue in kill" 11 | fi 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ShellScripting project 2 | Complete shell scripting project. 3 | 30 plus shell scripts project will be available here. 4 | -------------------------------------------------------------------------------- /Systemhealthcheck.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | EMAIL='' 3 | function sysstat { 4 | echo -e " 5 | ##################################################################### 6 | Health Check Report (CPU,Process,Disk Usage, Memory) 7 | ##################################################################### 8 | 9 | 10 | Hostname : `hostname` 11 | Kernel Version : `uname -r` 12 | Uptime : `uptime | sed 's/.*up \([^,]*\), .*/\1/'` 13 | Last Reboot Time : `who -b | awk '{print $3,$4}'` 14 | 15 | 16 | 17 | ********************************************************************* 18 | CPU Load - > Threshold < 1 Normal > 1 Caution , > 2 Unhealthy 19 | ********************************************************************* 20 | " 21 | MPSTAT=`which mpstat` 22 | MPSTAT=$? 23 | if [ $MPSTAT != 0 ] 24 | then 25 | echo "Please install mpstat!" 26 | echo "On Debian based systems:" 27 | echo "sudo apt-get install sysstat" 28 | echo "On RHEL based systems:" 29 | echo "yum install sysstat" 30 | else 31 | echo -e "" 32 | LSCPU=`which lscpu` 33 | LSCPU=$? 34 | if [ $LSCPU != 0 ] 35 | then 36 | RESULT=$RESULT" lscpu required to producre acqurate reults" 37 | else 38 | cpus=`lscpu | grep -e "^CPU(s):" | cut -f2 -d: | awk '{print $1}'` 39 | i=0 40 | while [ $i -lt $cpus ] 41 | do 42 | echo "CPU$i : `mpstat -P ALL | awk -v var=$i '{ if ($3 == var ) print $4 }' `" 43 | let i=$i+1 44 | done 45 | fi 46 | echo -e " 47 | Load Average : `uptime | awk -F'load average:' '{ print $2 }' | cut -f1 -d,` 48 | 49 | Heath Status : `uptime | awk -F'load average:' '{ print $2 }' | cut -f1 -d, | awk '{if ($1 > 2) print "Unhealthy"; else if ($1 > 1) print "Caution"; else print "Normal"}'` 50 | " 51 | fi 52 | echo -e " 53 | ********************************************************************* 54 | Process 55 | ********************************************************************* 56 | 57 | => Top memory using processs/application 58 | 59 | PID %MEM RSS COMMAND 60 | `ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10` 61 | 62 | => Top CPU using process/application 63 | `top b -n1 | head -17 | tail -11` 64 | 65 | ********************************************************************* 66 | Disk Usage - > Threshold < 90 Normal > 90% Caution > 95 Unhealthy 67 | ********************************************************************* 68 | " 69 | df -Pkh | grep -v 'Filesystem' > /tmp/df.status 70 | while read DISK 71 | do 72 | LINE=`echo $DISK | awk '{print $1,"\t",$6,"\t",$5," used","\t",$4," free space"}'` 73 | echo -e $LINE 74 | echo 75 | done < /tmp/df.status 76 | echo -e " 77 | 78 | Heath Status" 79 | echo 80 | while read DISK 81 | do 82 | USAGE=`echo $DISK | awk '{print $5}' | cut -f1 -d%` 83 | if [ $USAGE -ge 95 ] 84 | then 85 | STATUS='Unhealty' 86 | elif [ $USAGE -ge 90 ] 87 | then 88 | STATUS='Caution' 89 | else 90 | STATUS='Normal' 91 | fi 92 | 93 | LINE=`echo $DISK | awk '{print $1,"\t",$6}'` 94 | echo -ne $LINE "\t\t" $STATUS 95 | echo 96 | done < /tmp/df.status 97 | rm /tmp/df.status 98 | TOTALMEM=`free -m | head -2 | tail -1| awk '{print $2}'` 99 | TOTALBC=`echo "scale=2;if($TOTALMEM<1024 && $TOTALMEM > 0) print 0;$TOTALMEM/1024"| bc -l` 100 | USEDMEM=`free -m | head -2 | tail -1| awk '{print $3}'` 101 | USEDBC=`echo "scale=2;if($USEDMEM<1024 && $USEDMEM > 0) print 0;$USEDMEM/1024"|bc -l` 102 | FREEMEM=`free -m | head -2 | tail -1| awk '{print $4}'` 103 | FREEBC=`echo "scale=2;if($FREEMEM<1024 && $FREEMEM > 0) print 0;$FREEMEM/1024"|bc -l` 104 | TOTALSWAP=`free -m | tail -1| awk '{print $2}'` 105 | TOTALSBC=`echo "scale=2;if($TOTALSWAP<1024 && $TOTALSWAP > 0) print 0;$TOTALSWAP/1024"| bc -l` 106 | USEDSWAP=`free -m | tail -1| awk '{print $3}'` 107 | USEDSBC=`echo "scale=2;if($USEDSWAP<1024 && $USEDSWAP > 0) print 0;$USEDSWAP/1024"|bc -l` 108 | FREESWAP=`free -m | tail -1| awk '{print $4}'` 109 | FREESBC=`echo "scale=2;if($FREESWAP<1024 && $FREESWAP > 0) print 0;$FREESWAP/1024"|bc -l` 110 | 111 | echo -e " 112 | ********************************************************************* 113 | Memory 114 | ********************************************************************* 115 | 116 | => Physical Memory 117 | 118 | Total\tUsed\tFree\t%Free 119 | 120 | ${TOTALBC}GB\t${USEDBC}GB \t${FREEBC}GB\t$(($FREEMEM * 100 / $TOTALMEM ))% 121 | 122 | => Swap Memory 123 | 124 | Total\tUsed\tFree\t%Free 125 | 126 | ${TOTALSBC}GB\t${USEDSBC}GB\t${FREESBC}GB\t$(($FREESWAP * 100 / $TOTALSWAP ))% 127 | " 128 | } 129 | FILENAME="health-`hostname`-`date +%y%m%d`-`date +%H%M`.txt" 130 | sysstat > $FILENAME 131 | echo -e "Reported file $FILENAME generated in current directory." $RESULT 132 | if [ "$EMAIL" != '' ] 133 | then 134 | STATUS=`which mail` 135 | if [ "$?" != 0 ] 136 | then 137 | echo "The program 'mail' is currently not installed." 138 | else 139 | cat $FILENAME | mail -s "$FILENAME" $EMAIL 140 | fi 141 | fi 142 | -------------------------------------------------------------------------------- /automatedeluseraccount.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | #Delete_User - Automates the 4 steps to remove an account 4 | # 5 | ############################################################### 6 | # Define Functions 7 | # 8 | ##################################################### 9 | function get_answer { 10 | # 11 | unset answer 12 | ask_count=0 13 | # 14 | while [ -z "$answer" ] #While no answer is given, keep asking. 15 | do 16 | ask_count=$[ $ask_count + 1 ] 17 | # 18 | case $ask_count in #If user gives no answer in time allotted 19 | 2) 20 | echo 21 | echo "Please answer the question." 22 | echo 23 | ;; 24 | 3) 25 | echo 26 | echo "One last try...please answer the question." 27 | echo 28 | ;; 29 | 4) 30 | echo 31 | echo "Since you refuse to answer the question..." 32 | echo "exiting program." 33 | echo 34 | # 35 | exit 36 | ;; 37 | esac 38 | # 39 | if [ -n "$line2" ] 40 | then #Print 2 lines 41 | echo $line1 42 | echo -e $line2" \c" 43 | else #Print 1 line 44 | echo -e $line1" \c" 45 | fi 46 | # 47 | # Allow 60 seconds to answer before time-out 48 | read -t 60 answer 49 | done 50 | # Do a little variable clean-up 51 | unset line1 52 | unset line2 53 | # 54 | } #End of get_answer function 55 | # 56 | ##################################################### 57 | function process_answer { 58 | # 59 | answer=$(echo $answer | cut -c1) 60 | # 61 | case $answer in 62 | y|Y) 63 | # If user answers "yes", do nothing. 64 | ;; 65 | *) 66 | # If user answers anything but "yes", exit script 67 | echo 68 | echo $exit_line1 69 | echo $exit_line2 70 | echo 71 | exit 72 | ;; 73 | esac 74 | # 75 | # Do a little variable clean-up 76 | # 77 | unset exit_line1 78 | unset exit_line2 79 | # 80 | } #End of process_answer function 81 | # 82 | ############################################## 83 | # End of Function Definitions 84 | # 85 | ############# Main Script #################### 86 | # Get name of User Account to check 87 | # 88 | echo "Step #1 - Determine User Account name to Delete " 89 | echo 90 | line1="Please enter the username of the user " 91 | line2="account you wish to delete from system:" 92 | get_answer 93 | user_account=$answer 94 | # 95 | # Double check with script user that this is the correct User Account 96 | # 97 | line1="Is $user_account the user account " 98 | line2="you wish to delete from the system? [y/n]" 99 | get_answer 100 | # 101 | # Call process_answer funtion: 102 | # if user answers anything but "yes", exit script 103 | # 104 | exit_line1="Because the account, $user_account, is not " 105 | exit_line1="the one you wish to delete, we are leaving the script..." 106 | process_answer 107 | # 108 | ################################################################ 109 | # Check that user_account is really an account on the system 110 | # 111 | user_account_record=$(cat /etc/passwd | grep -w $user_account) 112 | # 113 | if [ $? -eq 1 ] # If the account is not found, exit script 114 | then 115 | echo 116 | echo "Account, $user_account, not found. " 117 | echo "Leaving the script..." 118 | echo 119 | exit 120 | fi 121 | # 122 | echo 123 | echo "I found this record:" 124 | echo $user_account_record 125 | echo 126 | # 127 | line1="Is this the correct User Account? [y/n]" 128 | get_answer 129 | # 130 | # 131 | # Call process_answer function: 132 | # if user answers anything but "yes", exit script 133 | # 134 | exit_line1="Because the account, $user_account, is not " 135 | exit_line2="the one you wish to delete, we are leaving the script..." 136 | process_answer 137 | # 138 | ################################################################## 139 | # Search for any running processes that belong to the User Account 140 | # 141 | echo 142 | echo "Step #2 - Find process on system belonging to user account" 143 | echo 144 | # 145 | ps -u $user_account> /dev/null #List user processes running. 146 | 147 | case $? in 148 | 1) # No processes running for this User Account 149 | # 150 | echo "There are no processes for this account currently running." 151 | echo 152 | ;; 153 | 0) # Processes running for this User Account. 154 | # Ask Script User if wants us to kill the processes. 155 | # 156 | echo "$user_account has the following process(es) running:" 157 | ps -u $user_account 158 | # 159 | line1="Would you like me to kill the process(es)? [y/n]" 160 | get_answer 161 | # 162 | answer=$(echo $answer | cut -c1) 163 | # 164 | case $answer in 165 | y|Y) # If user answers "yes", 166 | # kill User Account processes. 167 | # 168 | echo 169 | echo "Killing off process(es)..." 170 | # 171 | # List user process running code in command_1 172 | command_1="ps -u $user_account --no-heading" 173 | # 174 | # Create command_3 to kill processes in variable 175 | command_3="xargs -d \\n /usr/bin/sudo /bin/kill -9" 176 | # 177 | # Kill processes via piping commands together 178 | $command_1 | gawk '{print $1}' | $command_3 179 | # 180 | echo 181 | echo "Process(es) killed." 182 | ;; 183 | *) #If user answers anything but "yes", do not kill. 184 | echo 185 | echo "Will not kill process(es)." 186 | ;; 187 | esac 188 | ;; 189 | esac 190 | ################################################################# 191 | # Create a report of all files owned by User Account 192 | # 193 | echo 194 | echo "Step #3 - Find files on system belonging to user account" 195 | echo 196 | echo "Creating a report of all files owned by $user_account." 197 | echo 198 | echo "It is recommended that you backup/archive these files," 199 | echo "and then do one of two things:" 200 | echo " 1) Delete the files" 201 | echo " 2) Change the files' ownership to a current user account." 202 | echo 203 | echo "Please wait. This may take a while..." 204 | # 205 | report_date=$(date +%y%m%d) 206 | report_file="$user_account"_Files_"$report_date".rpt 207 | # 208 | find / -user $user_account> $report_file 2>/dev/null 209 | # 210 | echo 211 | echo "Report is complete." 212 | echo "Name of report: $report_file" 213 | echo -n "Location of report: "; pwd 214 | echo 215 | #################################### 216 | # Remove User Account 217 | echo 218 | echo "Step #4 - Remove user account" 219 | echo 220 | # 221 | line1="Do you wish to remove $user_account's account from system? [y/n]" 222 | get_answer 223 | # 224 | # Call process_answer function: 225 | # if user answers anything but "yes", exit script 226 | # 227 | exit_line1="Since you do not wish to remove the user account," 228 | exit_line2="$user_account at this time, exiting the script..." 229 | process_answer 230 | # 231 | userdel $user_account #delete user account 232 | echo 233 | echo "User account, $user_account, has been removed" 234 | echo 235 | # 236 | exit 237 | -------------------------------------------------------------------------------- /backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | backup_dirs=("/etc" "/home") 4 | dest_dir="/root/backup_today" 5 | mkdir -p $dest_dir 6 | backup_date=$(date +%b-%d-%y) 7 | 8 | echo "Starting backup of: ${backup_dirs[@]}" 9 | 10 | for i in "${backup_dirs[@]}"; do 11 | sudo tar -Pczf /tmp/$i-$backup_date.tar.gz $i 12 | if [ $? -eq 0 ]; then 13 | echo "$i backup succeeded." 14 | else 15 | echo "$i backup failed." 16 | fi 17 | cp /tmp/$i-$backup_date.tar.gz $dest_dir 18 | if [ $? -eq 0 ]; then 19 | echo "$i transfer succeeded." 20 | else 21 | echo "$i transfer failed." 22 | fi 23 | done 24 | 25 | sudo rm /tmp/*.gz 26 | echo "Backup is done." 27 | -------------------------------------------------------------------------------- /cal-func.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | clear 3 | echo "--------------------------------" 4 | echo "-------welcome to calculator----" 5 | echo "--------------------------------" 6 | read_input() 7 | { 8 | read -p "Enter first number: " num1 9 | read -p "Enter second number: " num2 10 | } 11 | echo -e "[a]ddition\n[b]Subtraction\n[c]Multiplication\n[d]Division\n" 12 | read -p "Enter your choice: " choice 13 | case $choice in 14 | [aA]) 15 | read_input 16 | result=$((num1+num2)) 17 | echo "The result for your choice is: $result" 18 | ;; 19 | [bB]) 20 | read_input 21 | result=$((num1-num2)) 22 | echo "The result for your choice is: $result" 23 | ;; 24 | [cC]) 25 | read_input 26 | result=$((num1*num2)) 27 | echo "The result for your choice is: $result" 28 | ;; 29 | [dD]) 30 | read_input 31 | result=$((num1/num2)) 32 | echo "The result for your choice is: $result" 33 | ;; 34 | *) 35 | echo "Wrong choice" 36 | ;; 37 | esac 38 | -------------------------------------------------------------------------------- /cal.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | clear 3 | echo "--------------------------------" 4 | echo "-------welcome to calculator----" 5 | echo "--------------------------------" 6 | echo -e "[a]ddition\n[b]Subtraction\n[c]Multiplication\n[d]Division\n" 7 | read -p "Enter your choice: " choice 8 | case $choice in 9 | [aA]) 10 | read -p "Enter first number: " num1 11 | read -p "Enter second number: " num2 12 | result=$((num1+num2)) 13 | echo "The result for your choice is: $result" 14 | ;; 15 | [bB]) 16 | read -p "Enter first number: " num1 17 | read -p "Enter second number: " num2 18 | result=$((num1-num2)) 19 | echo "The result for your choice is: $result" 20 | ;; 21 | [cC]) 22 | read -p "Enter first number: " num1 23 | read -p "Enter second number: " num2 24 | result=$((num1*num2)) 25 | echo "The result for your choice is: $result" 26 | ;; 27 | [dD]) 28 | read -p "Enter first number: " num1 29 | read -p "Enter second number: " num2 30 | result=$((num1/num2)) 31 | echo "The result for your choice is: $result" 32 | ;; 33 | *) 34 | echo "Wrong choice" 35 | ;; 36 | esac 37 | -------------------------------------------------------------------------------- /curl-weather.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Function to display usage 4 | show_usage() { 5 | echo "Usage: $0 " 6 | echo "Example: $0 Delhi" 7 | } 8 | 9 | # Check if city is provided 10 | CITY="$1" 11 | if [ -z "$CITY" ]; then 12 | echo "Error: City name not provided." 13 | show_usage 14 | exit 1 15 | fi 16 | 17 | # Displaying a loading message 18 | echo "Fetching weather details for $CITY..." 19 | 20 | # Fetch weather data 21 | WEATHER=$(curl -s "https://wttr.in/${CITY}?format=3") 22 | 23 | # Check if the response is valid 24 | if [[ "$WEATHER" == *"Unknown location"* ]]; then 25 | echo "Error: Unable to fetch weather details for '$CITY'. Please check the city name." 26 | exit 1 27 | fi 28 | 29 | # Display the weather details 30 | echo "====================================" 31 | echo " WEATHER REPORT " 32 | echo "====================================" 33 | echo "$WEATHER" 34 | echo "====================================" 35 | 36 | # Suggest viewing the full report 37 | echo "For more details, visit: https://wttr.in/${CITY}" 38 | 39 | root@Kritika:/home/devops/devopstechstack# ls 40 | ShellScripting commit.sh mysqlproc.sh rootusers.sh umount.sh weather.sh 41 | root@Kritika:/home/devops/devopstechstack# cat ^C 42 | root@Kritika:/home/devops/devopstechstack# cat weather.sh 43 | #!/bin/bash 44 | 45 | # Function to display usage 46 | show_usage() { 47 | echo "Usage: $0 " 48 | echo "Example: $0 Delhi" 49 | } 50 | 51 | # Check if city is provided 52 | CITY="$1" 53 | if [ -z "$CITY" ]; then 54 | echo "Error: City name not provided." 55 | show_usage 56 | exit 1 57 | fi 58 | 59 | # Displaying a loading message 60 | echo "Fetching weather details for $CITY..." 61 | 62 | # Fetch weather data 63 | WEATHER=$(curl -s "https://wttr.in/${CITY}?format=3") 64 | 65 | # Check if the response is valid 66 | if [[ "$WEATHER" == *"Unknown location"* ]]; then 67 | echo "Error: Unable to fetch weather details for '$CITY'. Please check the city name." 68 | exit 1 69 | fi 70 | 71 | # Display the weather details 72 | echo "====================================" 73 | echo " WEATHER REPORT " 74 | echo "====================================" 75 | echo "$WEATHER" 76 | echo "====================================" 77 | 78 | # Suggest viewing the full report 79 | echo "For more details, visit: https://wttr.in/${CITY}" 80 | -------------------------------------------------------------------------------- /curl.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | URL="https://github.com/Kritika-git/Docker-Projects" 4 | #echo "%{http_code}" 5 | response=$(curl -s -w "%{http_code}" $URL) 6 | 7 | http1_code=$(tail -n1 <<< "$response") # get the last line 8 | content=$(sed '$ d' <<< "$response") # get all but the last line which contains the status code 9 | 10 | echo "$http1_code" 11 | if [ $http1_code == 200 ]; 12 | then 13 | echo "Request is working fine" 14 | else 15 | echo "send slack message that request is denied" 16 | fi 17 | #echo "$content" 18 | -------------------------------------------------------------------------------- /deletelog.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "This script delete files which are older than 30 days " 3 | path="$1" 4 | echo $path 5 | find $path -mtime +30 -delete 6 | if [[ $? -eq 0 ]]; 7 | then 8 | echo "Files are successfully deleted " 9 | else 10 | echo "deleteion was having some issue" 11 | fi 12 | -------------------------------------------------------------------------------- /function.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "This is function test" 3 | disk_utilization() 4 | { 5 | disk=`df -h` 6 | echo "disk utiliation is : $disk " 7 | } 8 | if [[ $? -eq 0 ]]; 9 | then 10 | echo "this is disk usage report" 11 | disk_utilization 12 | else 13 | echo "disk has some issue " 14 | fi 15 | -------------------------------------------------------------------------------- /get_mysql_process.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Define colors 4 | RESET="\033[0m" 5 | CYAN="\033[36m" 6 | YELLOW="\033[33m" 7 | GREEN="\033[32m" 8 | MAGENTA="\033[35m" 9 | RED="\033[31m" 10 | 11 | # MySQL credentials 12 | MYSQL_USER="root" # Replace with your MySQL username 13 | MYSQL_PASS="devops" # Replace with your MySQL password 14 | MYSQL_HOST="localhost" # Change if not running MySQL locally 15 | 16 | # Run MySQL SHOW PROCESSLIST command 17 | RESULT=$(mysql -u$MYSQL_USER -p$MYSQL_PASS -h$MYSQL_HOST -e "SHOW PROCESSLIST;" 2>/dev/null) 18 | 19 | # Check for errors 20 | if [[ $? -ne 0 ]]; then 21 | echo -e "${RED}Error connecting to MySQL. Check your credentials.${RESET}" 22 | exit 1 23 | fi 24 | 25 | # Process and colorize output 26 | echo -e "${CYAN}--- SHOW PROCESSLIST Result ---${RESET}" 27 | echo "$RESULT" | while IFS= read -r line; do 28 | if [[ $line == *"Id"* && $line == *"Command"* ]]; then 29 | # Header 30 | echo -e "${YELLOW}${line}${RESET}" 31 | else 32 | # Data rows 33 | echo "$line" | awk -v reset="$RESET" -v green="$GREEN" -v cyan="$CYAN" -v magenta="$MAGENTA" -v yellow="$YELLOW" ' 34 | { 35 | printf cyan $1 reset "\t" green $2 reset "\t" magenta $3 reset "\t" yellow $4 reset "\t"; 36 | for (i = 5; i <= NF; i++) printf $i " "; 37 | printf "\n"; 38 | }' 39 | fi 40 | done 41 | -------------------------------------------------------------------------------- /grep.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | error_file=`cat /var/log/messages` 3 | matched_error=`grep -i error /var/log/messages` 4 | echo $matched_error 5 | if [[ $? -eq 0 ]]; 6 | then 7 | echo "found error in OS logs: $matched_error " 8 | else 9 | echo "no error in message logs" 10 | fi 11 | -------------------------------------------------------------------------------- /root-users.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Checking users with root privileges..." 4 | 5 | # 1. Users with UID 0 (direct root access) 6 | echo "Users with UID 0 (direct root access):" 7 | awk -F: '$3 == 0 {print $1}' /etc/passwd 8 | 9 | # 2. Users in the 'sudo' group (indirect root access via sudo) 10 | echo "Users in the 'sudo' group:" 11 | getent group sudo | awk -F: '{print $4}' | tr ',' '\n' 12 | 13 | # 3. Users explicitly granted sudo privileges in the sudoers file 14 | echo "Users with explicit sudo privileges in /etc/sudoers:" 15 | awk '/^[^#].*ALL=\(ALL\)/ {print $1}' /etc/sudoers 16 | 17 | # 4. Users in files under /etc/sudoers.d (additional sudo privileges) 18 | echo "Users with sudo privileges in /etc/sudoers.d:" 19 | for file in /etc/sudoers.d/*; do 20 | [ -f "$file" ] && awk '/^[^#].*ALL=\(ALL\)/ {print $1}' "$file" 21 | done 22 | -------------------------------------------------------------------------------- /servicestatus.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "====status check docker service====" 3 | status="`systemctl status docker|awk 'NR==3 {print}'|cut -d ':' -f 2|cut -d '(' -f 1`" 4 | echo $status 5 | if [ $status = "active" ]; 6 | then 7 | echo "service is running fine....." 8 | else 9 | echo "service is not running" 10 | fi 11 | -------------------------------------------------------------------------------- /statuscheck.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo " Welcome to service status check script " 3 | read -p "Enter the service name to check its status: " service_name 4 | if [ -z $service_name ]; 5 | then 6 | echo " Please eneter a valid service name " 7 | else 8 | systemctl status $service_name 9 | fi 10 | -------------------------------------------------------------------------------- /umount.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Define the mount point or device 4 | MOUNT_POINT="/mnt/wsl/docker-desktop/docker-desktop-user-distro" # Change to your mount point(The directory where the disk is mounted) 5 | DEVICE="/dev/sdc" # Change to your disk/device 6 | 7 | # Function to check if disk is in use 8 | #This function checks if there are any processes using files on the disk using the lsof +D command. 9 | #If the disk is in use, it will print an error message and return a non-zero exit code (1), preventing unmounting. 10 | 11 | check_disk_usage() { 12 | lsof +D "$MOUNT_POINT" > /dev/null 2>&1 13 | if [ $? -eq 0 ]; then 14 | echo "Error: Disk is still in use. Please close all files and processes accessing the disk." 15 | return 1 16 | else 17 | return 0 18 | fi 19 | } 20 | #The script first checks whether the disk is mounted using mount | grep. 21 | # If the disk is not mounted, it will print an error message and exit. 22 | # Check if the disk is mounted 23 | if mount | grep "on $MOUNT_POINT " > /dev/null; then 24 | echo "Disk is mounted at $MOUNT_POINT." 25 | 26 | # Step 1: Ensure no files are in use on the disk 27 | echo "Checking if disk is in use..." 28 | if ! check_disk_usage; then 29 | echo "Unable to unmount disk. Please stop processes using the disk." 30 | exit 1 31 | fi 32 | #The script tries to unmount the disk using sudo umount. If the unmount is successful, it prints a success message. 33 | # If it fails, it suggests that the disk may need to be forcefully unmounted (though this is avoided in this script). 34 | # Step 2: Attempt to unmount the disk 35 | echo "Attempting to unmount $DEVICE at $MOUNT_POINT..." 36 | sudo umount "$MOUNT_POINT" 37 | 38 | # Check if umount was successful 39 | if [ $? -eq 0 ]; then 40 | echo "Successfully unmounted $DEVICE from $MOUNT_POINT." 41 | else 42 | echo "Error: Failed to unmount $DEVICE. You may need to force unmount." 43 | fi 44 | #The script handles errors gracefully by printing messages and exiting when necessary (e.g., if the disk is in use or not mounted). 45 | else 46 | echo "Error: $DEVICE is not mounted at $MOUNT_POINT." 47 | exit 1 48 | fi 49 | -------------------------------------------------------------------------------- /whiletest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "while demo......" 3 | while read -r line; 4 | do 5 | echo "$line" 6 | done < test.txt 7 | --------------------------------------------------------------------------------