This project looks at various tasks that can be accomplished using bash scripting.
Most of the examples are taken from the course exercises of Mastering Linux: The Comprehensive Guide.
The database backup script, db-backup-script.sh, is one I use to backup my personal database.
-
bulk-image-downloader.sh
This script loops through using a counter and downloads images using curl from a specified url.
The script breaks out of the loop when the number of "notfound" images reaches 10.curl --fail "${url/\%\%/$counter}" -o "image-${counter}.jpg" -
city-weather.sh
This script prompts the user for a City name and then sends a request to a weather API
to fetch the current temperature in degrees Celsius.
The temperature is then converted to Fahrenheit.
Both temperatures are then displayed back to the user.curl 'https://downloads.codingcoursestv.eu/055%20-%20bash/api/api.php' -G --data-urlencode "city=${city}" -o "${city_no_blanks}_data.json" -L -s -
db-backup-script.sh
This script takes a backup of the database. It pulls in database credentials from environment variables.
Then finally removes backups older than 15 days.mysqldump -u$DB_USER_1 -p$DB_PASSWORD_1 $DB_NAME_1 > ${BACKUP_DIR_1}${DB_NAME_1}-${BACK_UP_1}-${TIMESTAMP}.sql find $BACKUP_DIR -type f -name "*.sql" -mtime +15 -exec rm {} \; -
dialog-input-box.sh
This script prompts the user for a name using a dialog box.
The name is then appended to a text file and the user is
presented options to either enter the next name or quit.
Finally, the text file is printed on screen.name=$(dialog --keep-tite --title 'Important input' --backtitle 'Course signup' --inputbox 'Please enter the name: ' 0 0 2>&1 >/dev/tty)Note, dialog has to be pre-installed for this to work
sudo apt-get update && sudo apt-get install dialog or dnf install dialog -
directory-monitor.sh
This script allows us to easily monitor the size of directories The directories are passed in as arguments to the script Then for each of those directories, we want to:
- Calculate the total size of the directory (with the du program)
- Then, check if this size is above a defined threshold (5 megabytes)
- If this is the case, we want to print out the size of this directory
read -r size rest < <(du -sm "${folder}")Invoking the script with arguments:
./directory-monitoring.sh -d Krausen-and-Maentz -d iptables-terraform -d Practice -
download-file.sh
This script downloads and analyzes an eBook and outputs the finding on screen.
The tasks include:
-
Downloads eBook file - but only when needed!
i.e. if the file doesn't exist in a specified directory -
And then, calculates how many lines contain the word "love" atleast 1 time
i. It should print error messages to stderr
ii. Example of printing to stderr
echo 'I am an error message' >&2iii. It should exit with the correct exit code on error
i.e. if an error occurs then a non-zero exit code should be set
This can be done with a file that sets an exit code
curl https://downloads.codingcoursestv.eu/055%20-%20bash/if/romeo.txt -o romeo.txt -s -
Downloads eBook file - but only when needed!
-
image-thumbnail-processor.sh
This script processes all images in a directory:
Checks if a thumbnail already exists, If it does, skip generating a new thumbnail
check, if it needs to generate a thumbnail at all
(meaning that width / height of the image is greater than 100 pixels)
Only then, a thumbnail should be generated.width=$(identify -format '%w' "${filename}") height=$(identify -format '%h' "${filename}") convert "${filename}" -resize 100x100 "${thumbnail_filename}" -
trivia-quiz.sh
This script creates a function load_question
When invoked, load_question should load the trivia.json file,
determine how many questions are in it, and provide a random question
Example:
What did the first moving picture depict?- A galloping horse (correct response)
- A woman in a dress
- A man walking
- A crackling fire
Note jq must be installed prior to running the script
sudo apt-get update && sudo apt-get install jq