liveBook · Manning

Shell script

As explained in The Complete Obsolete Guide to Generative AI

Bash is a command-line shell and scripting language for interacting with operating systems and executing shell programs.

A shell script turns a repeatable command-line task into something you can run again without retyping the whole incantation. Here, Bash automates requests to the ChatPDF API: first add a PDF by URL, then use the returned source ID to ask questions about it. section 5.2

Commands can be spread across lines for sanity. In a Bash shell, a trailing backslash means the command continues on the next line—but it must be the final character. One sneaky trailing space can break the command, because computers enjoy tiny dramas. section 5.2

Upload a PDF with curl

A multiline curl command sends a POST request with an API key, JSON content type, and a document URL. A successful upload returns a sourceID, which identifies the PDF in later requests. section 5.2 section 5.2

# Read the file line by line
while IFS= read -r line; do
  # Construct the command using a heredoc
  command=$(cat <<EOF
    curl -X POST 'https://api.chatpdf.com/v1/chats/message' \
      -H 'x-api-key: sec_xxxxxx' \
      -H 'Content-Type: application/json' \
      -d '{
        "sourceId": "src_xxxxxx",
        "messages": [
          {
            "role": "user",
            "content": "Based on the information in the PDF file at \
            https://bootstrap-it.com/[...].pdf, create multi-select \
            assessment questions that include the question, five \
            possible answers, the correct answers (identified only \
            by number), and an explanation. The questions should \
            cover each of these topics: $line"
          }
        ]
      }' >> multi_select_raw
EOF
  )
  
  echo "Executing: $command"
  eval "$command"
done < "$1"

This script reads an input file one line at a time, inserts each topic into a detailed prompt, and sends separate ChatPDF requests. It writes the resulting raw assessment-question output to a file, making a long manual task repeatable. section 5.2

How The Complete Obsolete Guide to Generative AI covers it

How deep it goes
Demonstrates multiline curl commands, source IDs, line-by-line input, heredocs, variables, and command execution without explaining Bash broadly.
Examples
Uses PDF upload and querying, then a script that turns topic-file lines into multi-select assessment-question requests.
What it assumes
Introduces multiline command continuation while assuming an API key, a PDF URL, and access to a Bash shell.
What you can do after
Apply it via curl and the ChatPDF API to submit a PDF and automate repeated prompt requests.
How it connects
Connects Bash, curl, APIs, JSON, PDF documents, ChatPDF, and assessment-question generation.

Also explained in

Compare how 6 books explain shell script →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Complete Obsolete Guide to Generative AI. The section links point to the parts of the book it draws on.