250 likes | 269 Views
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
E N D
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 • Easy for using / migrating • Completion & correction • Available in ports: shells/zsh
Introduction – ZSH (2/2) • % rsync -<TAB> • % cd /usr/lica<TAB>
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
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
IO 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
IO Redirection - Null Redirection • % < file • more or $READNULLCMD • % > file • cat or $NULLCMD
Filename 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)
Filename 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.)
Filename 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 • … $ ls 1ggyy.c MakeFile Makefile Mekefile Mikefile Mokefile Mukefile Nukefile makefile $ ls (#i)Make(#I)file Makefile makefile
Filename 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 • …
Filename 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 • …
Filename Generation – Reference • For further glob information • http://zsh.sourceforge.net/Doc/Release/Expansion.html#Filename-Generatio • Have a look on other types of expansion find(1) 是什麼小朋友? 可以吃嗎?Open! ZSH 愛用者
Script • Bourne shell based • Globbing • Expansion • zshexpn(1) • Portability • Use zsh built-ins if possible • Module support
Script – 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 • …
Script - Module • zsh/curses • zsh/mathfunc • zsh/net/tcp • zsh/example • … • Example • http://zshwiki.org/home/code/scripts/zshttpd
Completion • <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
Completion 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/
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
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
Completion Styles • Control the behavior of completers • zstyle ':completion:*' menu select • zstyle ':completion:*:kill:*' force-list always • zstyle ':completion:*' completer _complete _correct _approximate
Summing 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
Live Demo • 如果有東西壞了請當作沒有看到
Some 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