• 沒有找到結果。

UNIX Basics + 
 shell commands

N/A
N/A
Protected

Academic year: 2022

Share "UNIX Basics + 
 shell commands"

Copied!
24
0
0

加載中.... (立即查看全文)

全文

(1)

UNIX Basics + 
 shell commands

Michael Tsai 2017/03/06

(2)

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

(3)

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

(4)
(5)

Linux flavors

Linux族譜:

http://upload.wikimedia.org/wikipedia/commons/

1/1b/Linux_Distribution_Timeline.svg

(6)

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.

(7)

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

(8)

MAN: your online manual

Your first man: man man

Sections 1-9 of the man pages

[] 可有可無

| 選⼀一個

重複

man -k 要找的東⻄西 —> 列列出所有相關的

(9)

Choose your editor

Standard editor: Vim

For beginner: nano or joe

(10)

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

(11)

路路徑

樹狀狀的結構

/ : 根⽬目錄, 以及分隔. e.g., /home/hsinmu

~: 我的家⽬目錄

~hsinmu: hsinmu這個帳號的家⽬目錄

(12)

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

(13)

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:新建、刪掉⽬目錄中的檔案或改名

(14)

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

(15)

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

(16)

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

(17)

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

(18)

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

(19)

&& 和 ||

&&: 前⾯面執⾏行行成功了了, 後⾯面才會執⾏行行

||: 前⾯面執⾏行行失敗了了, 後⾯面才會執⾏行行

Example 1: 


lpr /tmp/t2 && rm /tmp/t2

Example 2: 


cp —preserve —recursive /etc/* /spare/backup \


|| echo “Did NOT make backup”

(20)

變數

給值時直接⽤用


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`”

(21)

Example: showusage

Elements to learn:

#!/bin/bash

if else fi elif

$# $0 $1 $2: command-line argument

$#: 有幾個argument, 


$0: 指令本⾝身,


$1, $2, …: 第幾個參參數

function裡⾯面: $# 有幾個參參數, $1, $2, 第幾個參參數

(22)

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

(23)

Example: str_and_number

$((var)): 把var裡⾯面的東⻄西當作數學式⼦子計算並替換 a=1

b=$((2)) c=$a+$b

d=$(($a+$b))

(24)

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

參考文獻

相關文件

It takes 20 cm of ribbon to make a bowknot which is 0.05 m less than a corsage?. Mary has a ribbon of 8

According to the passage, which of the following can help facilitate good sleep for children.. (A) Carefully choose the online content

* Before making a request from Interlibrary Loan service, please check the N TU Library online catalog to make sure the items are not NTU libraries' holdings.. * For

Midpoint break loops are useful for situations where the commands in the loop must be executed at least once, but where the decision to make an early termination is based on

• For novice writers, a good strategy is imitation: choose a well-written paper that is of a similar flavor, analyze its organization, and sketch an organization for your results

A Very good. You are able to apply your understanding of how endogenetic processes leading to the formation of major landform features along plate boundaries to explain the

Freely write an added part to the original motive, make sure your new part is of a different duration to the ostinato motive (say, more than two bars in length) so that they have

grep - print lines matching a pattern. $ grep [OPTIONS]