ChatGPT code generated on openai.com

I created an openai account today and waded into the waters of ChatGPT. Naturally, I had to ask it a bash programming question. Below is a screen shot of the conversation. It took about 10 seconds to generate the code. I was very impressed. Crazy good as a matter of fact.

The comamnd I gave it was:

Please generate a bash code to send a text message after the successful completion of a crontab command.

Here is the code:

#!/bin/bash

# Command to be run by crontab
crontab_command="your_crontab_command"

# Send a text message after the command has completed
function send_text_message {
  # Replace "your_phone_number" and "your_message" with the appropriate values
  curl -X POST "https://api.twilio.com/2010-04-01/Accounts/your_account_sid/Messages.json" \
  -d "To=your_phone_number" \
  -d "From=your_twilio_number" \
  -d "Body=your_message" \
  -u your_account_sid:your_auth_token
}

# Run the crontab command
$crontab_command

# Check the exit status of the command
if [ $? -eq 0 ]; then
  # Send the text message if the command was successful
  send_text_message
fi

1 Like

Here is a PHP question. It took maybe 20 seconds to generate this code:

Generate php code to name a photo based on gps coordinates found in exit data.

<?php

// Path to the photo
$photo_path = 'your_photo_path.jpg';

// Get the EXIF data from the photo
$exif = exif_read_data($photo_path);

// Check if the EXIF data contains GPS coordinates
if (isset($exif['GPSLatitude']) && isset($exif['GPSLongitude'])) {
  // Convert the GPS coordinates to decimal format
  $latitude = $exif['GPSLatitude'][0] + ($exif['GPSLatitude'][1] / 60) + ($exif['GPSLatitude'][2] / 3600);
  $longitude = $exif['GPSLongitude'][0] + ($exif['GPSLongitude'][1] / 60) + ($exif['GPSLongitude'][2] / 3600);

  // Check if the latitude is South of the equator
  if (strtolower($exif['GPSLatitudeRef']) == 's') {
    $latitude = -$latitude;
  }

  // Check if the longitude is West of the prime meridian
  if (strtolower($exif['GPSLongitudeRef']) == 'w') {
    $longitude = -$longitude;
  }

  // Format the GPS coordinates as a string
  $gps_coordinates = sprintf("%01.6f,%01.6f", $latitude, $longitude);

  // Get the file name and extension of the photo
  $path_parts = pathinfo($photo_path);
  $file_name = $path_parts['filename'];
  $file_ext = $path_parts['extension'];

  // Generate the new file name based on the GPS coordinates
  $new_file_name = $file_name . '_' . $gps_coordinates . '.' . $file_ext;

  // Rename the photo with the new file name
  rename($photo_path, $new_file_name);
}

Note: it recognized that I called EXIF data “exit” data in the question. It still generated the correct code!

1 Like

Here is a GO language question:

Generate code in the GO language to query results from a table named “photos” where GPS coordinates are within 100 meters of a target set of coordinates.

It took 11 seconds to generate this code:


package main

import (
    "database/sql"
    "fmt"
    "log"

    // Import the database driver
    _ "github.com/go-sql-driver/mysql"
)

// Target GPS coordinates
const latitude = 40.7128
const longitude = -74.0060

// Maximum distance in meters
const maxDistance = 100

func main() {
    // Connect to the database
    db, err := sql.Open("mysql", "user:password@tcp(host:port)/database")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // Query the photos table for coordinates within 100 meters of the target coordinates
    rows, err := db.Query("SELECT * FROM photos WHERE (6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) <= ?", latitude, longitude, latitude, maxDistance)
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()

    // Iterate over the query results
    for rows.Next() {
        var id int
        var latitude float64
        var longitude float64
        err = rows.Scan(&id, &latitude, &longitude)
        if err != nil {
            log.Fatal(err)
        }

        // Print the results
        fmt.Println(id, latitude, longitude)
    }
    err = rows.Err()
    if err != nil {
        log.Fatal(err)
    }
}


1 Like

THANK YOU for posting this, i’m all over the map with how I feel about ai code generation and i’m very curious about it.

I can’t speak to PHP or Go but I see a lot of other people’s BASH on a weekly basis and that syntax looks normal.

Problem is “normal” looks like [ $? -eq 0 ] which is only built into BASH to make POSIX scripts run faster. BASH scripts are supposed to use [[ $? == 0 ]] which is expression based and removes all the time cost of shell interpretation along with the danger that comes with it if anything’s not quoted or escaped correctly.

Normal is also gratuitous use of double-quotes for text not containing anything that needs interpretation.

On the other hand…

Damn is that ever an incredible tool for discovering software, services, APIs and so on and that’s especially important for BASH because it tends to be a glue between those things so you really have to know what’s out there to get the most out of it.

It’s also nice to just get a starter template. It’s one thing to have generic ones but with AI you can start with anything.

Impressive.

Well, I had to take chapGPT to the woodshed today. After asking it a question to supply php code to parse the URI of a server request, it supplied an invalid answer. Here is the tail end of our convo:

Me:

Are you certain the “/page_name” attached to the end of a domain is testable with $_SERVER[HTTP_HOST]?

ChatGPT:

No, I apologize for the mistake in my earlier response. The $_SERVER['HTTP_HOST'] variable only contains the domain name, and does not include any additional path or query string information. To test for the presence of /page_name in the request path, you can use the $_SERVER['REQUEST_URI'] variable instead.

As of Star Date May 10, 2023 there is still some work to be done on this AI. And still a place for Homo sapiens like myself.