NCNU-OpenSource
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Linux 2020 讀書會 — 第四次 [toc] ## Subject 1 - 認識與學習 BASH ![](https://i.imgur.com/0NY8OyY.png) ### 變數與功能 > 特定字串來代表不固定的內容 - 供系統運作使用 - Example: PATH - 個人設定的使用 - 設定規則 - 等號來連結名稱與變數的值 - 兩邊不能有空格 - 變數名稱只能是英文或數字 但開頭不能是數字 - 變數內容若有空白字元可使用雙引號或單引號將變數內容結合起來or跳脫字元(\ ) - 可使用 MyHostName=changlin\ ${MyHostName} 來連接變數 - 可透過 `export` 來將變數設為環境變數 - 可用 `unset` 把變數刪掉 - 列出環境變數 `env` 、 `set` - `declare [-aixr] varible` 宣告變數 - -r readable - -a array - `test=${test-abc}` 若變數 `test` 尚未設定 則將它設定為 abc :::info 讀取使用者 input 來設定變數內容 - `read -p "提示" 變數名稱` 顯示提示 - `read -t 秒數 變數名稱` 等待多少秒 ::: #### 變數種類 - 環境變數 > 可視為全域變數 - 自訂變數 > 可視為區域變數 > 存在當前 Bash 程序 (Process) #### 命令別名 `alias 別名="指令"` 設定別名對應指令 `unalias 名稱` 移除別名 #### 歷史指令紀錄 - `history`: 顯示歷史使用的指令 - `history 數量`: 顯示指定筆數的歷史紀錄 - 在指令前加上空白不會被存至歷史指令中 - `echo ${HISTSIZE}`: history 內存的指令總數 ##### HISTCONTROL - [設定教學](https://blog.gtwang.org/linux/mastering-linux-command-line-history/) ![](https://i.imgur.com/e4co9hO.png) ### Bash #### bash 進站歡迎詞 - 由 `/etc/motd` 設定佈告欄訊息 - 存放在 `/etc/issue` 、 `/etc/issie.net` #### /etc/profile - login profile - non-login profile :::info 可以去暸解在 SSH 情況下 profile 們讀取的順序 ::: #### source - 在當前 shell 的環境下(同個 process,不 fork)執行指令 - 例如 `$ source program.sh` - 好處:如果檔案中有設定環境變數,則會在當前 shell 中表現出來 範例: ``` echo "export a=3" > /tmp/a sh /tmp/a echo $a <--- 沒東西 source /tmp/a echo $a <--- 3 exec /tmp/a <--- 當前 shell 被關閉 ``` :::info 值得注意的是,shell 執行指令預設是 fork 一個 process 後執行,結束後返回父行程。 使用 `source` 的話,會在當前行程執行指令。 使用 `exec` 的話,會在當前行程執行指令,並且在執行過後關閉目前行程。 ::: #### 終端機環境設定 - [stty(set tty)](http://manpages.ubuntu.com/manpages/xenial/zh_TW/man1/stty.1.html) - `-a`: 所有設定 - `-u`: 使用未設定變數顯示錯誤訊息 - `-v`: 輸出訊息前先顯示訊息原始內容 ### 資料流重新導向 - 標準輸出 (stdout): 代碼為 1 - 標準錯誤 (stderr): 代碼為 2 - `>` 表示 replace,`>>` 表示 append > 2 > &1 將 "標準錯誤 (stderr)" 重導入至 "標準輸出(stdout)當前的檔案描述符" > & > file.txt 將所有輸出、錯誤都存到 file.txt - [標準輸入: < ](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Redirecting-Input) - `cat > text.txt` 後可以輸入在 shell 輸入文字,並將輸入內容存進text.txt - `cat > text.txt << "EOF"` 在輸入 EOF 後結束輸入 ``` $ cat > catfile << "eof" > This is a test. > OK now stop > eof <==輸入這關鍵字,立刻就結束而不需要輸入 [ctrl]+d ``` - 多句執行 (A, B 可為任意指令與參數) - A ; B -> 會執行 A + B - A && B -> 前面 A 正確才執行後面 B - A || B -> 前面 A 正確後面 B 不執行、前面 A 錯誤後面 B 執行 ### 管線命令(pipe) - cut -d'分隔字元' -f 分隔區間 - 用分隔字元作為分段,再選出要哪段 - cut -c 字元區間 - 選第幾個字元到第幾個字元的資料 - sort: 排序資料 - `-f`:忽略大小寫 - uniq: 讓排序後的指令不重複顯示 - wc: 計算檔案裡多有少多少行 (-l)、字元數(-m)、英文單字的種數(-w) - tee: 雙向重導向,把執行中的 stdin 顯示出來 - expand 把tab轉成幾個空白建(-t) - `split [-bl] file PREFIX` 依據檔案大小(-b) 行數(-l) PREFIX:分割檔案的前導文字 :::info ps auxf: 顯示 Process 的 Parent/Child 關係 ::: - 補充 - 上課沒教的東西 https://missing.csail.mit.edu/2020/ - sudo 時傳遞環境變數 ![](https://i.imgur.com/ERdoYaG.png) - sudo 傳遞當前環境變數 ![](https://i.imgur.com/Kb530iD.png) - echo $0 顯示目前 shell - 在 vim 中透過 `w !sudo tee %` 內容重導向回當前檔案儲存 (Yiyu0x 補充) - 可以研究 wc & uniq 為什麼前面有 ~~可惡的~~ 空白 - https://hackmd.io/KLivYX2oQ-GNjcB7XczBpQ?view ## Subject 2 - 學習 shell scripts ### For Why? - 自動化 - 減少重複流程 ### 執行 `bash 檔案.sh` ### 編寫 - `#` 代表註解 - `exit`: 代表回傳值 ### 常用script 腳本 #### 對談式腳本 > `$((計算式))` (變數不加括號預設會被當作字串) - 矩陣 - `age=("34" "25" "29")` : 矩陣 ### 條件判斷 ### test filename - -e 該『檔名』是否存在? - -f 該『檔名』是否存在且為檔案(file)? - -d 該『檔名』是否存在且為目錄(directory)? - -r 是否可讀 - -w 是否可寫 - -x 是否可執行 #### 判斷符號 ==[kaldksladkkdakdlaskd](https://hackmd.io/v_JJi7QhQCGRIeFdSm0q6Q?view) ~~我可不可以直接貼講者共筆的連結過來~~[:accept:](https://hackmd.io/v_JJi7QhQCGRIeFdSm0q6Q?view) https://hackmd.io/v_JJi7QhQCGRIeFdSm0q6Q?view - 補充 - 檢查 Shell 可能存在的風險 https://shellcheck.net - https://codeburst.io/13-tips-tricks-for-writing-shell-scripts-with-awesome-ux-19a525ae05ae - https://en.wikipedia.org/wiki/Fork_bomb - 謹慎使用(X - Shell 檔頭 https://en.wikipedia.org/wiki/Shebang_(Unix)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully