Z Shell – the last shell you’ll ever need

25
Z Shell Z Shell – the last shell – the last shell you’ll ever need you’ll ever need changlp

description

Z Shell – the last shell you’ll ever need. changlp. Introduction - Shell. 殼 Fetch → Analyze → Execute Fork Type. Introduction – ZSH (1/2). Originally by Paul Falstad in 1990 A shell designed for interactive use Powerful user experience Use sh as underlying syntax - PowerPoint PPT Presentation

Transcript of Z Shell – the last shell you’ll ever need

Page 1: Z Shell         – the last shell you’ll ever need

Z ShellZ Shell – the last shell you’ll ever need – the last shell you’ll ever need

changlp

Page 2: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

2

Introduction - ShellIntroduction - Shell

殼 Fetch → Analyze → Execute

• Fork

Type

Login Non-login

Interactive User’s shell Shell executed after login

Non-interactive

? Script

Page 3: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

3

Introduction – ZSH (1/2)Introduction – ZSH (1/2)

Originally by Paul Falstad in 1990 A shell designed for interactive use

• Powerful user experience

Use sh as underlying syntax• Easy for using / migrating

Completion & correction Available in ports: shells/zsh

Page 4: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

4

Introduction – ZSH (2/2)Introduction – ZSH (2/2)

% rsync -<TAB>

% cd /usr/lica<TAB>

Page 5: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

5

Startup Files (1/2)Startup Files (1/2)

For all ZSH• /etc/zshenv

• ~/.zshenv

If [ login shell ]• /etc/zprofile

• ~/.zprofile

If [ interactive ]• /etc/zshrc

• ~/.zshrc

If [ login shell ]• /etc/(z)login

• ~/.(z)login

Login Non-login

Interactive User’s shell Shell executed after login

Non-interactive

? Script

Page 6: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

6

Startup Files (2/2)Startup Files (2/2)

zshenv• Settings for ALL types of ZSH

zprofile• Settings for each login session (path … etc)

zshrc• Some interactive settings (prompt … etc)

zlogin (zlogout)• Execute some program on login / logout

Page 7: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

7

IO Redirection - MultiosIO Redirection - Multios

% date >file1 >file2 … >fileN % date >file1 | prog1 % cat -n < /var/log/**/*log(R.)

• All files under /var/log and its subdir with 0004 permission

% cat bar | sort <foo• % cat bar foo | sort # note the order

Page 8: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

8

IO Redirection - Null RedirectionIO Redirection - Null Redirection

% < file• more or $READNULLCMD

% > file• cat or $NULLCMD

Page 9: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

9

Filename Gerneration – Extended GlobbingFilename Gerneration – Extended Globbing

Common shells support simple globbing• ls –lh *

• ls –lh *.[co]

• ls –lh *.[^oh]

ZSH supports extended globbing• ls –lh ^*.c

• ls –lh ^Makefile

• Option: EXTENDEDGLOB (default on)

Page 10: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

10

Filename Generation – GlobbingFilename Generation – Globbing

Recursive globbing **/

All dirs and subdirs

***/ Also follow symbolic links

• Ex. $ ls –lh **/foo Searches foo under all dir & subdir

Form: (flags)pattern,operator …(qualifier)• Ex. $ ls –lh (#i)[[:digit:]]ggyy.[^oh](R.)

Page 11: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

11

Filename Generation – Globbing flagsFilename Generation – Globbing flags

Globbing flags Affect to their right

Until end of enclosing group or end of the pattern

May interleave with operators $ ls -lh (#i)Make(#I)file

• #I/i: case sensitivity

• #cM,N: {M,N} in regex

• #a: Approximate

• …

$ ls1ggyy.c MakeFile Makefile Mekefile Mikefile Mokefile Mukefile Nukefile makefile$ ls (#i)Make(#I)fileMakefile makefile

Page 12: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

12

Filename Generation – Globbing operatorFilename Generation – Globbing operator

Globbing operator Cooperate with patterns Like regex

• * This is self explanatory

• [] Match enclosed characters Named class

– [:alpha:], [:digit:]…

Use “^” or “!” for negation

• () Grouping

• …

Page 13: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

13

Filename Generation – Globbing qualifierFilename Generation – Globbing qualifier

Globbing qualifier Specify which filenames should be argument list

• /: directories

• .: plain files

• @: links

• +cmd: filename will be included if cmd returns a zero status

• …

Page 14: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

14

Filename Generation – ReferenceFilename Generation – Reference

For further glob information• http://

zsh.sourceforge.net/Doc/Release/Expansion.html#Filename-Generatio

• Have a look on other types of expansion

ZSH 愛用者

find(1) 是什麼小朋友 ?可以吃嗎 ?Open!

Page 15: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

15

ScriptScript

Bourne shell based Globbing Expansion

• zshexpn(1)

Portability• Use zsh built-ins if possible

• Module support

Page 16: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

16

Script – Type SystemScript – Type System

Data Types• Array

Slice

• Scalar (string)

• Float

• Integer

• Association (hash) % typeset -A blah

% typeset -r agent• Constant

% typeset -U unique_set• Unique set

Page 17: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

17

Script - ModuleScript - Module

zsh/curses zsh/mathfunc zsh/net/tcp zsh/example …

Example• http://zshwiki.org/home/code/scripts/zshttpd

Page 18: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

18

CompletionCompletion

<TAB>, and shell tries to fill the rest In (t)csh

• More in: /usr/share/examples/tcsh/complete.tcsh

In bash

• More in: shells/bash-completion

Completion distributed separately

complete sudo 'p/1/c/'complete chown 'p/1/u/‘complete cd ‘p/1/d/’

complete -f -X '!*.@(Z|[gGd]z|t[ag]z)' gunzip zcat unpigz

Page 19: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

19

Completion in ZSHCompletion in ZSH

Programmable completion Context sensitive

• % echo $OS<TAB>

• % cd <TAB>

• % ssh <TAB>

Completion function distributed with ZSH• [FreeBSD] See

/usr/local/share/zsh/<version>/functions/Completion/

Page 20: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

20

Completion Context (1/2)Completion Context (1/2)

:completion:<func>:<completer>:<command>:<argument>:<tag>• Current context while completing

• How completion arrived here so far

Func• Blank if called from standard completion system

Completer• Completer name (‘completer’ in standard completion case)

• Correction… etc

Command Argument Tag

• Things can be completed from this point

Page 21: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

21

Completion Context (2/2)Completion Context (2/2)

% cd• :completion::complete:cd::

% git add• :completion::complete:git-add:: (+ more arguments)

• :completion::complete:git:: (+ more subcommands)

Tag: modified-files other-files ignored-modified-files ignored-other-files untracked-files

Page 22: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

22

Completion StylesCompletion Styles

Control the behavior of completers zstyle ':completion:*' menu select zstyle ':completion:*:kill:*' force-list always zstyle ':completion:*' completer _complete _correct

_approximate

Page 23: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

23

Summing UpSumming Up

Pros• Powerful completion system

Good for interactive Built-in completion support for most UNIX programs

Cons• Bad for

• Slow Completion system overhead

• Complex configuration

Page 24: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

24

Live DemoLive Demo

如果有東西壞了請當作沒有看到

Page 25: Z Shell         – the last shell you’ll ever need

Com

pu

ter C

en

ter, C

S, N

CTU

25

Some Plugins & ResourseSome Plugins & Resourse

https://github.com/zsh-users/zsh-syntax-highlighting https://github.com/robbyrussell/oh-my-zsh From Bash to Z Shell: Conquering the Command Line ZSH manual