amine's

Hey there!

Are you someone who’s always looking for ways to be more efficient and save time? Well, then bash scripts are definitely for you. And what’s even better than writing your own bash scripts? Having an AI write them for you, of course!

Enter chatGPT, the language model developed by OpenAI that can generate text that’s almost indistinguishable from human-written stuff. Not only has chatGPT been used to create all sorts of things, from articles and stories to poems and even music, but it can also be used to write scripts that can automate all sorts of tasks.

For example, take a look at the bash script we have for you today. This script can turn a bunch of images into a single PDF file using ImageMagick, and all you have to do is specify the directory containing the images. It’s the perfect solution for lazy people who want to save time and effort (and let’s be real, who doesn’t?).

So the next time you find yourself needing to automate a task, why not let chatGPT do the heavy lifting? You’ll be amazed at how much time and energy you can save. Thanks for reading!

Now here’s a little youtube-ish plot twist for you, the previous introduction was generated by chatGPT himself.

the prompts

1-

write me a bash script that generates a pdf file from images in a folder using imagemagick

#!/bin/bash

# Check if ImageMagick is installed
if ! command -v convert > /dev/null 2>&1; then
  echo "Error: ImageMagick is not installed. Please install it and try again."
  exit 1
fi

# Prompt for directory
echo "Enter the path to the directory containing the images: "
read directory

# Check if the specified directory exists
if [ ! -d "$directory" ]; then
  echo "Error: The specified directory does not exist."
  exit 1
fi

# Generate PDF
convert "$directory"/*.jpg "$directory"/output.pdf

echo "PDF generated successfully."

2-

rewrite the script to detect the linux distribution that’s running and install imgaemagick if it’s missing

#!/bin/bash

# Check if ImageMagick is installed
if ! command -v convert > /dev/null 2>&1; then
  # Detect Linux distribution
  if [ -f /etc/os-release ]; then
    # Parse /etc/os-release to get the ID field
    distro_id=$(awk -F= '/^ID/{print $2}' /etc/os-release | tr -d '"')
  else
    # If /etc/os-release is not present, fall back to lsb_release
    distro_id=$(lsb_release -i | awk '{print $3}')
  fi

  # Install ImageMagick based on the detected Linux distribution
  case $distro_id in
    ubuntu|debian)
      sudo apt-get update
      sudo apt-get install -y imagemagick
      ;;
    centos|fedora)
      sudo yum install -y imagemagick
      ;;
    arch)
      sudo pacman -S imagemagick
      ;;
    *)
      echo "Error: Unable to detect Linux distribution. Please install ImageMagick manually."
      exit 1
      ;;
  esac
fi

# Prompt for directory
echo "Enter the path to the directory containing the images: "
read directory

# Check if the specified directory exists
if [ ! -d "$directory" ]; then
  echo "Error: The specified directory does not exist."
  exit 1
fi

# Generate PDF
convert "$directory"/*.jpg "$directory"/output.pdf

echo "PDF generated successfully."

3-

rewrite the script to include the top 10 distributions from distro watch

#!/bin/bash

# Check if ImageMagick is installed
if ! command -v convert > /dev/null 2>&1; then
  # Detect Linux distribution
  if [ -f /etc/os-release ]; then
    # Parse /etc/os-release to get the ID field
    distro_id=$(awk -F= '/^ID/{print $2}' /etc/os-release | tr -d '"')
  else
    # If /etc/os-release is not present, fall back to lsb_release
    distro_id=$(lsb_release -i | awk '{print $3}')
  fi

  # Install ImageMagick based on the detected Linux distribution
  case $distro_id in
    ubuntu|debian)
      sudo apt-get update
      sudo apt-get install -y imagemagick
      ;;
    centos|fedora|redhat|rhel)
      sudo yum install -y imagemagick
      ;;
    arch|manjaro)
      sudo pacman -S imagemagick
      ;;
    solus)
      sudo eopkg install imagemagick
      ;;
    openSUSE|suse)
      sudo zypper install imagemagick
      ;;
    gentoo)
      sudo emerge --ask imagemagick
      ;;
    mageia)
      sudo urpmi imagemagick
      ;;
    void)
      sudo xbps-install -S imagemagick
      ;;
    slackware)
      sudo slackpkg install imagemagick
      ;;
    *)
      echo "Error: Unable to detect Linux distribution. Please install ImageMagick manually."
      exit 1
      ;;
  esac
fi

# Prompt for directory
echo "Enter the path to the directory containing the images: "
read directory

# Check if the specified directory exists
if [ ! -d "$directory" ]; then
  echo "Error: The specified directory does not exist."
  exit 1
fi

# Generate PDF
convert "$directory"/*.jpg "$directory"/output.pdf

echo "PDF generated successfully."

As you can see, the bot “remembers”, you can always tell it to go back and change something for you, it also included a useful explanation to what it’s doing every time you gave it a new prompt. Writing the introduction to his blog post went through the same process; “hey bot write me a introduction to a blog post about this script”, “make it less blog-ish”, “make it a little funnier”, “can you mention how lazy people love shell scripts, and how having an AI write you a shell script is the laziest thing ever”

PS: I know the script isn’t that useful and you can easily achieve this with a one liner, but I just wanted to try this, maybe in the future I’ll make the bot do more interesting things, I did make it generate a javascript that displays a random line on the top of this navbar every time you refresh the page.