/cnc/img/learning-with-gpt.png

BASH Challenges: File Analysis

Welcome to our BASH series, where we focus on improving our understanding and skills related to bash scripting with the help of OpenAI’s latest and greatest GPT-4 technology. In this series, we will explore various challenges and tasks that can push us to new heights of understanding and proficiency in writing efficient and effective scripts in the BASH language.

By leveraging the power of GPT-4, we can receive challenges and tasks that are tailored to our level of expertise and designed to help us grow and improve our abilities. Through this series, we will share our experience of using GPT-4 to tackle these challenges and provide practical tips, tricks, and insights that can help you become a more proficient BASH scripter.

Whether you’re a beginner or an experienced user, we invite you to join us on this journey to master the art of BASH scripting with the help of OpenAI’s GPT-4 technology. Let’s see what we can accomplish together!

Bash Scripting Challenge 1

Challenge 1: File Information
Difficulty: Easy
Requirements: Create a Bash script that accepts a file as an argument. The script should print the file's name, size in bytes, and the time it was last modified. Make sure to handle the case where the file does not exist or the argument is not provided.

Breaking The Challenge Into Pieces

The best way to solve challenges like this is to break each part of the challenge up into parts. After analysing the challenge requirements, we come up with the following list of steps:

  • Script must accept file as argument
  • Script must handle the case where the file does not exist or the argument is not provided
  • print the file’s name, size in bytes, and the time it was last modified

Crafting the Solution

With the challenge broken down into smaller tasks, let’s start crafting our solution, step by step, while explaining how and why each piece works.

Accepting a File as an Argument

The first task is for our script to accept a file as an argument. In Bash scripting, we achieve this through the use of positional parameters, which are special variables that hold the arguments provided to the script when it’s run. The variable $1 corresponds to the first argument passed to the script. By assigning FILENAME=$1, we store the name of the file provided as an argument in the FILENAME variable for later use.

bash
FILENAME=$1
  • Script must accept file as argument
  • Script must handle the case where the file does not exist or the argument is not provided
  • print the file’s name, size in bytes, and the time it was last modified

Nonexistent File or Missing Argument

The next task involves error handling. We need to ensure our script can handle situations where the file doesn’t exist or no argument was provided.

For the first part, if no argument is provided, the special variable $# can help. It holds the number of arguments supplied to the script. If $# is equal to 0, it means no arguments were provided. We can check this with an if statement and output an appropriate message to the user.

bash
if [ $# -eq 0 ];
then
echo "No file was provided!"
exit 1
fi

The second part of error handling requires checking if the provided file exists and is readable. In Bash, the -f operator checks if a file exists and is a regular file, and the -r operator checks if the file is readable. If either check fails, we inform the user and exit the script.

bash
if [ ! -f $FILENAME ] || [ ! -r $FILENAME ];
then
echo "The file you've provided either does not exist or is unreadable: $FILENAME"
exit 1
fi
  • Script must accept file as argument
  • Script must handle the case where the file does not exist or the argument is not provided
  • print the file’s name, size in bytes, and the time it was last modified

Displaying File Information

The final task is to display the name of the file, its size in bytes, and the time it was last modified. We can achieve this by using various built-in utilities in Bash.

To get the file’s size in bytes, we use the du command with the -b option. The du command estimates file and directory space usage, and the -b option tells it to print the size in bytes.

bash
SIZE_IN_BYTES=$(du -b "$FILENAME")

To get the time the file was last modified, we use the stat command with the -c %y option. The stat command displays file or filesystem status, and the -c %y option tells it to print the time of last modification.

bash
LAST_MODIFICATION=$(stat -c %y "$FILENAME")

Then we simply echo out the file’s name, size, and modification time.

bash
echo "FILE Name: $FILENAME"
echo "$FILENAME Size in Bytes: $SIZE_IN_BYTES"
echo "$FILENAME Was last modified: $LAST_MODIFICATION"

By combining these steps, we construct a Bash script that effectively meets the challenge requirements.

  • Script must accept file as argument
  • Script must handle the case where the file does not exist or the argument is not provided
  • print the file’s name, size in bytes, and the time it was last modified
bash
#! /bin/bash
# Day 2, Challenge 1: Create a Bash script that accepts a file as an argument. The script should print the file's name,
# size in bytes, and the time it was last modified. Make sure to handle the case where the file does not exist or the
# argument is not provided.
# First Step: Was the file passed as an argument?
if [ $# -eq 0 ];
then
echo "No file was provided!"
exit 1
fi
# Capture the File in a variable $1
FILENAME=$1
# Next, we need to test to ensure that this file exists in the current directory and is a regular file
if [ ! -f $FILENAME ] || [ ! -r $FILENAME ];
then
echo "The file you've provided either does not exists or is unreadable: $FILENAME"
exit 1
fi
# At this stage in the script, if still running, we now need to provide the information about the file.
echo "***********************NOW ANALYZING FILE************************************"
sleep 5s
echo "**********************ANALYSIS IS COMPLETE!***********************************"
echo "**********************RESULTS:************************************************"
SIZE_IN_BYTES=$(du -b "$FILENAME")
LAST_MODIFICATION=$(stat -c %y "$FILENAME")
echo "FILE Name: $FILENAME"
echo "$FILENAME Size in Bytes: $SIZE_IN_BYTES"
echo "$FILENAME Was last modified: $LAST_MODIFICATION"
exit 0

GPT-4 Critique and Improved Script

The whole purpsose of these writeups is to highlight the ability for GPT to be used as a tool for learning. After submitting my script to GPT, it replied with the following review of the script and even offered a more efficient version and explaination for the changes it recommended.

GPT-4: Great job! Your Bash script is well-written and does exactly what was required in the challenge. You've correctly managed the case where no file is provided or the file does not exist. Furthermore, you've used the du and stat commands effectively to get the file size and last modification time. Here's a slightly optimized version of your script:
bash
#!/bin/bash
# Exit if no argument was provided
if [ $# -eq 0 ]; then
echo "No file was provided!"
exit 1
fi
FILENAME=$1
# Exit if the file does not exist or is not readable
if [ ! -f $FILENAME ] || [ ! -r $FILENAME ]; then
echo "The file you've provided either does not exist or is unreadable: $FILENAME"
exit 1
fi
# Get the file size in bytes and the last modification time
SIZE_IN_BYTES=$(stat -c%s "$FILENAME")
LAST_MODIFICATION=$(stat -c%y "$FILENAME")
echo "*************************FILE ANALYSIS RESULTS*****************************"
echo "File Name: $FILENAME"
echo "Size in Bytes: $SIZE_IN_BYTES"
echo "Last Modified: $LAST_MODIFICATION"

The changes include:

  1. Use of stat -c%s to directly get the size in bytes, which is more accurate and faster than du -b.
  2. Removal of the sleep command, which isn’t necessary and delays the output. You typically want your scripts to run as quickly as possible, especially if they’re part of a larger system.
  3. Simplified output messages for readability.