1
0
mirror of https://github.com/bregman-arie/devops-exercises.git synced 2025-07-15 23:54:32 +02:00

Rename exercises dir

Name it instead "topics" so it won't be
strange if some topics included "exercises" directory.
This commit is contained in:
abregman
2022-08-02 01:51:39 +03:00
parent ea1d94d67b
commit 99c4e02ecf
235 changed files with 283 additions and 74 deletions

View File

@ -1,13 +0,0 @@
## Basic Date
### Objectives
1. Write a script that will put the current date in a file called "the_date.txt"
### Solution
```
#!/usr/bin/env bash
echo $(date) > the_date.txt
```

View File

@ -1,24 +0,0 @@
## Count Chars
### Objectives
1. Read input from the user until you get empty string
2. For each of the lines you read, count the number of characters and print it
### Constraints
1. You must use a while loop
2. Assume at least three lines of input
### Solution
```
#!/usr/bin/env bash
echo -n "Please insert your input: "
while read line; do
echo -n "$line" | wc -c
echo -n "Please insert your input: "
done
```

View File

@ -1,30 +0,0 @@
## Directories Comparison
### Objectives
1. You are given two directories as arguments and the output should be any difference between the two directories
### Solution
Suppose the name of the bash script is ```dirdiff.sh```
```
#!/bin/bash
if test $# -ne 2
then
echo -e "USAGE: ./dirdiff.sh directory1 directory2"
exit 1
fi
# check for the checksums.
# If both the checksums same, then both directories are same
if test `ls -1 $1 | sort | md5sum | awk -F " " '{print $1}'` == `ls -1 $2 | sort | md5sum | awk -F " " '{print $1}'`
then
echo -e "No difference between the 2 directories"
exit 0
fi
diff -q $1 $2
```

View File

@ -1,20 +0,0 @@
## Empty Files
### Objectives
1. Write a script to remove all the empty files in a given directory (including nested directories)
### Solution
```
#! /bin/bash
for x in *
do
if [ -s $x ]
then
continue
else
rm -rf $x
fi
done
```

View File

@ -1,20 +0,0 @@
## Shell Scripting - Factors
### Objectives
Write a script that when given a number, will:
* Check if the number has 2 as factor, if yes it will print "one factor"
* Check if the number has 3 as factor, if yes it will print "one factor...actually two!"
* If none of them (2 and 3) is a factor, print the number itself
### Solution
```
#!/usr/bin/env bash
(( $1 % 2 )) || res="one factor"
(( $1 % 3 )) || res+="...actually two!"
echo ${res:-$1}
```

View File

@ -1,17 +0,0 @@
## Files Size
### Objectives
1. Print the name and size of every file and directory in current path
Note: use at least one for loop!
### Solution
```
#!/usr/bin/env bash
for i in $(ls -S1); do
echo $i: $(du -sh "$i" | cut -f1)
done
```

View File

@ -1,15 +0,0 @@
## Great Day
### Objectives
1. Write a script that will print "Today is a great day!" unless it's given a day name and then it should print "Today is <given day>"
Note: no need to check whether the given argument is actually a valid day
### Solution
```
#!/usr/bin/env bash
echo "Today is ${1:-a great day!}"
```

View File

@ -1,15 +0,0 @@
## Shell Scripting - Hello World
### Objectives
1. Define a variable with the string 'Hello World'
2. Print the value of the variable you've defined and redirect the output to the file "amazing_output.txt"
### Solution
```
#!/usr/bin/env bash
HW_STR="Hello World"
echo $HW_STR > amazing_output.txt
```

View File

@ -1,20 +0,0 @@
## It's Alive!
### Objectives
1. Write a script to determine whether a given host is down or up
### Solution
```
#!/usr/bin/env bash
SERVERIP=<IP Address>
NOTIFYEMAIL=test@example.com
ping -c 3 $SERVERIP > /dev/null 2>&1
if [ $? -ne 0 ]
then
# Use mailer here:
mailx -s "Server $SERVERIP is down" -t "$NOTIFYEMAIL" < /dev/null
fi
```

View File

@ -1,26 +0,0 @@
## Number of Arguments
### Objectives
* Write a script that will print "Got it: <argument value>" in case of one argument
* In case no arguments were provided, it will print "Usage: ./<program name> <argument>"
* In case of more than one argument, print "hey hey...too many!"
### Solution
```
#!/usr/bin/env bash
set -eu
main() {
case $# in
0) printf "%s" "Usage: ./<program name> <argument>"; return 1 ;;
1) printf "%s" "Got it: $1"; return 0 ;;
*) return 1 ;;
esac
}
main "$@"
```

View File

@ -1,30 +0,0 @@
## Sum
### Objectives
1. Write a script that gets two numbers and prints their sum
3. Make sure the input is valid (= you got two numbers from the user)
2. Test the script by running and passing it two numbers as arguments
### Constraints
1. Use functions
### Solution
```
#!/usr/bin/env bash
re='^[0-9]+$'
if ! [[ $1 =~ $re && $2 =~ $re ]]; then
echo "Oh no...I need two numbers"
exit 2
fi
function sum {
echo $(( $1 + $2 ))
}
sum $1 $2
```