diff --git a/quests/copy/game.yml b/quests/copy/game.yml index eddee51..e517005 100644 --- a/quests/copy/game.yml +++ b/quests/copy/game.yml @@ -225,15 +225,131 @@ stages: response: content: | 你成功查看到 `world2.txt` 的內容了! - 正如我們所見,`world2.txt` 的內容跟 `world.txt` 一模一樣。 - 恭喜你完成了這個關卡! - 輸入 `echo finish` 來進入下一個關卡吧! - hint: 輸入 `echo finish` 來進入下一個關卡 + `world2.txt` 的內容跟 `world.txt` 一模一樣。 + 接下來我們要來學習如何複製資料夾。 + 現在目錄下有一個 `notes` 資料夾,裡面有一個 `note.txt` 的檔案。 + 我們要能整個資料夾包含裡面的檔案一起複製到 `notes2`。 + 跟複製單一檔案不一樣的是,複製資料夾時,我們要加上 `-r` 的選項(recursive)代表將資料夾中的所有檔案都一起複製。 + 在使用指令時,如果想要調整指令的行為,可以在指令後面加上選項。 + 選項的格式通常是 `-` 加上一個字母,例如 `-r`。 + 選項擺放的位置沒有特別的限制,通常會跟在指令的後面,置於檔案名稱的前面。 + 請你使用 `cp -r notes notes2` 來複製 `notes` 資料夾到 `notes2`。 + hint: | + 使用 `cp -r notes notes2` 來複製 `notes` 資料夾到 `notes2` + 記得要加上 `-r` 的選項來複製所有檔案 - - id: END - name: 恭喜你完成了所有任務! + - id: cp-notes + name: 複製 `notes` 資料夾到 `notes2` requirements: - cat-world2 + condition: + pwd: /home/commander + command: cp -r notes notes2 + files: + - path: /home/commander/notes2 + type: directory + exists: true + - path: /home/commander/notes2/note.txt + type: file + exists: true + exceptions: + - id: wrong_directory + condition: + not: + pwd: /home/commander + response: + content: | + 你不在 `/home/commander` 資料夾裡。 + 請使用 `cd /home/commander` 來返回 `/home/commander` 資料夾。 + - id: missing_recursive + condition: + command: cp notes notes2 + response: + content: | + 你忘記加上 `-r` 的參數了嗎? + 請輸入 `cp -r notes notes2` 來複製 `notes` 資料夾到 `notes2` + response: + content: | + 你成功複製 `notes` 資料夾到 `notes2` 了! + 我們打開來確認一下 `notes2` 資料夾的內容吧! + hint: 使用 `ls notes2` 來查看 `notes2` 資料夾的內容 + + - id: ls-notes2 + name: 查看 `notes2` 資料夾的內容 + requirements: + - cp-notes + condition: + pwd: /home/commander + command: ls notes2 + output: note.txt + exceptions: + - id: wrong_directory + condition: + not: + pwd: /home/commander + response: + content: | + 你不在 `/home/commander` 資料夾裡。 + 請使用 `cd /home/commander` 來返回 `/home/commander` 資料夾。 + response: + content: | + 你成功查看到 `notes2` 資料夾的內容了! + 我們可以看到,整個 `notes` 資料夾包含裡面的檔案,都被複製到 `notes2` 資料夾了。 + 那麼最後我們要來試試看,如果複製一個檔案到已經存在的檔案會發生什麼事。 + 請把 `hello.txt` 複製到 `world.txt` 看看會發生什麼事。 + hint: 使用 `cp hello.txt world.txt` 來複製 `hello.txt` 到 `world.txt` + + - id: cp-overwrite + name: 複製 `hello.txt` 到 `world.txt` + requirements: + - ls-notes2 + condition: + pwd: /home/commander + command: cp hello.txt world.txt + exceptions: + - id: wrong_directory + condition: + not: + pwd: /home/commander + response: + content: | + 你不在 `/home/commander` 資料夾裡。 + 請使用 `cd /home/commander` 來返回 `/home/commander` 資料夾。 + response: + content: | + 你成功複製 `hello.txt` 到 `world.txt` 了! + 我們打開來確認一下 `world.txt` 的內容吧! + hint: 使用 `cat world.txt` + + - id: check-world + name: 檢查 `world.txt` 的內容 + requirements: + - cp-overwrite + condition: + pwd: /home/commander + command: cat world.txt + output: Hello + exceptions: + - id: wrong_directory + condition: + not: + pwd: /home/commander + response: + content: | + 你不在 `/home/commander` 資料夾裡。 + 請使用 `cd /home/commander` 來返回 `/home/commander` 資料夾。 + response: + content: | + `world.txt` 的內容變成 `Hello` 了! + 這是因為,當我們複製檔案時,如果目的檔案已經存在,那麼目的檔案的內容就會被覆蓋掉。 + 所以在複製檔案的時候,要特別注意目的檔案是否已經存在,否則可能會不小心把資料弄丟。 + 以上就是複製檔案的基本操作! + 請輸入 `echo finish` 來完成這個關卡。 + + - id: finish + name: 恭喜你完成了所有任務! + requirements: + - check-world condition: command: echo finish response: diff --git a/quests/remove/answer.sh b/quests/remove/answer.sh new file mode 100644 index 0000000..9d69429 --- /dev/null +++ b/quests/remove/answer.sh @@ -0,0 +1,14 @@ +echo start +ls +rm hello.txt +ls +rm world.txt wow.txt +ls +rmdir empty +ls +rmdir notes +rm notes/note.txt +rmdir notes +ls trash +rm -r trash +ls \ No newline at end of file diff --git a/quests/remove/game.yml b/quests/remove/game.yml new file mode 100644 index 0000000..54ba38a --- /dev/null +++ b/quests/remove/game.yml @@ -0,0 +1,252 @@ +id: remove +title: 刪除檔案與資料夾 +image: base +instruction: | + 在這個關卡中,你將會學到: + 1. 如何使用 `rm` 刪除檔案 + 2. 如何使用 `rmdir` 刪除空資料夾 + 3. 如何使用 `rm -r` 刪除資料夾及其內容 + +requirements: + - copy + - move + +stages: + - id: start + name: 按下 `開始` 按鈕開始 + condition: + command: echo start + response: + content: | + 今天我們要學習如何刪除檔案與資料夾。 + 現在資料夾裡有一個 `hello.txt` 跟一個 `world.txt`,以及一個 `notes` 資料夾。 + 請先用 `ls` 來查看它們。 + 看完之後,使用 `echo next` 來繼續吧! + hint: | + 用 `ls` 來查看資料夾內的檔案 + 完成後輸入 `echo next` 來繼續 + + - id: next + requirements: + - start + name: 查看檔案內容,完成後輸入 `echo next` 來繼續 + condition: + command: echo next + response: + content: | + 現在我們要來學習如何刪除檔案。 + `rm` 指令是 remove 的縮寫,可以用來刪除檔案。 + 使用方式為 `rm [檔案名稱]`。 + 我們先來刪除 `hello.txt` 吧! + hint: 使用 `rm hello.txt` 來刪除 `hello.txt` + + - id: rm-hello + requirements: + - next + name: 刪除 `hello.txt` + condition: + command: rm hello.txt + files: + - path: /home/commander/hello.txt + type: file + exists: false + response: + content: | + 你做得很好! + 我們用 `ls` 來查看一下資料夾內的檔案吧! + hint: 使用 `ls` 來查看資料夾內的檔案 + + - id: ls-no-hello + requirements: + - rm-hello + name: 檢查 `hello.txt` 是否還在資料夾中 + condition: + pwd: /home/commander + command: ls + response: + content: | + `hello.txt` 已經不在資料夾中了! + 你會發現 `world.txt` 與 `wow.txt` 還在。 + `rm` 也可以同時刪除多個檔案,只要把檔案名稱用空白隔開就可以了。 + 使用方式為 `rm [檔案1] [檔案2] ...`。 + 現在請你使用 `rm` 刪除 `world.txt` 與 `wow.txt`。 + hint: | + 使用 `rm world.txt wow.txt` 來刪除 `world.txt` 與 `wow.txt` + 檔案名稱之間要用空白隔開 + 名稱的順序不重要 + + - id: rm-world-wow + requirements: + - ls-no-hello + name: 刪除 `world.txt` 與 `wow.txt` + condition: + command: rm world.txt wow.txt + files: + - path: /home/commander/world.txt + type: file + exists: false + - path: /home/commander/wow.txt + type: file + exists: false + response: + content: | + 你做得很好! + 我們用 `ls` 來查看一下資料夾內的檔案吧! + hint: 使用 `ls` 來查看資料夾內的檔案 + + - id: ls-no-world-wow + requirements: + - rm-world-wow + name: 檢查 `world.txt` 與 `wow.txt` 是否還在資料夾中 + condition: + pwd: /home/commander + command: ls + response: + content: | + 你會發現 `world.txt` 與 `wow.txt` 已經不見了。 + 現在我們來學習 `rmdir` 指令。 + `rmdir` 指令可以用來刪除空的資料夾。 + 使用方式為 `rmdir [資料夾名稱]`。 + 我們先來刪除 `empty` 資料夾吧! + hint: 使用 `rmdir empty` 來刪除 `empty` 資料夾 + + - id: rm-empty + requirements: + - ls-no-world-wow + name: 刪除 `empty` 資料夾 + condition: + command: rmdir empty + files: + - path: /home/commander/empty + type: directory + exists: false + response: + content: | + 你做得很好! + 我們用 `ls` 來查看一下資料夾內的檔案吧! + hint: 使用 `ls` 來查看資料夾內的檔案 + + - id: ls-no-empty + requirements: + - rm-empty + name: 檢查 `empty` 資料夾是否還在 + condition: + pwd: /home/commander + command: ls + response: + content: | + 你會發現 `empty` 資料夾已經不見了。 + 現在請你試試看 `rmdir` 指令刪除 `notes` 資料夾。 + hint: 使用 `rmdir notes` 來刪除 `notes` 資料夾 + + - id: rm-notes-failed + requirements: + - ls-no-empty + name: 嘗試刪除 `notes` 資料夾 + condition: + command: rmdir notes + error: "rmdir: failed to remove 'notes': Directory not empty" + response: + content: | + 指令執行失敗了! + 你會發現 `notes` 資料夾還在。 + 這是因為 `rmdir` 指令只能刪除空的資料夾。 + 我們先來將 `notes` 資料夾的檔案刪除再來試一次。 + 現在請你使用 `rm` 指令刪除 `notes` 資料夾內的檔案。 + hint: | + 使用 `rm notes/[檔案名稱]` 來刪除 `notes` 資料夾內的檔案 + 你可能需要使用 `ls notes` 來查看 `notes` 資料夾內有哪些檔案 + + - id: rm-notes-content + requirements: + - rm-notes-failed + name: 刪除 `notes` 資料夾內的檔案 + condition: + command: rm notes/note.txt + files: + - path: /home/commander/notes/note.txt + type: file + exists: false + exceptions: + - id: hint + condition: + command: echo hint + response: + content: | + 你可以使用 `rm notes/note.txt` 來刪除 `notes` 資料夾內的 `note.txt` 檔案。 + hint: 使用 `rm notes/note.txt` 來刪除 `notes` 資料夾內的 `note.txt` 檔案 + response: + content: | + 很好!現在我們再使用 `rmdir` 指令刪除 `notes` 資料夾。 + hint: 使用 `rmdir notes` 來刪除 `notes` 資料夾 + + - id: rm-notes + requirements: + - rm-notes-content + name: 刪除 `notes` 資料夾 + condition: + command: rmdir notes + files: + - path: /home/commander/notes + type: directory + exists: false + response: + content: | + 很好!`notes` 資料夾已經被刪除了。 + 接下來我們來刪除 `trash` 資料夾。 + 先使用 `ls` 指令查看一下 `trash` 資料夾內容。 + hint: 使用 `ls trash` 來查看 `trash` 中的檔案 + + - id: ls-trash + requirements: + - rm-notes + name: 查看 `trash` 資料夾內容 + condition: + command: ls trash + response: + content: | + 天啊!`trash` 資料夾裡有好多檔案,甚至還有其他的資料夾! + 如果今天資料夾中有很多檔案與資料夾,我們不想一個一個刪除,該怎麼辦呢? + 跟 `cp` 的時候一樣,這時候我們可以加上 `-r` 的選項來刪除資料夾與它的所有檔案。 + `-r` 的意思是遞迴(recursive),也就是會把資料夾中的所有檔案都刪除。 + 我們來試試看吧! + hint: 使用 `rm -r trash` 來刪除 `trash` 資料夾 + + - id: rm-trash + requirements: + - ls-trash + name: 刪除 `trash` 資料夾及其內容 + condition: + command: rm -r trash + files: + - path: /home/commander/trash + type: directory + exists: false + response: + content: | + 很好!現在請你使用 `ls` 指令查看資料夾內容。 + hint: 使用 `ls` 來查看資料夾內容 + + - id: ls-no-trash + requirements: + - rm-trash + name: 檢查 `trash` 資料夾是否還在 + condition: + pwd: /home/commander + command: ls + response: + content: | + `trash` 資料夾已經不見了。 + 恭喜你!你已經學會如何刪除檔案與資料夾了! + 請輸入 `echo finish` 來完成這個關卡。 + + - id: END + requirements: + - ls-no-trash + name: 完成任務 + condition: + command: echo finish + response: + content: | + 你已經學會如何刪除檔案與資料夾了! + 恭喜你完成這個關卡! diff --git a/quests/remove/home/empty/.gitkeep b/quests/remove/home/empty/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/quests/remove/home/hello.txt b/quests/remove/home/hello.txt new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/quests/remove/home/hello.txt @@ -0,0 +1 @@ +Hello diff --git a/quests/remove/home/notes/note.txt b/quests/remove/home/notes/note.txt new file mode 100644 index 0000000..77c6f9e --- /dev/null +++ b/quests/remove/home/notes/note.txt @@ -0,0 +1 @@ +Note diff --git a/quests/remove/home/trash/garbage.txt b/quests/remove/home/trash/garbage.txt new file mode 100644 index 0000000..e69de29 diff --git a/quests/remove/home/trash/rubbish.txt b/quests/remove/home/trash/rubbish.txt new file mode 100644 index 0000000..e69de29 diff --git a/quests/remove/home/trash/secrets/envelope.txt b/quests/remove/home/trash/secrets/envelope.txt new file mode 100644 index 0000000..e69de29 diff --git a/quests/remove/home/world.txt b/quests/remove/home/world.txt new file mode 100644 index 0000000..216e97c --- /dev/null +++ b/quests/remove/home/world.txt @@ -0,0 +1 @@ +World diff --git a/quests/remove/home/wow.txt b/quests/remove/home/wow.txt new file mode 100644 index 0000000..18bd653 --- /dev/null +++ b/quests/remove/home/wow.txt @@ -0,0 +1 @@ +Wow \ No newline at end of file