11B-43 NGUYỄN DUY TIẾN
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    # Learning Linux (Lai Nớt) ## Basic Commands * pwd: xem dir hiện tại * ls dir_name: list files in dir * cd: change dir * mkdir/rmdir: make and remove dir ## Working with files * File f1: determine the file type of f1 * touch f1: create f1 file * rm f1: remove file f1 * cp f1 f2: copy f1 qua f2 * mv f5 f6(file): change name from f5 to f6 * mv f5 QNH(dir): move f5 to QNH * locate f1: xem đường dẫn f1 * echo "hello" f1: đưa text hello vào file f1 * cat f1: display the hello from f1 #### Thao tác với vi cơ bản: * vi hello.txt: tạo\edit 1 file text tên hello, sau đó nhấn i để sang insert và ta có thể write dc, sau khi write xong, ta esc+:wq! để lưu lại và thoát ra * less hello.txt: view hello.txt * Esc + /word: tìm chữ trong file * Esc + :3 : nhảy đến dòng số 3 ## File Permission #### Thao tác với user #### sudo and su usage in Linux * [Guide to use sudo/su linux](https://www.atatus.com/blog/essential-guide-to-sudo-and-su-in-linux/#:~:text=su%20command%20in%20linux%20allows,the%20privileges%20of%20another%20user.) * root user is the administrator * every account having user id 0 is a root account in Linux * sudo su - : dùng để truy cập quyền admin * Linux users can't install software or update system directories; for say, they can't report to the root directory * If you want to do something that demands these permissions, you must obtain them with su or sudo commands in Linux. * useradd tien: add user tên tien * passwd tien: tạo password cho user tien * vi \etc\ssh\sshd_config: sửa cấu hình ssh * service sshd restart: restart ssh * su tien: nhập password và login vào * exit: dùng để logout user * userdel tien: xóa user tien * cat /etc/passwd | cut -d: -f1 : list all users #### Thao tác với nhóm * groupadd caulong: tạo nhóm chơi cầu lông * usermod -a -G caulong tien: add tien vào chơi cùng * id tien: ktra xem tien đã trong nhóm chưa #### File and folder permission <div> <p> <strong>Thứ tự phân quyền:</strong> (1) <i>Root</i> (2) <i>Group</i> (3) <i>Others</i> </p> <table> <tr>Bảng phân quyền</tr> <tr> <th>No.</th> <th>Permission Type</th> <th>Symbol</th> </tr> <tr> <td>0</td> <td>No Permission</td> <td>---</td> </tr> <tr> <td>1</td> <td>Execute</td> <td>--x</td> </tr> <tr> <td>2</td> <td>Write</td> <td>-w-</td> </tr> <tr> <td>3</td> <td>Execute + Write</td> <td>-wx</td> </tr> <tr> <td>4</td> <td>Read</td> <td>r--</td> </tr> <tr> <td>5</td> <td>Read + Execute</td> <td>r-x</td> </tr> <tr> <td>6</td> <td>Read + Write</td> <td>rw-</td> </tr> <tr> <td>7</td> <td>Read + Write + Execute</td> <td>rwx</td> </tr> </table> </div> <div> <p><b>Lệnh cấp quyền cho file và folder:</b> <code>chmod [quyền] [tên file hoặc thư mục]</code></p> <p>Ví dụ: <code>chmod -rf 777 ketoan</code></p> </div> <div> <p><b>Lệnh gán quyền cho user và group:</b> <code>chown [tên user]:[tên nhóm] [tên file hoặc tên thư mục]</code></p> <p> <b>Ví dụ</b>: <code>chown -rf bao:bao ketoan</code></p> </div> ## Package management <div> <p> <b>Package management</b> là một phương thức nhằm cài đặt và bảo trì các phần mềm được cài đặt trong hệ thống. </p> </div> <div> <ul><p>Có 3 thành phần chính trong packages management:</p> <li><b>Package files: </b> đơn vị cơ bản nhất của một package và được quản lý bởi maintainer, chứa tất cả thông tin cần thiết để cài đặt chương trình. </li> <li><b>Repositories: </b>Một nơi tập trung chứa các package files</li> <li><b>Dependencies:</b> </li> </ul> </div> <div> <p> Các loại <b>packaging system: </b> </p> <table> <tr> <th>Packaging system</th> <th>Distribution</th> </tr> <tr> <td>Debian-style (.deb)</td> <td>Debian, Ubuntu, Linux Mint, Raspbian</td> </tr> <tr> <td>Red Hat–style (.rpm)</td> <td>Fedora, CentOS, Red Hat Enterprise Linux, OpenSUSE</td> </tr> </table> Do yêu cầu là tìm hiểu về Linux (lai nớt) </div> Mai sẽ giải thích kỹ hơn về phần này + bonus giải thích sudo/su. #### Red Hat Packaging System * sudo yum search apache: tìm những packages liên quan apache * sudo yum update: update packages theo version mới nhất từ repos * rpm -U package-name: nếu những file package chứa version mới nhất đã tải về thì dùng low-level tool * rpm -qa: list packages in system * rpm -q package_name: xem package đã install chưa * yum info package_name: xem info package * rpm -qf file_name: xem file đó có trong package nào ## System Info * uname: kernel name * uname -a: display all system information * sudo lshw: show important hardware info such as memory. CPU. disks * ![](https://hackmd.io/_uploads/SJIF4OPC3.png) * lscpu: list CPU details * lsblk: show storage such as hard drive, partitions, ... * lsusb: list info about all USB controllers * cat /proc/cpuinfo: list CPU details * sudo dmidecode --type processor: kết xuất thông tin từ computer's DMI (SMBIOS) ## Networking <div> <p> <strong>Definition: </strong>To communicate between 2 or more computers internally or externally, we need <strong>Network</strong> to maintain and operate. </p> <p> <strong>References:</strong> <a href="https://www.javatpoint.com/linux-networking-commands"> Linux-networking-commands</a> </p> </div> Some useful commands: <div> <table> <tr> <th>Command </th> <th>Usage</th> <th>Syntax</th> </tr> <tr> <td><code>ifconfig</code></td> <td>Interface configurator, it is used to inspect network, initialize interface, enable or disable IP address and config an interface with IP address.</td> </tr> <tr> <td><code>ip</code></td> <td>New version of <code>ifconfig</code> command.</td> </tr> <tr> <td><code>traceroute</code></td> <td>It is used to balance the network. It identifies the delay and decides the pathway to our target.</td> </tr> <tr> <td><code>tracepath</code></td> <td>Similar to traceroute but doesn't require root privileges.</td> </tr> <tr> <td><code>ping</code></td> <td>Abbreviation of (Packet Internet Groper). It is used to check connectivity between two nodes.</td> </tr> <tr> <td><code>netstat</code></td> <td>Abbreviation of (Network statistics). It is used to display open sockets, routing tables, and connection information</td> </tr> <tr> <td><code>ss</code></td> <td>The replacement of netstat. Give more information and faster than netstat</td> </tr> <tr> <td><code>dig</code></td> <td>To query DNS related information.</td> </tr> <tr> <td><code>nslookup</code></td> <td>Find DNS related query.</td> </tr> <tr> <td><code>route</code></td> <td>Shows and manipulate IP routing table.</td> </tr> <tr> <td><code>host</code></td> <td>Performs DNS lookups.</td> </tr> <tr> <td><code>arp</code></td> <td>View or add contents of the kernel's ARP table.</td> </tr> <tr> <td><code>iwconfig</code></td> <td>Used to configure wireless network interface.</td> </tr> <tr> <td><code>hostname</code></td> <td>To identify a network name.</td> </tr> <tr> <td><code>curl or wget</code></td> <td>To download a file from Internet</td> </tr> <tr> <td><code>whois</code></td> <td>To tell about the website's whois.</td> </tr> <tr> <td><code>ifplugstatus</code></td> <td>To tell whether a cable is plugged in or not.</td> </tr> </table> </div> <div> <h2>User Environment</h2> <ul> <li>Environment Variable là 1 biến có tên và các giá trị liên quan</li> <li>Biến môi trường cho phép bạn tùy chỉnh cách hệ thống hoạt động và hoạt động của các ứng dụng trên hệ thống</li> <li>Ví dụ: biến môi trường có thể lưu trữ thông tin về trình soạn thảo văn bản hoặc trình duyệt mặc định , đường dẫn đến các tệp thực thi hoặc cài đặt bố cục bàn phím và hệ thống.</li> <table> <tr> <th>Commands</th> <th>Action</th> </tr> <tr> <td>env</td> <td>in ra all env variable</td> </tr> <tr> <td>printenv HOME</td> <td>In thông tin biến mt HOME</td> </tr> <tr> <td>set</td> <td>danh sách tất cả các biến: môi trường, shell và biến và hàm shell</td> </tr> <tr> <td>unset</td> <td>Xóa biến tạm thời ko cần nữa</td> </tr> <tr> <td>export</td> <td>Đặt biến môi trường</td> </tr> </table> <li>Shell là gì: 1 ctrinh nhận và dịch câu lệnh (command line) dưới dạng text, là interface tương tác với system.</li> <li>shell phổ biến: bash, sh, tcsh, csh, ksh, zsh</li> <li>Note: bash shell dùng nhiều nhất, sh là pointer trỏ đến bash shell hoặc shell</li> <li>Shell store 2 info trong environment:<ul> <li>shell variables: thiết lập bởi bash</li> <li>environment variables: tất cả biến còn lại</li> <li>shell cũng có thể lưu trữ **aliases** và **function**</li> </ul> </li> <li>MY_VAR="choigame": Tạo 1 biến shell mới tên là MY_VAR và giá trị là choigame</li> <li>printenv MY_VAR: check biến môi trường</li> <li>export MY_VAR: chuyển sang biến môi trường</li> <a href="(https://wiki.minhduy.vn/cach-set-va-liet-ke-bien-moi-truong-environment-variables-trong-linux.html)">link</a> </ul> </div> <div> <h2>Command line and Vim</h2> <h4>Command line</h4> <li>Internal command: bulit-in command trong shell</li> <li>External command: lệnh dc tìm trong biến $PATH và tạo 1 process để chạy nó nên internal ưu tiên hơn external</li> <h5>Stream</h5> ![](https://hackmd.io/_uploads/rk5QaHdCn.png) <h5>Control the Stream</h5> ![](https://hackmd.io/_uploads/ByukAHu0n.png) <h5>Example of redirect output</h5> <li>Redirect the output of ls -l to output.txt</li> ![](https://hackmd.io/_uploads/SyZD0BO0n.png) <li>Redirect the output of error to a black hole and cannot see that error</li> ![](https://hackmd.io/_uploads/SyOUk8OA3.png) ![](https://hackmd.io/_uploads/Sk7Ox8_A3.png) <li>Mục đích của lệnh Tee là đọc nội dung từ STDIN (standard input), xuất ra STDOUT (standard output) và ghi vào file.</li> <li>Lệnh "Tee" được sử dụng để lưu trữ và xem (cùng một lúc) output của các lệnh bất kỳ.</li> ![](https://hackmd.io/_uploads/BJMYXL_Rh.png) ![](https://hackmd.io/_uploads/rJALNUuCh.png) ![](https://hackmd.io/_uploads/SJYVrU_02.png) ![](https://hackmd.io/_uploads/rkGHvIO02.png) <li>Dùng xargs để đếm số files trong từng folder</li> ![](https://hackmd.io/_uploads/BJRxuUOAh.png) <li>Dùng xargs để kết hợp nội dung của all txt file lại</li> ![](https://hackmd.io/_uploads/rJe0dUdC3.png) <h4>Vim</h4> <ul> <li>Vim is an upgraded of vi</li> <li>Vi editor has 2 modes: Command and Insert mode</li> <li>Command mode: sau khi display file thì vào đầu tiên là command mode</li> <li>Insert mode: press i to enter, press esc to return command mode</li> <li>vi f1: display content of f1/ create f1</li> <li>colon (:) : give instructions to vi</li> </ul> </div> <table> <tr> <th>Commands</th> <th>Action</th> </tr> <tr> <td>:wq</td> <td>Save and quit</td> </tr> <tr> <td>:w</td> <td>Save</td> </tr> <tr> <td>:q</td> <td>Quit</td> </tr> <tr> <td>:w fname</td> <td>Save as fname</td> </tr> <tr> <td>ZZ</td> <td>Save and quit</td> </tr> <tr> <td>:q!</td> <td>Quit and not save</td> </tr> <tr> <td>:w!</td> <td>Save and write to non-writable file</td> </tr> </table> * Switch from command to insert mode: <table> <tr> <th>Commands</th> <th>Action</th> </tr> <tr> <td>i</td> <td>type before current char</td> </tr> <tr> <td>I</td> <td>type at start of current line</td> </tr> <tr> <td>a</td> <td>type after current char</td> </tr> <tr> <td>A</td> <td>type at end of current line</td> </tr> <tr> <td>o</td> <td>start at new line after current line</td> </tr> <tr> <td>O</td> <td>start at new line before current line</td> </tr> </table> * Move in a file trong command mode (ko dùng chuột): <table> <tr> <th>Commands</th> <th>Move</th> </tr> <tr> <td>j</td> <td>down</td> </tr> <tr> <td>k</td> <td>up</td> </tr> <tr> <td>h</td> <td>left</td> </tr> <tr> <td>l</td> <td>right</td> </tr> <tr> <td>G</td> <td>jump to last line</td> </tr> </table> * delete trong command mode: <table> <tr> <th>Commands</th> <th>action</th> </tr> <tr> <td>x</td> <td>delete char sau dấu nháy</td> </tr> <tr> <td>X</td> <td>delete char trc dấu nháy</td> </tr> <tr> <td>r</td> <td>replace char1 = char2</td> </tr> <tr> <td>xp</td> <td>switch 2 char next to</td> </tr> <tr> <td>dd</td> <td>del current line</td> </tr> <tr> <td>dG</td> <td>del current line from the end</td> </tr> </table> * repeat/undo command: <table> <tr> <th>Commands</th> <th>Action</th> </tr> <tr> <td>u</td> <td>undo last cmd</td> </tr> <tr> <td>.</td> <td>repeat last cmd</td> </tr> </table> <table> <tr> <th>Commands</th> <th>Action</th> </tr> <tr> <td>yy (yank yank)</td> <td>copy a line</td> </tr> <tr> <td>p</td> <td>past line under current</td> </tr> </tr> <tr> <td>P</td> <td>past line above current</td> </tr> </table> <h2>Text Commands</h2> <div> <a href="https://viblo.asia/p/co-ban-ve-thao-tac-voi-du-lieu-text-tren-linux-L4x5xyDBKBM">Link tham khảo_1</a> </br> <a href="https://viblo.asia/p/lpic-1-chuong-1-kham-pha-command-line-tren-linux-phan-3-xu-ly-text-bang-filter-trong-linux-cat-join-paste-sort-head-tail-wc-oOVlYGDyK8W">Link tham khảo_2</a> </br> <a href="https://viblo.asia/p/lpic-1-chuong-1-kham-pha-command-line-tren-linux-phan-4-regular-expression-1Je5E4gjlnL">Link tham khảo_3</a> </br> <caption><strong>Một số câu lệnh xử lý text cơ bản:</strong></caption> <ul> <li>Lệnh <strong><code>cat</code></strong>: Được sử dụng để tạo, chèn, hiển thị, và ghép nội dung của file. <ul> <li>Cách tạo file: <ul> <li>Cú pháp: <code>cat > file1</code></li> <li>Trong trường hợp file chưa tồn tại thì hệ thống sẽ tạo 1 file mới.</li> <li>Nếu file đã tồn tại thì sẽ bị ghi đè với nội dung mới</li> <li>Trong trường hợp không muốn bị ghi đè thì chúng ta sẽ sử dụng thêm 1 dấu <strong>">"</strong> , cụ thể: <strong><code>cat >> file1</code>. Lúc này dữ liệu sẽ được chèn vào cuối file.</strong></li> </ul> </li> </ul> </li> <li>Lệnh <strong><code>join</code></strong>: Sẽ giúp ta nối file theo hàng ngang thay vì nối hàng dọc như lệnh <code>head</code> <ul> <li>Cú pháp: <code>join file1 file2</code></li> </ul> </li> <li>Lệnh <strong><code>paste</code></strong>: Lệnh paste dùng để nối dòng với dòng, cách nhau bởi TAB, và không gộp chung key như join. <ul> <li>Cú pháp: <code>paste file1 file2</code></li> </ul> </li> <li>Lệnh <strong><code>wc</code></strong>: Lệnh này giúp đếm số lượng dữ liệu có trong file (word count).</li> <li>Cú pháp: <strong><code>wc file</code></strong></li> <li>Lệnh sẽ trả về 4 giá trị: <ul> <li>Số dòng</li> <li>Số từ</li> <li>Số ký tự</li> <li>Tên file</li> </ul> </li> <li>Lệnh <strong><code>grep</code></strong>: Dùng để hiển thị những dòng có nội dung nào đó <ul> <li>Cú pháp: <strong><code>grep "nội dung muốn hiển thị" file</code></strong></li> <li>Ngoài ra còn có những tùy chọn khác như hiển thị số dòng được hiển thị, không phân biệt chữ hoa thường, ...</li> </ul> </li> <li>Lệnh <strong><code>head</code></strong> và <strong><code>tail</code></strong>: Hai lệnh có cách sử dụng tương tự nhau, <code>head</code> lấy văn bản ở đầu file, <code>tail</code> lấy văn bản ở cuối file. <ul> <li> Ngoài ra chúng ta có thể thay đổi chức năng như <code>-nx</code> để lấy x dòng hoặc <code>-cx</code> để lấy x ký tự. </li> <li><strong>Ví dụ:</strong> Muốn lấy 8 dòng đầu từ file <code>text.txt</code> thì chúng ta sử dụng câu lệnh <code>head -n8 text.txt</code></li> </ul> </li> <li>Lệnh <strong><code>sort:</code></strong> Dùng để sắp xếp dữ liệu trong file <ul> <li>Cú pháp: <code>sort "filename"</code></li> <li>Ngoài ra, chúng ta có thể thêm các tùy chọn khác như <code>-u</code> để bỏ qua các giá trị trùng lặp, <code>-r</code> để sắp xếp theo thứ tự giảm dần, <code>-f</code> để không phân biệt in hoa hay thường.</li> </ul> </li> </ul> </div> <div> <h2>Bash Programming</h2> <p><a href="https://viblo.asia/p/lap-trinh-shell-bash-script-1VgZvER9KAw">Link tham khảo 1</a></p> <p><a href="https://viblo.asia/p/don-gian-hoa-tac-vu-trong-linux-voi-bash-script-phan-1-gGJ59gaGZX2">Link tham khảo 2</a></p> <p>Bash programming được sử dụng để viết 1 hoặc nhiều câu lệnh đơn lẻ vào để thực thi. Chúng được gọi là <strong>shell script</strong>. </p> </br> <p>Có nhiều cách để bash script được thực thi: Một là thực hiện từng câu lệnh riêng rẽ. Hai là viết tất cả các lệnh vào một file Bash <code>(*sh)</code> và thực thi.</p> </div> <div> <h4>Các ghi chú với Bash:</h4> <ul> <li> <strong>Đặt tên file:</strong> Không yêu cầu 1 điều kiện gì ngoại trừ điều kiện của hệ điều hành. Ví dụ: <code>my_document.sh</code></li> <li><strong>Dãy kí tự <code>#! (shebang): </code></strong> Dòng chứa kí tự này sẽ là dòng thông báo cho hệ điều hành biết file script này sẽ được thực thi bởi chương trình nào. Trong trường hợp không biết chính xác trình thông dịch của Bash nằm ở đâu, bạn có thể dùng lệnh <code>which bash</code>. Ví dụ: <code>#! /usr/bin/bash</code>. </li> <li><strong>Comment:</strong> Là những dòng lệnh chỉ có tác dụng chỉ dẫn hoặc mô tả và sẽ được bỏ qua trong file Bash, những dòng này sẽ được bắt đầu bằng dấu <code># (she)</code>. </li> <li><strong>Variable trong Bash:</strong> <ul>Các quy ước trong việc đặt tên biến: <li>Để khai báo một biến, ta sử dụng ký hiệu <strong>equal</strong> <code>(=)</code> đặt giữa tên và giá trị của biến (không được đặt bất cứ dấu space (<code> </code>) nào ở trước hoặc sau <code>=</code>.</li> <li>Ví dụ: <code>name='Viblo'</code>, <code>description='Knowledge sharing site'</code></li> <li>Để truy cập tới một biến, ta sử dụng ký hiệu dollar (<code>$</code>) ở ngay trước tên biến đó.</li> <li>Ví dụ: <code>$name</code>, <code>$description</code></li> </ul> <ul> <strong>Các kiểu dữ liệu trong Bash:</strong> <ul> <li><strong>string</strong>. Ví dụ: <code>name="phan phuc chua te AI"</code></li> <li><strong>number</strong>. Ví dụ: <code>age=16</code></li> <li><strong>constant</strong>.</li> <li><strong>array</strong>.</li> </ul> <li><strong>Lưu ý 1: Dấu <code>=</code> phải được viết liền, không có dấu cách</strong>.</li> <li><strong>Lưu ý 2: Biến sẽ phân biệt chữ hoa với chữ thường, ví dụ: Biến <code>name</code> sẽ khác biến <code>NAME</code></strong></li> </ul> </li> <li><strong>Tham số trong Bash:</strong> Bash cho phép được sử dụng một số tham số sau đây: <ul> <li><code>$0</code>: Tên của file script.</li> <li><code>$1 -> $9</code>: Các tham số truyền vào</li> <li><code>$#</code>: Số lượng của tham số truyền vào</li> <li><code>$*</code>: Danh sách các tham số được truyền vào. (các trường hợp <code>$#</code> và <code>$*</code> sẽ không bao gồm <code>$0</code>)</li> <li>Chúng ta có thể lấy kết quả của 1 câu lệnh khác và gán vào 1 biến khác. <ul> <li>Dùng dấu backtick. Cú pháp: <code>var_name=`command` </code></li> <li>Dùng dấu dollar. Cú pháp: <code>var_name=$(command) </code></li> </ul> </li> </ul> </li> <li><strong>Câu lệnh điều kiện: Bash hỗ trợ 3 dạng điều kiện:</strong> <ul>Dạng đơn giản: chỉ bao gồm một điều kiện, tùy theo điều kiện đúng hay sai mà sẽ thực hiện câu lệnh cụ thể. Cụ thể: <li> <p><code>if conditional ; then</code></p> <p><code> statement_1</code></p> <p><code>else</code></p> <p><code> statement_2</code></p> <p><code>fi</code></p> </li> </ul> <ul>Dạng đầy đủ: bao gồm từ 2 điều kiện trở lên. Cụ thể: <li> <p><code>if conditional_1 ; then</code></p> <p><code> statement_1</code></p> <p><code>elif conditional_2 ; then</code></p> <p><code> statement_2</code></p> <p><code>else </code></p> <p><code> statement_3</code></p> <p><code>fi</code></p> </li> </ul> <ul>Dạng mở rộng: có nhiều điện lồng nhau (nested). Cụ thể: <li> <p><code>if conditional_1 ; then</code></p> <p><code> if conditional_2 ; then</code></p> <p><code> statement_1</code></p> <p><code> else</code></p> <p><code> statement_2</code></p> <p><code> fi</code></p> <p><code>fi</code></p> </li> </ul> </li> <li><code>Conditional</code> có thể được sử dụng dưới 2 dạng: <ul> <li>Dùng <code>test</code> command: <code>test expression</code></li> <li>Dùng <code>[]</code> symbol: <code>[ expression ]</code></li> <ul>Ví dụ: <li><p><code>if [ $var = "true" ] ; then</code></p></li> <li><p><code>echo "Hello World!"</code></p></li> <li><p><code>else</code></p></li> <li><p><code>exit</code></p></li> <li><p><code>fi</code></p></li> </ul> </ul> </li> <li><strong>Vòng lặp <code>for</code></strong>: <ul> Có 2 dạng vòng lặp: <li>Vòng lặp <code>for</code>: <ul> <li><strong>Cú pháp: </strong><code>for (( exp1; exp2; exp3 )); do COMMANDS; done</code></li> <li><code>exp1</code> thường là biểu thức khởi tạo</li> <li><code>exp2</code> là biểu thức kiểm tra điều kiện, nếu không thỏa mãn vòng lặp sẽ dừng.</li> <li><code>exp3</code> là biểu thức thay đổi giá trị khởi tạo ở <code>exp1</code></li> <li><code>COMMANDS</code> bao gồm 1 hoặc nhiều câu lệnh được thực hiện trong khi điều kiện lặp vẫn thỏa mãn.</li> <li><strong>Ví dụ: </strong> bao gồm 1 hoặc nhiều câu lệnh được thực hiện trong khi điều kiện lặp vẫn thỏa mãn.</li> <li><code>for (( i=0; $i < 10; i=$i+l ) ) ; do echo $1; done</code></li> </ul> </li> <li>Vòng lặp <code>for each</code>: <ul> <li><strong>Cú pháp: </strong> <code>for NAME [in WORDS ... ]; do COMMANDS; done</code></li> <li>Ở dạng này, vòng lặp for tương đương với các vòng lặp mặc định trong các ngôn ngữ như <strong>Ruby, Python ...</strong> Các câu lệnh <code>COMMANDS</code> sẽ được thực hiện 1 lần cho mỗi giá trị được truyền vào trong danh sách <code>WORDS</code>, được gán và sử dụng thông qua <code>NAME</code></li> <li>Đặc biệt, nếu <code>WORDS</code> không được chỉ định cụ thể, vòng lặp sẽ tự động sử dụng danh sách tham số truyền vào file <code>Bash</code></li> </ul> </li> </ul> </li> <li><strong>Vòng lặp</strong> <code>while</code>: <ul> <li><strong>Cú pháp: </strong> <code>while CONDITION; do COMMANDS; done</code></li> <li>Không giống như vòng lặp <code>for</code> chỉ thực hiện dựa trên số lần lặp cố định (nếu không chỉ định <code>exp2</code> thì <code>for</code> sẽ trở thành vòng lặp vô hạn) , <code>while</code> không có số vòng lặp cụ thể, mà chỉ kết thúc khi <code>CONDITION</code> không còn thỏa mãn. Vì vậy, khi sử dụng <code>while</code>, ta cần chú ý thay đổi điều kiện khi cần thiết làm cho <code>CONDITION</code> có thể sai để vòng lặp kết thúc, tránh gây lãng phí bộ nhớ.</li> </ul> </li> <li><strong>Vòng lặp</strong> <code>until</code>: <ul> <li><strong>Cú pháp: </strong> <code>until CONDITION; do COMMANDS; done</code></li> <li>Vòng lặp <code>until</code> có thể coi là sự kế thừa của vòng lặp <code>repeat until</code> trong ngôn ngữ lập trình <strong>Pascal</strong></li> <li>Vòng lặp này chỉ khác vòng lặp <code>while</code> ở cách thực thi, ở đây, <code>COMMAND</code> sẽ còn thực hiện khi <code>CONDITION</code> không thỏa mãn (ở <code>while</code> thì thực hiện khi <code>CONDITION</code> thỏa mãn)</li> </ul> </li> <li><strong>Dừng điều kiện dừng vòng lặp:</strong> <ul> <li>Dừng theo điều kiện <strong>CONDITION:</strong> <ul> <li>Ở trên tôi có sử dụng các từ như <code>-lt</code>,<code>-ge</code>, đây là các cú pháp sử dụng khi muốn dừng vòng lặp tại <code>CONDITION</code>, ngoài ra <strong>Bash</strong> còn hỗ trợ các ký hiệu sau (chú ý các ký hiệu này không phải của <strong>Bash</strong> mà là của câu lệnh <code>test</code>): <ul> <li><code>-eq</code>: bằng (equal)</li> <li><code>-gt</code>: lớn hơn (greater than)</li> <li><code>-le</code>: nhỏ hơn hoặc bằng (little than or equal)</li> <li><code>-ne</code>: không bằng (not equal) Ngoài ra còn rất nhiều ký hiệu hữu ích khác mà bạn có thể dễ dàng kiểm tra thông qua câu lệnh <code>man test</code></li> </ul> </li> <li></li> </ul> </li> <li>Dừng theo điều kiện <strong>COMMAND</strong>: <ul><strong>Bash</strong> hỗ trợ 2 cú pháp bao gồm <code>continue</code> và <code>break</code>, tương tự như trong các ngôn ngữ lập trình phổ biến <li><code>continue</code>: thực hiện ngay lập tức lần lặp tiếp theo cho dù lần lặp hiện tại vẫn còn câu lệnh chưa thực hiện xong.</li> <li><code>break</code>: thực hiện việc kết thúc vòng lặp cho dù còn bao nhiêu lần lặp đi nữa.</li> </ul> </li> </ul> </li> </ul> </div> ## Systemd * Ý nghĩa tên: Systemd dùng để quản lý hệ thống (system management) nhưng ý nghĩ chữ d là daemon (MU) =)) - 1 thứ âm thầm hoạt động. Trong system, nó là những background process- các tiến trình này hoạt động liên tục (chạy ngầm). Users ko thể bk sự HĐ của nó. Nó cũng có tác dụng theo dõi các tiến trình khác hoạt động. ### Quản lý tiến trình trong linux * Hệ thống sẽ không thực sự quản lý toàn bộ các chương trình, mà chỉ quản lý khi nó được thực thi * mỗi chương trình chứa rất nhiều các đoạn mã máy (hay mã chỉ dẫn) giúp cho máy tính có thể biết được chương trình sẽ làm gì * Các đoạn mã này sẽ được nạp vào bộ nhớ, khi thực thi, nó được cấp phát vùng hoạt động, thời gian thực thi .... Và khi điều này xảy ra, thay vì gọi là chương trình, ta có một thuật ngữ khác là **tiến trình** * Những tiến trình này sẽ dc quản lý bởi HĐH/ hệ thống như linux, win,... * Linux có sự đa nhiệm * Lệnh **top**: giúp ta bk thông tin các tiến trình đang hđ ![](https://hackmd.io/_uploads/HyowNDO03.png) ![](https://hackmd.io/_uploads/rkGn4DORn.png) * Lệnh **ps** cũng hiển thị tiến trình nhưng ko realtime. ![](https://hackmd.io/_uploads/Hy9fBvdR2.png) * Kết thúc tiến trình: (khi ctrinh bị not responding) * ps aux | grep "chrome": dùng để hiện PID của tiến trình chrome * bonus: aux: a = show processes for all users u = display the process's user/owner x = also show processes not attached to a terminal * kill chrome: xóa tiến trình chrome ![](https://hackmd.io/_uploads/SJzXvvu0h.png) * kill cha thì con cũng die * Đọc thêm phần nhiệm vụ trc và sau nền: [link](https://viblo.asia/p/quan-ly-tien-trinh-trong-linux-YWOZrynvKQ0) ### Vai trò systemd * Systemd cung cấp một chương trình đặc biệt là /sbin/init và nó sẽ là chương trình đầu tiên được khởi động trong hệ thống (PID = 1) ### Các thành phần <table> <tr> <th>Thành phần</th> <th>Nhiệm vụ</th> </tr> <tr> <td>systemctl</td> <td>quản lý status dịch vụ hệ thống</td> </tr> <tr> <td>journald</td> <td>quản lý nhật ký (ghi log)</td> </tr> </tr> <tr> <td>logind</td> <td>Quản lý log in/out</td> </tr> <tr> <td>networkd</td> <td>Quản lý network connection</td> </tr> <tr> <td>timedated</td> <td>Quản lý time sys/network</td> </tr> <tr> <td>udev</td> <td>Quản lý device/firmware</td> </tr> </table> * Tất cả các chương trình được quản lý bởi systemd đều được thực thi dưới dạng background và được cấu hình thành 1 file configuration gọi là **unit file**. * Service là 1 unit file dc dùng nhiều. * Tạo service của systemd: * Ban đầu ta tạo 1 unit file: ```# Location: /etc/systemd/system/simple-echo.service [Unit] Description=Simple Echo [Service] Type = forking ExecStart = /usr/local/bin/simple-echo start ExecStop = /usr/local/bin/simple-echo stop ExecReload = /usr/local/bin/simple-echo restart [Install] WantedBy = multi-user.target ``` * Sau đó, tạo 1 file thực thi: ``` #!/bin/bash # Location: /usr/local/bin/simple-echo function echo_start () { echo "Echo: starting service ..." sleep 1 echo "Echo: starting done" } function echo_stop () { echo "Echo: stopping service ..." sleep 1 echo "Echo: stopping done" } function echo_status () { echo "Echo service" } case "$1" in 'start') echo_start ;; 'stop') echo_stop ;; 'restart') echo_start sleep 1 echo_stop ;; 'status') echo_status ;; *) echo "Usage: $0 {start|stop|status|restart}" >&2 ;; esac ``` * Linux là hdh open source. * Mỗi phân phối là 1 distro ### Cronjob #### Mot so cau lenh * systemctl status cron * Cron job chạy trong môi trường độc lập, bạn cần chỉ định đầy đủ đường dẫn tuyệt đối cho tệp hoặc lệnh. * Ghi log output để dễ dàng kiểm tra ``` # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | ``` * Cách để chạy cronjob: 1. Tạo file.sh, cấp quyền chmod. 2. Chạy crontab -e để edit file cronjob. 3. Điền nội dung job: * * * * * user file.sh 4. dùng crontab -l để list các cronjob 5. dùng crontab -r để xóa hết cronjob * Example: ``` */2 * * * * /home/ubuntu/write_log.sh >> /home/ubuntu/test.log */4 * * * * /home/ubuntu/backup.sh /home/ubuntu/test.log ```

    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