$30 off During Our Annual Pro Sale. View Details »

Linux Introduction (for Raspberry Pi Course)

Linux Introduction (for Raspberry Pi Course)

Tzu-ping Chung

August 19, 2015
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. speakerdeck.com/
    uranusjr

    View Slide

  2. Agenda
    • Introduction
    • The shell
    • Git
    • Linux topics
    • Writing code
    • Python basics
    • Python in shell
    • Extensions

    View Slide

  3. Introduction to Linux

    View Slide

  4. View Slide

  5. Me
    • Call me TP
    • https://uranusjr.com
    • Python, C++, OS X,
    iOS, Web
    • Tutorials and courses
    • Speaker
    • Django Girls Taipei


    View Slide

  6. Quick Polls
    • Something “not Windows”
    • Command line interface
    • Plain text editors
    • Programming

    View Slide

  7. View Slide

  8. View Slide

  9. Prompt

    View Slide

  10. pi@raspberrypi:~$

    View Slide

  11. pi@raspberrypi:~$
    ⢪欽罏

    View Slide

  12. pi@raspberrypi:~$
    ⚺堣せ珕

    View Slide

  13. pi@raspberrypi:~$
    騟䖜

    View Slide

  14. Shell
    • Core — Shell — User
    • Command line interface
    • Bash (Bourne-Again Shell)

    View Slide

  15. $  pwd  
    /home/pi

    View Slide

  16. $  mkdir  bedroom  

    View Slide

  17. $  ls  
    bedroom    Desktop

    View Slide

  18. $  touch  table  

    View Slide

  19. $  ls  
    bedroom    Desktop    table

    View Slide

  20. $  cd  bedroom  
    $  pwd  
    剙곎爙➊랁

    View Slide

  21. $  mkdir  closet  

    View Slide

  22. 銲䙥랁鹌Ⰵ  closet

    View Slide

  23. $  cd  closet  

    View Slide

  24. $  cd  ../..  
    植㖈➂㖈ㆬ酬♶銲⡲䒦

    View Slide

  25. 穩㼨騟䖜  
    /home/pi/bedroom/closet  
    湱㼨騟䖜  
    bedroom/closet

    View Slide

  26. ..  =  㢩꬗

    View Slide

  27. 現在位置(.)  
    /home/pi/bedroom/closet  
    外面兩層(../..)  
    /home/pi

    View Slide

  28. $  cd  ~  
    $  cp  -­‐r  bedroom  guest-­‐room  
    $  ls  
    剙⳿植➊랁

    View Slide

  29. $  cd  guest-­‐room  
    酬꬗僽やㄤ  bedroom  ♧垺剣♧
    ⦐  closet銲䙥랁墝叆

    View Slide

  30. 拷貝一樣東西  
    cp  something  
    拷貝一樣東西,以及裡面的所有東西  
    cp  -r  something

    View Slide

  31. 拷貝一樣東西  
    cp  something  
    拷貝一樣東西,以及裡面的所有東西  
    cp  -r  something
    չ⿭侶պ䧴չ䒷侶պ

    View Slide

  32. $  man  cp  
    ...  
    -­‐R,  -­‐r,  -­‐-­‐recursive  
           copy  directories  recursively

    View Slide

  33. $  cd  ..  
    鸍垺剙ⵋㆬ酬

    View Slide

  34. $  mv  guest-­‐room  living-­‐room  
    $  ls  
    剙⳿植➊랁

    View Slide

  35. $  mv  living-­‐room/closet  bedroom/closet2  
    鸍⦐䭷⟀⨞✪➊랁✲䞔

    View Slide

  36. $  rm  ~/table  
    $  rm  -­‐r  ~/bedroom  
    $  rm  -­‐r  ~/living-­‐room  
    敮嫅։

    View Slide

  37. The File System
    • The root directory
    • Other partitions are “mounted”
    • Things have their places
    • Names are case-sensitive

    View Slide

  38. /
    ├── bin
    ├── dev
    ├── etc
    ├── home
    │ └── pi
    ├── lib
    ├── media
    ├── opt
    ├── root
    ├── tmp
    ├── usr
    └── var

    View Slide

  39. Permissions
    • Essential for Un*x systems
    • Owner, group, others
    • Read, write, execute

    View Slide

  40. $  groups  
    pi  adm  dialout  cdrom  sudo  audio  
    video  plugdev  games  users  netdev  
    gpio  i2c  spi  input

    View Slide

  41. $  groups  pi  
    pi  :  pi  adm  dialout  cdrom  sudo  
    audio  video  plugdev  games  users  
    netdev  input  spi  i2c  gpio

    View Slide

  42. $  cd  /var  
    $  ls  -­‐l  
    total  102436  
    drwxr-­‐xr-­‐x  8  root  root    Aug  16  05:17  log  
    drwxrwsr-­‐x  2  root  mail    May    6  22:12  mail  
    -­‐rw-­‐-­‐-­‐-­‐-­‐-­‐-­‐  1  root  root    May    6  23:29  swap  
    drwxrwxrwt  2  root  root    Jan  11    2015  tmp

    View Slide

  43. 奚ꣳ垦硹 䠑纏
    r 隡《
    w 㻨Ⰵ
    x 㛂遤鹎Ⰵ湡ꏗ
    s ⟃暶㹁魨⟨㛂遤
    t ⫦乩剤罏〳ⵅꤑ荈䊹涸墂呪

    View Slide

  44. 奚ꣳ垦硹 䠑纏
    r 隡《
    w 㻨Ⰵ
    x 㛂遤鹎Ⰵ湡ꏗ
    s ⟃暶㹁魨⟨㛂遤
    t ⫦乩剤罏〳ⵅꤑ荈䊹涸墂呪

    View Slide

  45. $  cd  /var  
    $  ls  -­‐l  
    total  102436  
    drwxr-­‐xr-­‐x  8  root  root    Aug  16  05:17  log  
    drwxrwsr-­‐x  2  root  mail    May    6  22:12  mail  
    -­‐rw-­‐-­‐-­‐-­‐-­‐-­‐-­‐  1  root  root    May    6  23:29  swap  
    drwxrwxrwt  2  root  root    Jan  11    2015  tmp

    View Slide

  46. The Superuser
    • “root”
    • Can do anything
    • Log-in disabled by default

    View Slide

  47. /
    ├── bin
    ├── dev
    ├── etc
    ├── home
    │ └── pi
    ├── lib
    ├── media
    ├── opt
    ├── root
    ├── tmp
    ├── usr
    └── var
    Superuser’s
    home directory!

    View Slide

  48. sudo
    • “superuser do”
    • Run a command as
    superuser
    • Requires the sudo group

    View Slide

  49. $  groups  pi  
    pi  :  pi  adm  dialout  cdrom  sudo  
    audio  video  plugdev  games  users  
    netdev  input  spi  i2c  gpio

    View Slide

  50. $  whoami  
    pi  
    $  sudo  whoami  
    root

    View Slide

  51. Package Manager
    • APT = Advance package Tool
    • apt-get
    • apt-cache

    View Slide

  52. $  sudo  apt-­‐get  update  
    $  sudo  apt-­‐get  install  git

    View Slide

  53. Git Basics

    View Slide

  54. View Slide

  55. View Slide

  56. $  cd  ~  
    $  git  clone  https://github.com/raspberrypi-­‐
    tw/workshop.git  
    $  cd  workshop/gpio-­‐tutorial-­‐03/02_2-­‐
    led_blink/led_blink.py  
    $  ls  
    led.py

    View Slide

  57. $  python  led.py

    View Slide

  58. HJUDMPOF 䭡頺♧⦐(JU墂呪䏨
    HJUBEE墂呪
    䪾墂呪⸈Ⰵ墂呪䏨
    HJUBEE〳⟃♧妄⸈Ⰵ鿈墂呪
    HJUDPNNJUN鏽鎹 ⸈Ⰵ♧⦐晝劥
    HJUTUBUVT 叆溏湡⵹涸墂呪䏨朜䢀
    HJUEJ 叆溏湡⵹墂呪䏨ㄤ♳⦐晝劥⛓꟦涸䊴殯
    HJUMPH 叆溏晝劥鎹ꏗ
    HJUQVTI 㼟劥㖒墂呪䏨♳⫄荛黇畮
    HJUQVMM 刿倞劥㖒Ⰹ㺂

    View Slide

  59. How a Linux OS Boots

    View Slide

  60. Boot Sequence
    • Load the kernel (details omitted)
    • Prepare the system
    • Start services
    • Run custom startup hooks
    • User log in

    View Slide

  61. Boot Sequence
    • Load the kernel (details omitted)
    • Prepare the system
    • Start services
    • Run custom startup hooks
    • User log in

    View Slide

  62. Run Levels
    ꡠ堥
    ㋲➃笞隌垸䒭
    搂/'4㢵➃垸䒭
    㸤侮㢵➃垸䒭
    ⥃殆

    㕮䕎➝꬗
    ꅾꟚ堥

    View Slide

  63. $  ls  -­‐d  /etc/rc*  
    /etc/rc0.d    /etc/rc1.d    /etc/rc2.d  
    /etc/rc3.d    /etc/rc4.d    /etc/rc5.d  
    /etc/rc6.d    /etc/rc.local    /etc/rcS.d

    View Slide

  64. $  ls  -­‐d  /etc/rc*  
    /etc/rc0.d    /etc/rc1.d    /etc/rc2.d  
    /etc/rc3.d    /etc/rc4.d    /etc/rc5.d  
    /etc/rc6.d    /etc/rc.local    /etc/rcS.d

    View Slide

  65. raspberrypi  login:

    View Slide

  66. $  ls  -­‐A  ~  
    .bashrc      .bash_logout      .bash_profile  
    .profile  
    $  ls  -­‐d  /etc/profile*    
    /etc/profile    /etc/profile.d

    View Slide

  67. Summary
    • Startup
    • Run levels
    • Single-user and multi-user mode
    • Login shells and non-login shells

    View Slide

  68. $  nano

    View Slide

  69. View Slide

  70. ^R
    ➿邌
    Control

    View Slide

  71. 倰ぢ꒳ 獵⹛康垦
    1H6Q 1H%O )PNF &OE 獵⹛康垦
    ^O 㶸墂
    ^R Ꟛ㉬墂呪
    ^X 鷎ⴀ剚鑉㉏僽や⮭㶸墂呪
    ^W 䵃㼦
    ^K 䭡頺♧遤俒㶶
    ^U 顦♳⛓⵹䭡頺涸俒㶶

    View Slide

  72. Demo

    View Slide

  73. Error  writing  /etc/hosts:  Permission  denied

    View Slide

  74. Demo

    View Slide

  75. Try it!

    View Slide

  76. $  cd  ~  
    $  git  clone  https://github.com/serialhex/
    nano-­‐highlight.git  .nano  
    $  nano  .nanorc

    View Slide

  77. include  "~/.nano/python.nanorc"  
    set  tabsize  4  
    set  tabstospaces

    View Slide