Basic Usage

Bash Code Runner blocks allow you to execute Bash scripts within your Hunch canvas. This powerful feature enables you to process data, interact with the file system, use command-line tools, and create dynamic outputs.

Simple Example

Let’s start with a basic example. Assume we have a canvas with two input content blocks:

  1. A block titled “First Name”
  2. A block titled “Last Name”

We’ll create a Bash Code Runner block that combines these inputs to greet the person.

# Get the contents of the input blocks
first_name=$(hunch block output --title "First Name")
last_name=$(hunch block output --title "Last Name")

# Combine the names and create a greeting
greeting="Hello $first_name $last_name!"

# Output the greeting
echo "$greeting" > /output.md

This script will output: “Hello [First Name] [Last Name]!” where [First Name] and [Last Name] are replaced with the actual contents of those input blocks.

Using Hunch CLI Tools

Hunch provides several CLI tools that you can use in your Bash scripts to interact with your canvas.

Key Commands

  1. hunch compiled-prompt

    • Outputs the compiled prompt from all upstream blocks.
    • Example: hunch compiled-prompt > /tmp/prompt.txt
  2. hunch block

    • Retrieves the content of a specific block.
    • Example: hunch block output --title "Block Title"
  3. hunch input

    • Outputs all input data for the Code Runner block in JSON format.
    • Example: hunch input > /tmp/input.json

Input Handling

Accessing Specific Blocks

To access the content of a specific block, use the hunch block command:

# Access a block by its exact title
block_content=$(hunch block output --title "Exact Block Title")

# You can also use it in combination with jq for JSON parsing
block_content=$(hunch input | jq -r '.blocks[] | select(.title == "Exact Block Title") | .output')

Working with All Inputs

To work with all input blocks at once:

# Loop through all blocks
hunch input | jq -c '.blocks[]' | while read -r block; do
    title=$(echo $block | jq -r '.title')
    content=$(echo $block | jq -r '.output')
    echo "Block '$title': $content" >> /output.md
done

Output Handling

Text Output

Use redirection to /output.md to send text output:

echo "This is a simple text output." > /output.md
echo "You can append multiple lines." >> /output.md

File Output

To output files, write them to the /output directory:

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

# Create the output directory if it doesn't exist
mkdir -p /output

# Create the smiling emoji
convert -size 400x400 xc:none \
    -fill yellow -draw "circle 200,200 200,20" \
    -fill black \
    -draw "circle 140,140 140,120" \
    -draw "circle 260,140 260,120" \
    -draw "arc 100,180 300,300 0,180" \
    -strokewidth 10 \
    -stroke black \
    -draw "arc 100,180 300,300 0,180" \
    /output/smile.png

# Check if the image was created successfully
if [ $? -eq 0 ]; then
    echo "Smiling emoji has been created at /output/smile.png"
else
    echo "Failed to create the smiling emoji"
    exit 1
fi

Markdown Rendering

Output is rendered as Markdown. You can use Markdown syntax in your output:

cat << EOF > /output.md
# This is a Markdown Header
- List item 1
- List item 2
**Bold text** and *italic text*
EOF

Error Handling

It’s usually best not to handle errors. Rather let them propagate up to the Hunch platform so that the workflow stops. However, if you need to handle errors, you can use conditional statements:

if some_risky_operation 2>/dev/null; then
    echo "Operation successful" > /output.md
else
    echo "An error occurred" > /output.md
fi

Advanced Usage

Working with External Tools

Bash scripts can leverage a wide range of command-line tools. Here’s an example using curl:

if response=$(curl -s "https://api.example.com/data"); then
    echo "Received data: $response" > /output.md
else
    echo "API request failed" > /output.md
fi

Data Transformation

Bash is powerful for text processing. Here’s a simple example using sed and awk:

# Assume we have a CSV input block
csv_data=$(hunch block output --title "CSV Data")

# Transform the data: capitalize the first column
transformed_data=$(echo "$csv_data" | awk -F',' '{print toupper($1) "," $2 "," $3}')

# Output the transformed data
echo "$transformed_data" > /output.md

Best Practices

  1. Modularity: Use functions to organize your code for better readability and reusability.
  2. Error Handling: Always include error handling to make your scripts more robust.
  3. Comments: Add comments to explain complex logic or non-obvious operations.
  4. Input Validation: Validate inputs to ensure your script can handle unexpected data.
  5. Quoting: Always quote your variables to prevent word splitting and globbing.

Conclusion

Bash Code Runner blocks offer a powerful way to extend the capabilities of your Hunch canvas. By combining Bash’s text processing capabilities and access to system tools with Hunch’s AI-driven workflows, you can create sophisticated, dynamic, and efficient applications.

Remember to leverage the power of command-line tools and always consider security implications when writing your scripts, especially when dealing with user inputs or sensitive data.