I asked ChatGPT to write me a bash script, and it worked (mostly), why do I need to know how to code?

By now, ChatGPT has become pretty well known ( as of 18th Dec 2022). I’ve messed around with basic questions, but today I wanted to start to write a script that I could use with “OverSight” on Mac (https://objective-see.org/products/oversight.html). When you turn on your camera/mic, it can fire off a script with arguments.

In this post I’ll show you what I asked ChatGPT, what it gave me and if it worked! And if Google helped me or not, and if learning to code (or just knowing logic), is still worthwhile.

It’s been a while since I needed to work with bash script parameters, and instead of Googling for some boiler plate, I asked:

write a mac shell script that accepts 3 arguements, -d, -event and -process

And off it went…

To write a shell script that accepts three arguments, you can use the following template: 
Copy code 
# ! /bi n/bash 
# Check 
if C 
echo 
echo 
if the script is being run with the correct number of arguments 
-ne 3 ] ; 
then 
"Error: incorrect number of arguments" 
"Usage: scriptname -d <directory> -event <eventname> -process 
<processname>" 
exit 1 
# Parse the 
while C 
case "$1" 
-d) 
arguments 
-gt J; 
do 
in 
di rectory= 
shift 
2 
-event) 
shift 
2 
-process) 
process= 
shift 
2 
echo "Error: 
exit 1 
invalid option $1" 
O Regenerate response 
ChatGPT Dec 15 Version. Free Research Preview. Our goal is to make Al systems more natural and safe to interact with. Your feedback will help us improve.

The full unedited code it generated is below.

#!/bin/bash

# Check if the script is being run with the correct number of arguments
if [ $# -ne 3 ]; then
  echo "Error: incorrect number of arguments"
  echo "Usage: scriptname -d  -event  -process "
  exit 1
fi

# Parse the arguments
while [ $# -gt 0 ]; do
  case "$1" in
    -d)
      directory="$2"
      shift 2
      ;;
    -event)
      event="$2"
      shift 2
      ;;
    -process)
      process="$2"
      shift 2
      ;;
    *)
      echo "Error: invalid option $1"
      exit 1
      ;;
  esac
done

# Check if the required arguments have been provided
if [ -z "$directory" ] || [ -z "$event" ] || [ -z "$process" ]; then
  echo "Error: one or more required arguments are missing"
  echo "Usage: scriptname -d  -event  -process "
  exit 1
fi

# Your code goes here

echo "Directory: $directory"
echo "Event: $event"
echo "Process: $process"

First of all, it assumed -d meant directory, so is inferring things…

Next, I copied it out to a text editor, saved it as oversight.sh, and ran it.

Oops. So, I removed the validation code from the top and bottom just to see whats what, and bingo.

So it seems that for whatver reason (I’ve not debugged it, yet, if you see whats wrong please feel free to comment below!), the validation didnt work, BUT, the main body of the code was exactly what I was looking for.

That being said, I did skip over something

So… that wasn’t quite the whole story. When I got the first error, I didn’t just cut out validation and away I went…

I decided, screw this, let’s go and find a “proper” answer from the internet.

14.4 million hits returned in 0.48 seconds. Why I care about how long the query took is beyond me… but anyway.

I visited a good chunk of the top results, and the code examples they gave were all ones that would technically work. However, they weren’t quite answering my question. None used parameter names. So I had to search that bit out with another query, and another set of 4-5 blogs/websites.

5-10 minutes and 10 different ways to achieve the same thing later, I went back to my ChatGPT generated script and removed the validation, and bingo, it worked.

Whats the moral of the story here? Do I need to know how to code?

OpenAI is onto something super amazing, we can all agree on that. Terrifying, but still amazing. In this instance, my usual workflow would be search for some boilerplate to start with, then coding my specific use case into it can take a bit of time (I’m not a full time coder).

With ChatGPT, “write a mac shell script that accepts 3 arguements, -d, -event and -process” with arguments spelled wrong, returned me a 90% perfect boilerplate with the starter for 10 use case already done.

Could I have used this out of the box without having some former experience, I doubt it. If I didn’t know how to do basic troubleshooting, I’m still out of luck. BUT, with a little bit of experience, this can be a super fantastic assistant to get you started.

So, do I need to know how to code? I’d say yes, having a grasp of core concepts (computing logic, troubleshooting etc) is still key. Do I need to keep even less in my head than before, perhaps.

When ChatGPT can read the internet and learn from Stack Overflow’s accepted answers… good lord.

I just thought, why didn’t I ask ChatGPT why it wasn’t working… one for another day maybe.

Leave a Reply

Your email address will not be published. Required fields are marked *