UNIX Basics + shell commands
Michael Tsai 2017/03/06
Where UNIX started
• Multics OS project (1960s) @ Bell Labs
• UNIX on scavenged PDP-7 (1969)
• Space Travel game
• Good environment to do programming + a
“fellowship” could form.
• B —> C (1971 - 1973)
Reading: http://www.faqs.org/docs/artu/ch02s01.html
DEC PDP-7
Ken Thompson & Dennis Ritchie
Where LINUX started
• Linus Torvalds: announced the Linux project (1991)
- high cost of Sun’s Unix
• Parallel: 386BSD
• Linux has Internet capability & X (1993)
• GNU toolkit
• “20 years of open-source software in different UNIX platforms”
• A ”cheap UNIX system for everyone” Linus Benedict Torvalds Richard Stallman & FSF
Linux flavors
Linux族譜:
http://upload.wikimedia.org/wikipedia/commons/
1/1b/Linux_Distribution_Timeline.svg
SSH to remote system
• http://wslab.csie.ntu.edu.tw/ssh/
• Make sure your terminal handles 中⽂文 well keyword: encoding, UTF-8, or Big5
• Choose a good font. Make the font size larger.
Exercise 1: SSH login without password
• Make sure 中⽂文 is displayed correctly.
• Answer the question on zuvio:
terminal font and size.
• Disclaimer:
do NOT copy your private key over the network!
do this ONLY on your own computer!
• keyword: ssh-keygen
MAN: your online manual
• Your first man: man man
• Sections 1-9 of the man pages
• [] 可有可無
• | 選⼀一個
• … 重複
• man -k 要找的東⻄西 —> 列列出所有相關的
Choose your editor
• Standard editor: Vim
• For beginner: nano or joe
Pipes and redirection
• 0 (STDIN), 1 (STDOUT), 2 (STDERR)
• > : STDOUT 到檔案 (覆蓋)
>>: STDOUT 到檔案 (加到尾巴)
>&: STDOUT + STDERR 到檔案 2>: STDERR 到檔案
<: 檔案餵給STDIN
|:
• Example: echo “test message” > /tmp/blahblah
• Example: find / -name core 2> /dev/null
路路徑
• 樹狀狀的結構
• / : 根⽬目錄, 以及分隔. e.g., /home/hsinmu
• ~: 我的家⽬目錄
• ~hsinmu: hsinmu這個帳號的家⽬目錄
Some commands to learn
• 檔案管理理: ls, cd, mkdir, rm, mv, cp, find, pwd
• ⽂文字檔案處理理: cat, less, tail
• 程序管理理: ps, kill, top
• 寫shell script常⽤用: grep, sort, wc, cut, echo, tee
File attributes
Example:
ls -ld dsa/
drwxr-xr-x 2 hsinmu users 4096 10⽉月 14 2010 dsa1/
ld -l tmp
-rw-r--r-- 1 hsinmu users 12 3⽉月 9 16:08 tmp
• Role: owner, group owner, and others
• 檔案: x: 執⾏行行, w: 寫入, r: 讀取
• 對⽬目錄來來說:
x: 可以進去, r:列列出裡⾯面的檔案,
w:新建、刪掉⽬目錄中的檔案或改名
Change file permission/
ownership
• chmod: change file permission
• Examples:
chmod u+w blah
chmod 755 blah (7=rwx, 5=r-x) chmod ug=rw,o=r blah
chmod a-x blah chmod g=u blah
• chown: change file ownership
• Example:
chown nobody:nobody blah
More advanced permission control:
access control list (ACL)
• OS & filesystem dependent
• Identify user/group and then apply the permission
• POSIX-style ACLs are supported by ext* + a few other filesystems on Linux
Possible ACL entries
Format Example Sets permissions for
user::perms user::rw- The file’s owner
user:username:perms user:htlin:rw- A specific user
group::perms group::r-x The group that owns
the file
group:groupname:perms group:users:rw- A specific group
other::perms others::--- All others
mask::perms mask::rwx All but owner and other
ACL examples
• getfacl: get file access control lists Example: getfacl tmp
• setfacl: set file access control lists Example:
setfacl -m user::r,user:htlin:---,group:users:rw tmp setfacl -x user:htlin tmp
Shell
• We will teach bash “the Bourne-again shell”
• Default login shell on most systems
• Check if it is your current shell:
echo $SHELL
• If not, you need to change it.
Temp solution: bash -l
(run the shell as if it is a login shell.
避免環境參參數問題)
• Commands are either shell built-in or a script/executable
&& 和 ||
• &&: 前⾯面執⾏行行成功了了, 後⾯面才會執⾏行行
• ||: 前⾯面執⾏行行失敗了了, 後⾯面才會執⾏行行
• Example 1:
lpr /tmp/t2 && rm /tmp/t2
• Example 2:
cp —preserve —recursive /etc/* /spare/backup \
|| echo “Did NOT make backup”
變數
• 給值時直接⽤用
Example: hsinmu_dir=‘/nfs/home/hsinmu’
• 拿值的時候前⾯面加$
Example: echo $hsinmu_dir
• {}可以指定變數名稱到哪邊
Example: echo ${hsinmu_dir}-dir
• ‘’: 照著印出所有
“”: 替換裡⾯面該被執⾏行行的部分或變數
``: 執⾏行行裡⾯面的指令,並將output放在這個位置 Example 1: echo ‘my current work dir is `pwd`’
Example 2: echo “my current work dir is `pwd`”
Example: showusage
• Elements to learn:
• #!/bin/bash
• if else fi elif
• $# $0 $1 $2: command-line argument
• $#: 有幾個argument,
$0: 指令本⾝身,
$1, $2, …: 第幾個參參數
• function裡⾯面: $# 有幾個參參數, $1, $2, 第幾個參參數
test
String Numeric True if
x=y x -eq y x is equal to y x!=y x -ne y x is not equal
to y
x<y x -lt y x is less than y x<=y x -le y x is less or
equal to y x>y x -gt y x is greater
than y
x>=y x -ge y x is greater or equal to y
-n x x is not null
-z x x is null
-d file file exists and is a directory -e file file exists
-f file file exists and is a regular file -r file you have read
permission on -s file file exists and file
is not empty -w file you have write
permission on file1 -nt file2 file1 is newer file
than file2 file1 -ot file2 file1 is older
than file 2
Example: if [ $message_leve -le $LOG_LEVEL ]; then
Example: str_and_number
$((var)): 把var裡⾯面的東⻄西當作數學式⼦子計算並替換 a=1
b=$((2)) c=$a+$b
d=$(($a+$b))
List of other things you can read
• Regular expression: very powerful tool (with grep)!
(Hint: HW1)
• In bash shell script:
• while and for loop
• array
• vimtutor
• File attributes: setuid, setgid, and sticky bit
2.3
2.2