avatarCasey Whittaker

Summary

The webpage discusses the use of ChatGPT to create practical shell scripts for enhancing productivity and streamlining workflows, specifically focusing on scripts for cleaning up files, setting an alarm, and listing running applications.

Abstract

The webpage titled "Interesting Ways I Use ChatGPT to Create Shell Scripts" showcases the author's experience using ChatGPT to create shell scripts for improving productivity. The author shares three shell scripts that address common challenges faced by developers: a script to delete specific file types in a directory, a script to set an alarm using a sound file, and a script to list currently running applications for monitoring system performance. The author emphasizes the boundless capabilities of ChatGPT and encourages others to explore its potential for enhancing their workflows.

Opinions

  • The author believes that ChatGPT has almost boundless capabilities to enhance productivity and streamline workflows.
  • The author finds that integrating ChatGPT into their development toolkit unlocks new levels of efficiency and creativity.
  • The author cautions users to be careful when running scripts that delete files, as this action cannot be undone.
  • The author suggests using a sleep delay and playing a sound for creating an alarm using a shell script, which works well for alarms set for a short duration ahead.
  • The author recommends using the osascript command to interact with AppleScript from the terminal to list active applications on macOS.
  • The author encourages users to explore the potential of ChatGPT for automating tasks and enhancing their workflows.
  • The author promotes a cost-effective AI service called ZAI.chat, which provides similar performance and functions to ChatGPT Plus (GPT-4) at a lower price.

Interesting Ways I Use ChatGPT to Create Shell Scripts

ChatGPT has almost boundless capabilities to enhance productivity. The potential for leveraging this AI platform to streamline workflows and optimize day-to-day tasks is truly remarkable. For developers seeking to harness the power of AI to elevate their coding experience, I’ve discovered a particularly effective strategy for maintaining a clean and efficient development environment.

By inputting a few prompts to ChatGPT, I’ve devised a series of innovative and practical shell scripts that significantly reduce clutter and improve navigability within my workspace. These scripts are not just time-savers; they are crafted to address common challenges faced by developers, enabling a focus on creating and less on environment management.

Whether it’s automating routine tasks, organizing files, or swiftly clearing out unnecessary data, the potential applications are vast. By integrating ChatGPT into your development toolkit, you can unlock new levels of efficiency and creativity. Here are a few ways I’ve done just that:

1. Create a Script to Cleanup Files:

ChatGPT prompt: Create a shell script I can run on MacOS to delete any .csv files in my downloads folder

This simple shell script will delete all .csv files in your Downloads folder on macOS. You can interchange .csv with any other type of file extension, but I chose this one because I’ve been working with a lot of CSV data recently. Be cautious when running scripts that delete files, as this action cannot be undone.

  1. Open Terminal: First, you’ll need to open the Terminal app on your Mac.
  2. Create the Script File: You can create a new file using a text editor from the terminal, for example, using nano:
nano delete_csv_downloads.sh

3. Script Content: Copy and paste the following script into the editor:

#!/bin/bash

# Target directory (Downloads folder)
TARGET_DIR="$HOME/Downloads"

# Find and delete .csv files
find "$TARGET_DIR" -name '*.csv' -type f -delete

echo "All CSV files in the Downloads folder have been deleted."

4. Save and Exit: If you’re using nano, press CTRL + O to save the file, then Enter to confirm, and CTRL + X to exit the editor.

5. Make the Script Executable: Before running the script, you need to make it executable. In the Terminal, run:

chmod +x delete_csv_downloads.sh

6. Run the Script: Now, you can run the script with:

./delete_csv_downloads.sh

Important Notes:

  • Ensure you have backups or have verified that no important .csv files are in your Downloads folder before running this script, as it will permanently delete all .csv files found.
  • This script should not require sudo as it operates within your user's Downloads folder. Avoid using sudo for scripts unless necessary, as it can pose a security risk.
  • You can customize the TARGET_DIR variable if you want to target a different directory.

2. Create a Script that Sets an Alarm:

ChatGPT prompt: Create a shell script that creates an alarm for a time specified in minutes

Creating an alarm using a shell script on macOS or Linux can be an interesting way to leverage the power of your operating system’s scheduled tasks (using cron on Linux or launchd on macOS) and sound notifications. However, for simplicity and portability across Unix-like systems, we'll focus on a straightforward script that uses a sleep delay and then plays a sound. This method doesn't require setting up scheduled tasks and works well for alarms set for a short duration ahead.

  1. Choose a Sound File: First, you’ll need a sound file to play as your alarm. Ensure you have a .mp3 or .wav file available on your system that you'd like to use as the alarm sound. For macOS users, the system sounds are a good resource located in /System/Library/Sounds/.
  2. Create the Shell Script: Open your terminal, and let’s create a new shell script:
nano create_alarm.sh

3. Script Content: Copy and paste the following script into your editor:

#!/bin/bash

# Check if the correct number of arguments were passed
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <duration in minutes>"
    echo "Example: $0 10 (sets an alarm 10 minutes from now)"
    exit 1
fi

# Calculate sleep time in seconds
SLEEP_TIME=$(($1 * 60))

# Use a predefined sound or specify the path to your sound file
SOUND_FILE="/System/Library/Sounds/Submarine.aiff" # macOS example
# SOUND_FILE="/path/to/your/sound/file.mp3" # Use this line for a custom sound file

echo "Alarm set for $1 minutes from now."

# Sleep for the specified duration
sleep $SLEEP_TIME

# Play the sound (macOS command shown, adjust for Linux if necessary)
afplay $SOUND_FILE

# For Linux, you might use mpg123 or aplay instead, e.g.,
# mpg123 $SOUND_FILE
# aplay $SOUND_FILE

echo "Your $1 minutes are up!" 

4. Make the Script Executable: After saving the script from the editor (with Ctrl + O, Enter, and Ctrl + X in nano), make it executable:

chmod +x create_alarm.sh

5. Use the Script: Now, you can set an alarm by specifying the duration in minutes. For example, to set an alarm for 15 minutes from now, you’d run:

./create_alarm.sh 15

Important Notes:

The afplay command is specific to macOS. Linux users can replace it with a suitable command for playing audio files, such as mpg123 for MP3 files or aplay for WAV files. You may need to install these utilities if they're not already available on your system (sudo apt-get install mpg123 for Debian/Ubuntu, for example).

This script is a basic example of how to create an alarm. For more complex scheduling needs, consider learning about cron jobs on Linux or launchd on macOS, which offer much more flexibility for running tasks at specific times.

The script provided allows you to set the alarm for any number of minutes you specify as an argument when you run the script. The limitation on the number of minutes primarily depends on the maximum value the sleep command can handle and the stability of your system's uptime. Technically, the sleep command and most Unix-like system architectures can handle very large numbers, allowing you to set the alarm for minutes that translate to days, weeks, or even longer. If you’re like me though, setting the alarm for about an hour — if you’re trying to time-box a particular task or something — is plenty!

3. Create a Script that Outputs Open Applications:

ChatGPT prompt: Create a shell script that outputs which apps I have running (for performance reasons)

Creating a shell script that lists the currently running applications can be particularly useful for monitoring system performance and identifying resource-intensive processes. On macOS, you can use the osascript command to interact with AppleScript from the terminal, which provides a way to list active applications. For Linux systems, the approach usually involves commands like ps, top, or htop to list running processes, but identifying graphical applications specifically can be more nuanced.

  1. Create the Shell Script: Open your terminal, and let’s create a new shell script:
nano list_running_apps.sh

2. Script Content: Copy and paste the following lines into the nano text editor:

#!/bin/bash

echo "Listing all currently running applications..."

osascript -e 'tell application "System Events" to get name of (processes where background only is false)'

3. Save and Exit: Press Ctrl + O, press Enter to save the file, and then Ctrl + X to exit nano.

4. Make the Script Executable: Run the following in your terminal to make your script executable:

chmod +x list_running_apps.sh

5. Run the Script: Execute the script by typing the following in your terminal:

./list_running_apps.sh

When I ran this, my system outputted the following:

[12:17:25] [~] ❱❱❱ ./list_running_apps.sh
Listing all currently running applications...
Finder, DuckDuckGo, Slack, App Store, Messages, Notes, Google Chrome, iTerm2, Electron

Pretty cool, right?

This script uses osascript to execute an AppleScript command that asks "System Events" for the names of all processes where background only is false, which should correspond to applications with a user interface.

Conclusion:

Leveraging ChatGPT for crafting shell scripts unlocks numerous benefits, from seamlessly automating mundane tasks to facilitating swift prototyping and serving as a valuable learning resource. I’ve personally integrated these scripts to boost my productivity and highly recommend others to explore their potential for enhancing their own workflows. If there’s a task you’re looking to automate on your device to save time, there’s a great likelihood that ChatGPT can tailor a solution for you.

I’m eager to hear about your experiences and future project ideas involving ChatGPT. If this piece has sparked your interest, please share your thoughts and what you’re planning to create with ChatGPT in the comments. Let’s inspire each other with innovative uses of this transformative technology!

ChatGPT
Shell Script
Bash Script
Macos
Recommended from ReadMedium