Commit 4361a560 authored by mohammad.ali's avatar mohammad.ali

Update README.md

parent 66531148
HOMEWORK LINUX
EX1:
script :
#!/bin/bash
if [ "$#" -ne 3 ]; then
echo "Usage: $0 oldword newword extension"
exit 1
fi
oldword=$1
newword=$2
extension=$3
find . -type f -name "*.$extension" -exec sed -i "s/$oldword/$newword/g" {} +
echo "All occurrences of '$oldword' have been replaced with '$newword' in all .$extension files."
in terminal :
chmod +x name_of_script
./name_of_script oldword newword txt
EX2 :
in teminal :
a-
mkdir ~/new_folder
cd ~/new_folder
b-
for i in {1..10}; do for j in {a..z}; do touch "file${i}_${j}"; done; done
c-
for i in {1..10}; do echo "Hello Linux" > "file${i}_x"; done
d-
for letter in {a..z}; do
mkdir -p "$letter"
mv *_"$letter" "$letter"/
done
e-
find . -type f \( -name '*3*' -o -name '*4*' \) -delete
EX3 :
in script :
#!/bin/bash
# alive2.sh
# Checks to see if a range of hosts are alive
# Usage: alive2.sh 192.168.1.7-192.168.1.123
# Function to display usage
usage() {
echo "Usage: $0 start_ip-end_ip"
exit 1
}
# Check if the correct number of arguments are provided
if [ "$#" -ne 1 ]; then
usage
fi
# Validate IP range format
if [[ ! "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}-([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "Invalid IP range format."
usage
fi
start_ip=${1%-*}
end_ip=${1#*-}
# Convert IP to integer
ip_to_int() {
local a b c d
IFS=. read -r a b c d <<< "$1"
echo $((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))
}
int_to_ip() {
local ip dec=$1
for e in {3..0}; do
((octet = dec / (256 ** e)))
((dec -= octet * 256 ** e))
ip+=$octet.
done
echo "${ip%.}"
}
if [ "$(ip_to_int "$start_ip")" -gt "$(ip_to_int "$end_ip")" ]; then
echo "Start IP must be less than or equal to end IP."
exit 1
fi
# Iterate through IP addresses
current_ip=$(ip_to_int "$start_ip")
end_ip_int=$(ip_to_int "$end_ip")
while [ "$current_ip" -le "$end_ip_int" ]; do
host=$(int_to_ip "$current_ip")
ping -c2 "$host" &> /dev/null
if [ $? -eq 0 ]; then
echo "$host is UP"
else
echo "$host is DOWN"
fi
((current_ip++))
done
in terminal :
cd /path/to/your/directory
git init
git add name_of_script
git commit -m "Initial commit of name_of_script"
git remote add origin https://github.com/mohammad/network-check.git
git push -u origin master
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment