How to Build a Bash Application by using ChatGPT AI

Endrit Qerreti

Endrit Qerreti

I was thinking of creating a simple bash script that will check a path and look for a file, and if found the script should delete it, but then I thought why not give ChatGPT's AI a try and see what it can do.

Have you ever wondered if AI can code an application from scratch? Let's find out the answer together in this article.

I asked ChatGPT to write me a bash script that would delete a file after 3 days, and this is what ChatGPT replied with:

Bash Script that Deletes a file every 3 days

#!/bin/bash

filename="/path/to/your/file"

while true
do
  # Check if file exists
  if [ -f "$filename" ]; then
    # Delete file
    rm "$filename"
    echo "File deleted: $filename"
  else
    echo "File not found: $filename"
  fi

  # Sleep for 3 days
  sleep 259200
done

The code looks simple, it asks you to define the path of the file that you want deleted, and if deleted it prompts with 'File deleted' and with 'File not found' if the file wasn't found, once the code runs it sleeps for 3 days. However, what if you need to delete a '.log' file that keeps being created in the same path? Let's ask ChatGPT again and see what AI comes with this time:

#!/bin/bash

# Set the directory path to search for .log files
LOG_DIR="/path/to/logs"

# Delete all .log files in the directory
find "$LOG_DIR" -type f -name "*.log" -delete

# Save this script as a file, for example "log_cleanup.sh"
# Make the script executable with the following command:
# chmod +x log_cleanup.sh

# Set up a cron job to run the script every 3 days
# Edit the crontab file with the following command:
# crontab -e
# Add the following line to run the script at 1:00 AM every 3 days:
# 0 1 */3 * * /path/to/log_cleanup.sh >/dev/null 2>&1

# The above cron job will run the script every 3 days at 1:00 AM, 
# and redirect all output to /dev/null to suppress any messages.

So now we have a script similar to the first one, the difference on this code is that the script will delete all .log files in a given path.  Not only it wrote you another example of how you can achieve this task, but it also commented the code to explain what each line does.

Instead of using sleep parameter, this time ChatGPT suggested to use cron to run script every 3 days.

0 1 */3 * * /path/to/log_cleanup.sh >/dev/null 2>&1

The cron line above will run the script called 'log_cleanup.sh' every 3 days  at 1:00 AM and redirect all output to /dev/null.

So now we have a little more advanced script compared to the first one, instead of looking for a custom file to delete, now we can delete all files that end with .log by using the find command, also in the second example ChatGPT uses cron to run the code every 3 days and not the sleep command as shown on the first example.

#!/bin/bash

# Set the directory path to search for .log files
LOG_DIR="/path/to/logs"

# Delete all .log files in the directory
find "$LOG_DIR" -type f -name "*.log" -delete

# Send an email notification when the script finishes
echo "The log cleanup script has finished running." | mail -s "Log Cleanup Script Notification" your-email@example.com

# Save this script as a file, for example "log_cleanup.sh"
# Make the script executable with the following command:
# chmod +x log_cleanup.sh

# Set up a cron job to run the script every 3 days
# Edit the crontab file with the following command:
# crontab -e
# Add the following line to run the script at 1:00 AM every 3 days:
# 0 1 */3 * * /path/to/log_cleanup.sh >/dev/null 2>&1

# The above cron job will run the script every 3 days at 1:00 AM, 
# and redirect all output to /dev/null to suppress any messages.

Ok so, let's try to add a few more options to this script.

This time I'm going to ask ChatGPT to notify my via email when script runs and deletes the log files.

And this is the solution ChatGPT came with :

#!/bin/bash

# Set the directory path to search for .log files
LOG_DIR="/path/to/logs"

# Delete all .log files in the directory
find "$LOG_DIR" -type f -name "*.log" -delete

# Send an email notification when the script finishes
echo "The log cleanup script has finished running." | mail -s "Log Cleanup Script Notification" your-email@example.com

# Save this script as a file, for example "log_cleanup.sh"
# Make the script executable with the following command:
# chmod +x log_cleanup.sh

# Set up a cron job to run the script every 3 days
# Edit the crontab file with the following command:
# crontab -e
# Add the following line to run the script at 1:00 AM every 3 days:
# 0 1 */3 * * /path/to/log_cleanup.sh >/dev/null 2>&1

# The above cron job will run the script every 3 days at 1:00 AM, 
# and redirect all output to /dev/null to suppress any messages.

ChatGPT added mail utility in the code, an application that allows you to send an email from your terminal. So far so good but does it work? NO..well at least not yet. So how do we fix this issue?

Mail -s is just a command, to be able to use this command you must have installed mailutils package on your system.

To install mailutils run the command below

sudo apt install mailutils

How about now..can I run the script now? The script will run fine, but the email part will not work since the ssmtp.conf file is not configured.

Assuming you'll be using gmail to send emails, then this is how the ssmtp.conf configuration should look like

root=postmaster
mailhub=smtp.gmail.com:587
hostname=oht
AuthUser=your-email-address
AuthPass=password
FromLineOverride=YES
UseSTARTTLS=YES

Replace email address with your own email address and password with your password, once done copy paste the configuration above into ssmtp.conf file which is located on

/etc/ssmtp/ssmtp.conf

So now we have the mail configured correctly, it's time to test the application

Step 1 - Create log_cleanup.sh

Create the bash script file with any text editor you like, the file should be called log_cleanup.sh

Next, copy the whole code as shown on the second example and save it into this file.

Step 2 - Make log_cleanup.sh executable

Open terminal and run the following command below

chmod +x log_cleanup.sh

This command should be executed on the same path where you created the log_cleanup.sh script.

Step 3 - Set up crontab

Since the script will run every 3 days, we need to create a cronjob for this task. To do this simply type the crontab - e command

crontab -e

And copy the line below into the cron file

0 1 */3 * * /path/to/log_cleanup.sh >/dev/null 2>&1

And crontab should look like this

Next, save the file by pressing CTRL +X.

Step 4 - Test if code works

Once you have made the bash script executable and set up the cronjob, to make sure the app will work you can run the log_cleanup.sh manually, simply type the command below and the app should run

./log_cleanup.sh

And you should receive the email notification as shown on the image below

That's it!

Conclusion

In this tutorial you learned how to build a bash application by using ChatGPT.  ChatGPT is a great tool no doubts especially when it comes to coding as it comments everything for you to understand it better.