From 8597c22b9f94c3d4f2f09bc0d66dce1d42b7f5fc Mon Sep 17 00:00:00 2001 From: wancat Date: Tue, 23 Jul 2024 18:37:52 +0800 Subject: [PATCH] wip: execute tutorial content --- quests/execute/execute.md | 59 +++++++++++++++++++++++++++++++++++ quests/execute/game.yml | 18 +++++++++++ quests/execute/home/dog | 11 +++++++ quests/execute/home/hello.txt | 1 + 4 files changed, 89 insertions(+) create mode 100644 quests/execute/execute.md create mode 100644 quests/execute/game.yml create mode 100755 quests/execute/home/dog create mode 100644 quests/execute/home/hello.txt diff --git a/quests/execute/execute.md b/quests/execute/execute.md new file mode 100644 index 0000000..166970f --- /dev/null +++ b/quests/execute/execute.md @@ -0,0 +1,59 @@ +# 執行檔案 + +## Outline + +1. 什麼是可執行檔 +2. 使用 `which` 指令查看執行檔的位置 +3. 如何執行可執行檔 + +--- + +在我們先前的課程裡,已經使用了許多不同的指令,像是 `ls`, `cat`, `cp`, `mv`, `rm` 等。 +這些指令,其實都是一個**可執行檔**,在你打指令的時候被作業系統執行。 +所以大部分的指令,我們其實都能找到它在檔案系統裡的原始位置。 +要查看一個指令它的檔案位置,可以使用 `which` 指令。 +輸入 `which [指令名稱]` 就能夠查看指令的檔案位置了。 + +> 使用 `which ls` 來查看 `ls` 的路徑 + +--- + +你看到終端機顯示出 `/bin/ls` 這個路徑,它其實就是 `ls` 指令它檔案存在的位置。 +`/bin` 是一個系統資料夾,裡面會儲存許多系統內建的程式,像是 `cat`, `cp` 等指令也都被存在這裡。 + +> 使用 `which cat` 查看 `cat` 的路徑 + +--- + +那我們要執行一個可執行檔時,只要直接輸入它的路徑就可以執行它。 +我們來試看看使用執行檔的路徑來執行指令。 + +> 輸入 `/bin/ls` 來查看當前的資料夾 + +--- + +你會看到它列出了我們資料夾內的檔案,你可以自己比較看看,跟單純打 `ls` 的結果會一模一樣。 +它一樣可以使用參數。我們來用 `cat` 試試看。 + +> 輸入 `/bin/cat hello.txt` 來顯示 `hello.txt` 的內容 + +--- + +你會看到了 `hello.txt` 的內容,效果跟單純用 `cat` 一模一樣。 +事實上,這是必然的,因為 `cat` 的檔案位置就是 `/bin/cat`,我們執行了同一個程式,當然會有同樣的結果。 +其實指令,就是由 **一個執行檔** + **參數** 所構成。 +我們之所以不用打 `/bin/ls`,而是只要打 `ls` 就能執行,是因為有 `PATH` 這個環境變數,幫我們預設好了終端機要去哪些*資料夾*找檔案。 +但因為這超出了本次課程的範圍,關於 `PATH` 以及環境變數的議題,我們會在未來的課程再討論。 + +我們也可以執行不是內建的指令,像是現在資料夾中的 `dog` 檔案。 + +> 使用 `cat dog` 來查看 `dog` 的內容 + +--- + +你會看到這是一個用 `echo` 所組成的簡單程式, + +當我們要執行一個非內建的指令時,需要指定它的路徑,可以使用絕對或是相對路徑。 +如果使用相對路徑時,要用 `./` 來代表當前的目錄。因此我們要用 `./dog` 來執行 `dog` 檔案。 + +> 輸入 `./dog` 來執行 `dog` 檔案 diff --git a/quests/execute/game.yml b/quests/execute/game.yml new file mode 100644 index 0000000..b495608 --- /dev/null +++ b/quests/execute/game.yml @@ -0,0 +1,18 @@ +title: 執行檔案 +instruction: | + 在這個關卡中,你將會學到: + 1. 什麼是可執行檔 + 2. 如何執行可執行檔 + 3. 使用 `which` 指令查看執行檔的位置 + +requirements: + - remove + +stages: + - id: start + task: 按下開始按鈕 + type: start + condition: + command: ^echo start$ + responses: + - content: | diff --git a/quests/execute/home/dog b/quests/execute/home/dog new file mode 100755 index 0000000..7633a40 --- /dev/null +++ b/quests/execute/home/dog @@ -0,0 +1,11 @@ +#!/bin/sh + +echo "汪汪汪" +echo "我比貓還厲害" +if test -n "$1"; +then + echo "貓會的事我也會" + cat $1 +else + echo 貓會卡住我不會 +fi \ No newline at end of file diff --git a/quests/execute/home/hello.txt b/quests/execute/home/hello.txt new file mode 100644 index 0000000..2d9bd63 --- /dev/null +++ b/quests/execute/home/hello.txt @@ -0,0 +1 @@ +Hello! This is a file hello.txt