Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Shell Script Replacement

Replace Bash scripts with type-safe Deno.

Why Replace Shell Scripts?

Shell scripts are:

  • Hard to debug
  • No type safety
  • Limited error handling
  • Platform-specific

proc gives you:

  • Full TypeScript
  • IDE support
  • Proper error handling
  • Cross-platform

Common Patterns

File Operations

Bash:

#!/bin/bash
for file in *.txt; do
  wc -l "$file"
done

proc:

#!/usr/bin/env -S deno run --allow-read --allow-run
import { run } from "jsr:@j50n/proc@0.23.3";

for await (const entry of Deno.readDir(".")) {
  if (entry.name.endsWith(".txt")) {
    const count = await run("wc", "-l", entry.name).lines.first;
    console.log(count);
  }
}

Process Logs

Bash:

#!/bin/bash
grep ERROR app.log | wc -l

proc:

#!/usr/bin/env -S deno run --allow-read --allow-run
import { read } from "jsr:@j50n/proc@0.23.3";

const errors = await read("app.log")
  .lines
  .filter(line => line.includes("ERROR"))
  .count();

console.log(`${errors} errors`);

Backup Script

Bash:

#!/bin/bash
tar -czf backup-$(date +%Y%m%d).tar.gz /data

proc:

#!/usr/bin/env -S deno run --allow-read --allow-run
import { run } from "jsr:@j50n/proc@0.23.3";

const date = new Date().toISOString().split("T")[0].replace(/-/g, "");
await run("tar", "-czf", `backup-${date}.tar.gz`, "/data").toStdout();

System Monitoring

Bash:

#!/bin/bash
while true; do
  df -h | grep /dev/sda1
  sleep 60
done

proc:

#!/usr/bin/env -S deno run --allow-run
import { run, sleep } from "jsr:@j50n/proc@0.23.3";

while (true) {
  const usage = await run("df", "-h")
    .lines
    .find(line => line.includes("/dev/sda1"));
  
  console.log(usage);
  await sleep(60000);  // sleep() is exported from proc
}

Real Script Example

Complete deployment script:

#!/usr/bin/env -S deno run --allow-all
import { run } from "jsr:@j50n/proc@0.23.3";

console.log("πŸš€ Deploying application...");

try {
  // Pull latest code
  console.log("πŸ“₯ Pulling latest code...");
  await run("git", "pull").toStdout();
  
  // Install dependencies
  console.log("πŸ“¦ Installing dependencies...");
  await run("npm", "install").toStdout();
  
  // Run tests
  console.log("πŸ§ͺ Running tests...");
  await run("npm", "test").toStdout();
  
  // Build
  console.log("πŸ”¨ Building...");
  await run("npm", "run", "build").toStdout();
  
  // Restart service
  console.log("πŸ”„ Restarting service...");
  await run("systemctl", "restart", "myapp").toStdout();
  
  console.log("βœ… Deployment complete!");
} catch (error) {
  console.error("❌ Deployment failed:", error.message);
  Deno.exit(1);
}

Benefits

  1. Type Safety - Catch errors before running
  2. IDE Support - Autocomplete and refactoring
  3. Error Handling - Proper try-catch
  4. Debugging - Use debugger, breakpoints
  5. Testing - Write unit tests
  6. Portability - Works on any platform with Deno

Making Scripts Executable

chmod +x script.ts
./script.ts

Next Steps