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
- Type Safety - Catch errors before running
- IDE Support - Autocomplete and refactoring
- Error Handling - Proper try-catch
- Debugging - Use debugger, breakpoints
- Testing - Write unit tests
- Portability - Works on any platform with Deno
Making Scripts Executable
chmod +x script.ts
./script.ts
Next Steps
- Running Processes - Process basics
- Error Handling - Handle failures
- Process Pipelines - Chain commands