Shell Script
Shell Script
Write a script to check server usage and if it is 20% full, the script should
send you an email.
#!/bin/bash
#!/bin/bash
biggest=${numbers[0]}
sum=0
smallest=${numbers[0]}
biggest=${numbers[0]}
case $choice in
1) find_biggest_of_two ;;
2) find_biggest_of_three ;;
3) find_biggest_of_n ;;
4) find_sum_of_n ;;
5) find_smallest_of_n ;;
6) find_biggest_of_n ;;
7) check_odd_even ;;
8) echo "Exiting. Goodbye!"; exit ;;
*) echo "Invalid choice. Please enter a number between 1 and 8." ;;
esac
done
#!/bin/bash
echo
}
#!/bin/bash
length=${#word}
reversed=""
6. Write a Bash script that takes a directory path as an argument and counts the
number of files and directories in that directory.
#!/bin/bash
directory_path="$1"
if [ ! -d "$directory_path" ]; then
echo "Error: Not a valid directory path."
exit 1
fi
7. Create a Bash script that reads a CSV file and prints the number of lines in it.
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <csv_file>"
exit 1
fi
csv_file="$1"
if [ ! -f "$csv_file" ]; then
echo "Error: Not a valid CSV file."
exit 1
fi
8. Create a Bash script that reads a CSV file and prints 2nd,5th column in it.
#!/bin/bash
csv_file="$1"
if [ ! -f "$csv_file" ]; then
echo "Error: Not a valid CSV file."
exit 1
fi
# Print the 2nd and 5th columns from the CSV file
awk -F',' '{print $2, $5}' "$csv_file"
9. Write a Bash script that searches for all files with a specific extension (e.g.,
.txt) in a given directory and its subdirectories.
- moves them to a separate folder.
#!/bin/bash
source_directory="$1"
destination_directory="$2"
if [ ! -d "$source_directory" ]; then
echo "Error: Not a valid source directory."
exit 1
fi
if [ ! -d "$destination_directory" ]; then
echo "Error: Not a valid destination directory."
exit 1
fi
# Search for files with the specified extension in the source directory and its
subdirectories
find "$source_directory" -type f -name "*$file_extension" -exec mv -t
"$destination_directory" {} +
10. Write a Bash script that searches for all files with a specific extension
(e.g., .txt)
- Copy then to a separate folder call backup with the file and directory name
appended with backup
#!/bin/bash
source_directory="$1"
backup_directory="backup"
if [ ! -d "$source_directory" ]; then
echo "Error: Not a valid source directory."
exit 1
fi
# Search for files with the specified extension in the source directory and its
subdirectories
find "$source_directory" -type f -name "*$file_extension" -exec bash -c '
for file do
backup_name="${file##*/}_backup"
cp "$file" "$backup_directory/$backup_name"
done
' bash {} +
11. Create a Bash script that reads a log file and extracts all IP addresses that
made more than 100 requests in a given time frame.
#!/bin/bash
log_file="$1"
if [ ! -f "$log_file" ]; then
echo "Error: Not a valid log file."
exit 1
fi
# Extract IP addresses that made more than 100 requests in the given time frame
awk -v time_frame="$time_frame" '$4 >= (systime() - time_frame) { print $1 }'
"$log_file" \
| sort \
| uniq -c \
| awk '$1 > 100 { print $2 }'
12. Write a Bash script that checks the disk usage of a specific directory and
sends an email notification if the usage exceeds a certain threshold.
#!/bin/bash
# Get the current disk usage percentage for the specified directory
disk_usage=$(df -h "$directory_to_check" | awk 'NR==2 {print $(NF-1)}' | cut -d'%'
-f1)
13. Create a Bash script that reads a configuration file and installs the required
packages listed in it using a package manager
Script should able to identify the current linux type and use the appropriate
pkg manager - e.g., apt or yum.
#!/bin/bash
config_file="$1"
if [ ! -f "$config_file" ]; then
echo "Error: Not a valid configuration file."
exit 1
fi
14. Create a Bash script that monitors the CPU and memory usage of a server and
sends an alert if the usage exceeds a certain threshold.
The script should also log the usage details with timestamp.
#!/bin/bash
In Future
1. Create a Bash script that takes a URL as input and checks if the website is up
or down using the curl command.
The script should print "Website is up" if the HTTP response is 200, and
"Website is down" otherwise.
#!/bin/bash
# Function to check if the website is up or down
check_website_status() {
url="$1"
http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url")
website_url="$1"
#!/bin/bash
directory_path="$1"
# Get the current date and time for the tar.gz file name
timestamp=$(date +"%Y%m%d_%H%M%S")
#!/bin/bash
# Get the current date and time for the backup file name
timestamp=$(date +"%Y%m%d_%H%M%S")
# backup_config.sh
# Database configuration
DB_HOST="your_database_host"
DB_USER="your_database_user"
DB_PASSWORD="your_database_password"
DB_NAME="your_database_name"
Save both scripts, make them executable, and then you can run the backup script:
chmod +x backup_script.sh
./backup_script.sh
This script will dump the database, compress the backup file, and upload it to a
remote storage location using cURL.
Adjust the script according to your specific database and storage configurations.
#!/bin/bash
# Load configuration from a separate file
source /path/to/deployment_config.sh
# Pull the latest code from the version control system (assuming git)
git clone "$REPOSITORY_URL" "$LOCAL_REPO_PATH" || { echo >&2 "Failed to clone the
repository. Aborting."; exit 1; }
# deployment_config.sh
Replace placeholder values with your actual version control system and server
details.
Save both scripts, make them executable, and then you can run the deployment
script:
chmod +x deploy_script.sh
./deploy_script.sh
This script pulls the latest code from a Git repository, builds the application
using Maven, and deploys it to a remote server using SCP.
Adjust the script according to your specific application, version control system,
and deployment configurations.
#!/bin/bash
# docker_config.sh
# Container configuration
CONTAINER_NAME="your_container_name"
HOST_PORT="your_host_port"
CONTAINER_PORT="your_container_port"
Replace placeholder values with your actual container image and deployment details.
Save both scripts, make them executable, and then you can run the deployment
script:
chmod +x deploy_docker_script.sh
./deploy_docker_script.sh
This script pulls the latest container image, stops and removes existing containers
(if any), and runs the container with the specified configurations.
Adjust the script according to your specific Docker container configurations and
deployment requirements.