← Foundations Course

FOUNDATIONS PHASE 2 · 20

Shell / PowerShell:你輸入一行 Command,OS 到底做了什麼?

Shell 是你和 OS/process 世界之間的 command environment。真正重要的是 current working directory、command resolution、stdin/stdout/stderr、pipe、exit code。

Learning outcomes

1. Shell 啟動別的程式

PS C:\Project> node server.js

PowerShell 解析 command,找到 Node executable,啟動 Node process;PowerShell 本身不是 Node runtime。

2. Current Working Directory

Get-Location
Set-Location D:\Projects\App

Relative path 通常以 cwd 為 context,因此「檔案明明存在」但 command 找不到,常是 cwd 不同。

3. stdout / stderr

npm run build

# normal output → stdout
# errors/diagnostics → stderr

兩個 stream 都是 process 對 caller 的 evidence。

4. Pipe

Get-Process |
  Where-Object {
    $_.CPU -gt 10
  }

PowerShell pipe 傳遞的是 objects;傳統 Unix shell 常以 text/bytes 為主。概念共同點是把上一個 command output 接到下一個 command input。

5. Exit Code

npm run check

# success → typically 0
# failure → non-zero

CI 就是利用 exit status 決定 step pass/fail。

Project checkpoint:Course Workspace CLI

cd project
npm install
npm run check
npm run build

Evidence:
cwd
stdout/stderr
exit code
dist/ artifact

Debug evidence:Command Not Found

  1. command 拼字?
  2. executable 是否安裝?
  3. PATH 是否包含它?
  4. PowerShell variable 是否真的是 command path?

Knowledge check

  1. PowerShell 和 Node 是同一層嗎?
  2. cwd 如何影響 relative path?
  3. pipe 代表什麼資料流?
  4. CI 為什麼在意 exit code?