Skip to content

Commit

Permalink
4th commit adding of Unix Shell assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
femigoals authored Dec 9, 2023
1 parent a4fc050 commit dc92ce9
Show file tree
Hide file tree
Showing 18 changed files with 587 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Assignment_1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

echo -n "Enter your directory: "
read -r x
du -sh "$x"
8 changes: 8 additions & 0 deletions Assignment_10.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
echo -n "Enter The Number: "
read -r n
if [ $((n % 2)) -eq 0 ]; then
echo "is a Even Number"
else
echo "is a Odd Number"
fi
16 changes: 16 additions & 0 deletions Assignment_11.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
echo -n "Enter A Number: "
read -r n
arm=0
temp=$n
while [ "$n" -ne 0 ]; do
r=$((n % 10))
arm=$((arm + r * r * r))
n=$((n / 10))
done
echo $arm
if [ $arm -eq "$temp" ]; then
echo "Armstrong"
else
echo "Not Armstrong"
fi
15 changes: 15 additions & 0 deletions Assignment_12.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
echo -n "Enter Any Number: "
read -r n
i=1; c=1
while [ $i -le "$n" ]; do
i=$((i + 1))
r=$((n % i))
[ $r -eq 0 ] && c=$((c + 1))
done

if [ $c -eq 2 ]; then
echo "Prime"
else
echo "Not Prime"
fi
11 changes: 11 additions & 0 deletions Assignment_13.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

x=0; y=1; i=2
while true; do
i=$((i + 1))
z=$((x + y))
echo -n "$z "
x=$y
y=$z
sleep .5
done
34 changes: 34 additions & 0 deletions Assignment_14.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

for ((i = 32; i >= 0; i--)); do
r=$((2 ** i))
Probablity+=($r)
done

[[ $# -eq 0 ]] && {
echo -e "Usage \n \t $0 numbers"
exit 1
}

echo -en "Decimal\t\tBinary\n"
for input_int; do
s=0
test ${#input_int} -gt 11 && {
echo "Support Upto 10 Digit number :: skiping \"$input_int\""
continue
}

printf "%-10s\t" "$input_int"

for n in ${Probablity[@]}; do

if [[ $input_int -lt $n ]]; then
[[ $s == 1 ]] && printf "%d" 0
else
echo -n 1
s=1
input_int=$((input_int - n))
fi
done
echo -e
done
28 changes: 28 additions & 0 deletions Assignment_15.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

#
# Author: Abhishek Shingane (abhisheks@iitbhilai.ac.in)
# Date: 11 Sep 2020
#

if ! [ -x "$(command -v jq)" ]; then
echo 'Error: jq is not installed. Install via https://stedolan.github.io/jq/download/'
exit 1
fi

if [[ $# -ne 1 ]]; then
echo 'Provide I.P as command line parameter. Usage: ' $0 ' 15.45.0.1 '
exit 1
fi
link=$(echo "http://ip-api.com/json/"$1)
data=$(curl $link -s) # -s for slient output

status=$(echo $data | jq '.status' -r)

if [[ $status == "success" ]]; then

city=$(echo $data | jq '.city' -r)
regionName=$(echo $data | jq '.regionName' -r)
country=$( echo $data | jq '.country' -r)
echo $city, $regionName in $country.
fi
6 changes: 6 additions & 0 deletions Assignment_16.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
name=$1
path=$2
tar -czvf "$name.tar.gz" "$path"
gpg -c "$name.tar.gz"
rm -rf "$name.tar.gz"
23 changes: 23 additions & 0 deletions Assignment_17.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#!/bin/bash
#
# Linux Shell Scripting Tutorial 1.05r3, Summer-2002
#
# Written by Vivek G. Gite <vivek@nixcraft.com>
#
# Latest version can be found at www.nixcraft.com/
#
# Q1.Script to sum to nos
#

if [ $# -ne 2 ]
then
echo “Usage – $0 x y”
echo ” Where x and y are two nos for which I will print sum”
exit 1
fi
echo “Sum of $1 and $2 is `expr $1 + $2`
#
# ./ch.sh: vivek-tech.com to nixcraft.com referance converted using this tool
# See the tool at www.nixcraft.com/uniqlinuxfeatures/tools/
#
12 changes: 12 additions & 0 deletions Assignment_18.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# scmuser created the topic: how to restore the dump file using shell scripting

# I have created a script file to dump the application files using the following script

# Code
# =============
#!/bin/bash


now=$(date +”%d-%m-%Y”)
#use 1 instead of 0 which is incremental backup
dump -0f $now /var/www/html/*
53 changes: 53 additions & 0 deletions Assignment_2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
# test-file: Evaluate the status of a file
echo "Hey what's the File/Directory name (using the absolute path)?"
read FILE

if [ -e "$FILE" ]; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ]; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ]; then
echo "$FILE is writable."
fi
if [ -x "$FILE" ]; then
echo "$FILE is executable/searchable."
fi
else
echo "$FILE does not exist"
exit 1
fi
exit



#!/bin/bash

# Program to check the status of a file

echo -n "Enter the file directory"
read -r File

if [ -e "$FILE" ]; then
if [-f $FILE]; then
echo "$FILE is a regular file"
if
if [ -r $FILE ]; then
echo "$FILE is a readable file"
if
if [ -d $FILE ]; then
echo "$FILE is a directory"
if
else
echo "$FILE is not available "
exit 1
fi
exit


35 changes: 35 additions & 0 deletions Assignment_3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
date
echo "uptime:"
uptime
echo "Currently connected:"
w
echo "--------------------"
echo "Last logins:"
last -a | head -3
echo "--------------------"
echo "Disk and memory usage:"
df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'
free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}'
echo "--------------------"
start_log=$(head -1 /var/log/messages | cut -c 1-12)
oom=$(grep -ci kill /var/log/messages)
echo -n "OOM errors since $start_log :" $oom
echo ""
echo "--------------------"
echo "Utilization and most expensive processes:"
top -b | head -3
echo
top -b | head -10 | tail -4
echo "--------------------"
echo "Open TCP ports:"
nmap -p -T4 127.0.0.1
echo "--------------------"
echo "Current connections:"
ss -s
echo "--------------------"
echo "processes:"
ps auxf --width=200
echo "--------------------"
echo "vmstat:"
vmstat 1 5
18 changes: 18 additions & 0 deletions Assignment_4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

MAX=95
EMAIL=server@127.0.0.1

# Calculate CPU usage and round to the nearest integer
USE=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {printf "%.0f", usage}')

# Debug statement to print the calculated CPU usage
echo "Calculated CPU Usage: $USE"

# Compare the integer value with the maximum threshold
if [ "$USE" -gt "$MAX" ]; then
echo "Percent used: $USE" | mail -s "Running out of CPU power" "$EMAIL"
echo "Email sent."
else
echo "CPU usage below threshold."
fi
22 changes: 22 additions & 0 deletions Assignment_5.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

MAX=95
EMAIL=server@127.0.0.1
PART=sda1

# Extract the usage percentage, making sure to handle the case where the partition is not found
DF_OUTPUT=$(df -h)
USE=$(echo "$DF_OUTPUT" | awk -v part="$PART" '$NF==part {print $(NF-1)}' | sed 's/%//')

# Debug statement to print the actual output of the df command
echo "df output:"
echo "$DF_OUTPUT"

# Check if USE is a valid number
if [[ "$USE" =~ ^[0-9]+$ ]]; then
# Compare the integer value with the maximum threshold
if [ "$USE" -gt "$MAX" ]; then
echo "Percent used: $USE" | mail -s "Running out of disk space" "$EMAIL"
fi

fi
Loading

0 comments on commit dc92ce9

Please sign in to comment.