<style>
.command {
display: inline-block;
font-size: 24px;
color: black;
background-color: white;
padding: 7px;
border-radius: 10px;
margin: 5px;
}
</style>
## 計算機概論Lab-11
### LINUX-2

<p>https://hackmd.io/@IMOK/Lab11</p>
---
<img src="https://hackmd.io/_uploads/ryUSIqnJT.jpg" width=400 style="border-radius:1000px;"/>
講師: 賴昱有
---
### HOW TO 魔改 TERMINAL
----
### Bash vs Z Shell
<div style="float: left;margin : 0px 0px px 20px">
<img src= "https://hackmd.io/_uploads/HJVmpwc71e.png" width=500 style="margin : 20px 0px 0px 0px"/>
</div>
<div style="margin : 0px 0px 0px 0px">
<img src= "https://hackmd.io/_uploads/rJom6P97kg.png" width=400 style="margin : 20px 0px 0px 0px"/>
</div>
----
+ <font size = 5> login shell 改成 zsh</font>
+ <font size = 5><span class="command">chsh -s /usr/bin/zsh</span></font>
+ <font size = 5>安裝 oh-my-zsh</font>
+ <font size = 5><span class="command">sh -c "$(curl -fsSL `https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh`)"</span></font>
+ <font size = 5>安裝 zsh theme (我用 Powerlevel10k)</font>
+ <font size = 5><span class="command">git clone --depth=1 `https://github.com/romkatv/powerlevel10k.git` ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k</span></font>
----
+ <font size = 5> 編輯 ~/.zshrc 修改 theme</font>
+ <font size = 5><span class="command">ZSH_THEME="powerlevel10k/powerlevel10k"</span></font>
+ <font size = 5>重整 shell</font>
+ <font size = 5><span class="command">exec $SHELL</span></font>
+ <font size = 5>安裝 zsh-syntax-highlighting</font>
+ <font size = 5><span class="command">git clone `https://github.com/zsh-users/zsh-syntax-highlighting.git`</span></font>
+ <font size = 5><span class="command">echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> \${ZDOTDIR:-$HOME}/.zshrc</span></font>
+ 載入設定檔
+ <font size = 5><span class="command">source ~/.zshrc</span></font>
----

---
### SSH
<font size = 5>用法 : <span class="command">ssh {user}@{host}</span></font>
<font size = 5>e.g. <span class="command">ssh b0135XXXX@linux.aclab.cool</span></font>
<font size = 5>or 使用 GUI (Bitvise SSh Client)</font>
---
## VIM
<font size = 6 >文字介面編輯器</font>
----
<div style="text-align: center;">
<font size =7 style="margin : 0px 0px 0px 40px">分成三種模式</font>
<br>
<br>
<ul>
<li>normal mode</li>
<li>edit mode</li>
<li>visual mode</li>
</ul>
</div>
<br>
<font size = 6 >一開始是 normal mode 按 <span class="command">i</span> 進入 edit mode</font>
<font size = 6 >按 <span class="command">ESC</span> 回到 normal mode</font>
----
<font size = 6 >在 normal mode 打冒號可以輸入指令</font>
<style>
table {
width: 80%;
border-collapse: collapse;
line-height: 1.5; /* 行距 */
}
th, td {
font-size: 28px; /* 字體大小 */
}
</style>
| Command | Description |
| ------- | ----------- |
| :w | 儲存 |
| :wq | 儲存並退出 |
| :q! |不儲存退出 |
----
<font size = 5 >vim 是個很強大的編輯器</font>
<font size = 5 >在 normal mode 可以用的指令還有連技超多</font>
<font size = 5 >如果想了解的可以看看 這個[Vim](https://mropengate.blogspot.com/2015/07/vim-ch2.html)</font>
<font size = 5 >推薦要學 只能用文字介面的時候非常好用</font>
---
### JOE
<font size = 5 >另一個文字介面編輯器</font>
<font size = 5 ><span class="command">joe filename.txt</span></font>
<style>
table {
width: 80%;
border-collapse: collapse;
line-height: 1.5; /* 行距 */
}
th, td {
font-size: 28px; /* 字體大小 */
}
</style>
| operation | description |
| --------- | ---------------- |
| Ctrl+K,H | show help |
| Ctrl+K,F | search text |
| Ctrl+K,L | go to line |
| Ctrl+K,S | save file |
| Ctrl+K,X | save file and exit editor |
| Ctrl+Y| delete a line |
---
### COMMANDS
<font size = 5 >如果不知道指令怎麼用 請參考</font>
<font size = 5 ><span class="command">command --help</span></font>
<font size = 5 ><span class="command">man command</span></font>
---
### Quick search table
----
<style>
table {
width: 80%;
border-collapse: collapse;
line-height: 1.5; /* 行距 */
}
th, td {
font-size: 28px; /* 字體大小 */
}
</style>
| command | usage | description |
| ------- | ------------------ | ------------------ |
| chsh | chsh /bin/tcsh | 修改預設登入 shell |
| passwd | passwd | 修改密碼 |
| chmod | chmod 664 file | 修改檔案權限 |
| cd | cd directory | 進入資料夾 |
| pwd | pwd | 顯示目前的目錄位置 |
| echo | echo "hello world" | 輸出文字 |
| cat | cat file | 檢視檔案內容 |
| less | less file | 另開分頁查看內容 |
----
<style>
table {
width: 80%;
border-collapse: collapse;
line-height: 1.5; /* 行距 */
}
th, td {
font-size: 28px; /* 字體大小 */
}
</style>
| command | usage | description |
| ------- | ------------------------ | -------------------- |
| head | head -n 10 file | 檢視檔案前10行 |
| tail | tail -n 10 file | 檢視檔案最後10行 |
| grep | grep "apple" example.txt | 擷取有關鍵字的那一行 |
| cp | cp file file_copy | 複製檔案 |
| mv | mv file file_moved | 移動、重新命名檔案 |
| mkdir | mkdir directory | 創建資料夾 |
| rmdir | rmdir directory | 刪除空目錄 |
| touch | touch file | 創建檔案 |
| rm | rm file | 刪除檔案 |
----
<style>
table {
width: 80%;
border-collapse: collapse;
line-height: 1.5; /* 行距 */
}
th, td {
font-size: 28px; /* 字體大小 */
}
</style>
| command | usage | description |
| -------- | ------------------------ | -------------------- |
| chsh |chsh /bin/tcsh | 修改預設登入 shell |
| ypchsh | ypchsh /bin/tcsh | 在 NIS 機器上修改預設登入 shell |
| passwd | passwd | 修改密碼 |
| yppasswd | yppasswd | 在 NIS 機器上修改密碼 |
| ls | ls -l | 列出當前目錄下的檔案 |
| ll | ll | ls -l的縮寫 |
----
| command | usage | description |
| ------- | -------------------- | ------------------ |
| vim | vim file | 使用 vim 編輯檔案 |
| df | df -h | 列出磁碟剩餘空間 |
| du | du –max-depth 1 -h . | 列出資料夾所占空間 |
| free | free -h | 列出記憶體使用狀況 |
| who | who | 查看目前登入使用者 |
| gcc | gcc main.c | 編譯 c 語言檔案 |
| g++ | g++ main.cpp | 編譯 c++ 語言檔案 |
----
| command | description |
| ------- | ------------------ |
| gcc file.c | 編譯 file.c,生成默認執行檔 a.out |
| gcc -o output file.c | 編譯 file.c,輸出執行檔為 output |
| gcc -c file.c | 只編譯生成目標檔案 file.o 不連結|
| gcc -E file.c -o file.i | 只進行預處理,輸出為 file.i |
| gcc -S file.c | 只產生組合語言檔案 file.s |
| gcc -v file.c | 顯示詳細編譯過程 |
----
<img src= "https://hackmd.io/_uploads/HJAT8nqXJg.png" width=400 style="margin : 0px 0px 0px 0px"/>
---
#### DF
<font size = 5 >列出磁碟剩餘空間</font>
<font size = 5 >e.g.<span class="command">df -h</span></font>
#### DU
<font size = 5 >列出資料夾所占空間</font>
<font size = 5 >e.g.<span class="command"> du --max-depth 1 -h .</span></font>
#### FREE
<font size = 5 >列出記憶體使用狀況</font>
<font size = 5 >e.g. <span class="command">free -h</span></font>
----
#### GCC
<font size = 5 >編譯 c 語言檔案</font>
<font size = 5 >e.g. <span class="command">gcc main.c</span></font>
#### G++
<font size = 5 >編譯 c++ 語言檔案</font>
<font size = 5 >e.g. <span class="command">g++ main.cpp</span></font>
---
#### PROCESSES AND JOBS
----
<font size = 5 >在指令後面加上<span class="command"> & </span>可以放到背景執行</font>
<font size = 5 >e.g. <span class="command">sleep 60 &</span></font>
<font size = 5 >用<span class="command"> ps </span>列出當前所有 process</font>
<font size = 5 >用<span class="command"> jobs </span>可以看到正在背景執行的 process</font>
<img src= "https://hackmd.io/_uploads/SJxvL_q71e.png" width=400 style="margin : 0px 0px 0px 0px"/>
----
<font size = 5 >執行程式時可以用組合鍵<span class="command"> Ctrl+c </span>傳送停止訊號</font>
<font size = 5 >或是用<span class="command"> Ctrl+z </span>將程式暫停後放到背景</font>
<font size = 5 >放到背景後可以用<span class="command"> fg %1 </span>把它放到前景繼續執行</font>
<font size = 5 >或是<span class="command"> bg %1 </span>讓他在背景繼續執行</font>
<img src= "https://hackmd.io/_uploads/ryMUOdqmJx.png" width=700 style="margin : 20px 0px 0px 0px"/>
----
<font size = 5 >可以使用<span class="command"> kill %1 </span>對程式發送訊號</font>
<font size = 5 ><span class="command">kill </span>並不是只能發送停止訊號</font>
<img src= "https://hackmd.io/_uploads/ryPaO_qXkl.png" width=500 style="margin : 20px 0px 0px 0px"/>
---
### REDIRECTION
<font size = 5 >overwrite:<span class="command">echo "string" > file.txt</span></font>
<font size = 5 >append:<span class="command">echo "string2" >> file.txt</span></font>
---
### PIPE
<font size = 5 ><span class="command">cat file.txt | grep "apple"</span></font>
<font size = 5 ><span class="command">cat /proc/meminfo | grep Pages | sort</span></font>
<font size = 5 ><span class="command">du -d 1 -h | sort -h</span></font>
<img src= "https://hackmd.io/_uploads/Hy-Dt_cX1g.png" width=400 style="margin : 0px 0px 0px 0px"/>
---
### WILDCARDS
<font size = 5 ><span class="command">ls *.txt</span></font>
<font size = 5 ><span class="command">cat file *</span></font>
---
### ARCHIVING
[GNU / Linux 各種壓縮與解壓縮指令 - 凍仁的筆記](https://note.drx.tw/2008/04/command.html)
+ <font size = 5 >打包</font>
+ <font size = 5 ><span class="command">tar cvf FileName.tar DirName</span></font>
+ <font size = 5 >解包</font>
+ <font size = 5 ><span class="command">tar xvf FileName.tar</span></font>
+ <font size = 5 >打包且壓縮</font>
+ <font size = 5 ><span class="command">tar zcvf FileName.tar.gz DirName</span></font>
+ <font size = 5 >解包</font>
+ <font size = 5 ><span class="command">tar zxvf FileName.tar.gz</span></font>
---
## 作業網站
作業做完請上傳到 http://140.121.197.13/tutorial
並且注意上傳時間限制、檔名、其他規範
上課時提前做完可以直接給助教 demo 登記
就不需要上傳 demo 過的部分
----
## Question 1
<font size=6>
Count the disk usage of your <span class="command">www</span>directory.
</font>
----
## Question 2
<font size=6>Write a program to find the prime numbers
between 1 and 10000 and compile it using gcc or g++.
</font>
----
## Question 3
<font size=6>Execute the file and redirect the output to the file <span class="command">primes.txt</span>.
</font>
----
## Question 4
<font size=6>[Insertion sort intro](https://www.youtube.com/watch?v=JU767SDMDvA)</font>
<font size=5>Allow the user to input 10 numbers, then sort them using the insertion sort and
save the sorted result to a file <span class="command">isort10.txt</span>.
</font>
----
## Question 5
<font size=5>Complete the 99 multiplication table using only one loop
and output the result to a file named <span class="command">1loop99.txt</span>.

</font>
{"title":"計算機概論Lab-11","description":"image","contributors":"[{\"id\":\"738dd674-cd6a-462c-87e2-b67e68f12ac0\",\"add\":20509,\"del\":9335}]"}