Sie sind auf Seite 1von 63

Advanced UNIX Tutorial

Munteanu, Mircea
based on slides by Shelling, Otheus

For University of Innsbruck, November 2007

Topics Covered
Why Unix? Brief History Architecture Inter-process Communication Filesystem Regular Expressions Program Execution
PATH to enlightenment
root:/ $

The Shells
BASH Programming Shell Startup Short-cuts

Cool Commands
2

Why UNIX? And Why Not?


Well-designed Unix: born in 1970 at AT&T/Bell Labs Linux: born in 1992 in Helsinki, Finland High Performance Fine-grain control over hardware Fine-grain control over OS WYSIWYG .. Not WYSIAYG
Windows has caught up in performance Windows is easier to administer Windows systems dont require fine-grain control 3

Currently there are different versions of UNIX such as SunOS, Linux, DEC OSF/1, AIX, HP-UX 10, Solaris, IRIX, SCO UNIX,etc.

Flavors, Versions, Distributions


Flavors UNIX comes in various Flavors
System V (Solaris), BSD (FreeBSD), Linux

Distributions A particular Flavor will be packaged and marketed through various Distributions
FreeBSD, RedHat, Gentoo

Versions A particular Distribution will be released with a specific Version number


RedHat EL4 US, Solaris 10 (2.10)

Other version designations The kernel and various software tools and applications each have their own version numbering
X11R6, Linux 2.6, Perl 5.6
4

UNIX History
Began in 1969 as Unics
Labs (AT&T) by

at Bell

Ken Thompson

Dennis Ritchie (C) Brian Kernighan (AWK, m4) Douglas McIlroy (pipes)
Bill Joy (vi, csh, NFS)

Other pioneers:

1st Programming manual in 1971 Rewritten in C in 1973 Berkeley - added TCP/IP support and
Network Sockets

GNU project started in 1984:


Richard Stallman (gcc, emacs) Brian Fox (bash, info)

Landmark releases:
System V

BSD 4.3

POSIX definition 1986 Portable Operating System unIX


5

UNIX History
X11 released in 1987
Sun Microsystems
SunOS 1.x to 4.x: BSD based SunOS 5.x (aka Solaris 2.x) System V based Solaris 2.10 (aka Solaris 10)
released in 2005

1989 Intel releases 80386 Linux begins in 1991: Linus Torvalds Alan Cox (network code) Marc Ewing (RedHat)

BSD forks into


FreeBSD
NetBSD OpenBSD

UNIX Architecture System Level


Devices managed by drivers

Drivers interact through an API with the Kernel

Kernel manages core resources Processes Memory (physical and virtual) Interprocess communication (IPC) presents to users: system calls device files

UNIX Architecture User Level


User:
interacts with an application through a device

Application:
commands ls, qsub, run-time environment bash, perl, python, Java, PHP, mozilla subsystem X Windows, SGE
[prompt]$ <command> <flags> <args>

fiji:~$ ls l -a unix-tutorial

Shared libraries:
glibc, gtk
Each application is in its own process and has its own private data. The code can be shared across instances. Each process runs under the permissions of the user that started it effective user id The application and its libraries make system calls and operates on files.

Inter-Process Communication

How process communicate

Inter-Process communication (IPC)


Semaphores Shared Memory Filesystem Pipes RPC Signals
[mircea@ example]$ ls -1 example example2 myexample sample [mircea@ example]$ ls -1 >files [mircea@ example]$ sort -k 1.2,1.4 files sample files example example2 myexample

10

Inter-Process communication (IPC)


Semaphores Shared Memory Filesystem Pipes
Unnamed Named / FIFOs Network Sockets
[mircea@ example]$ ls -1 example example2 myexample sample [mircea@ example]$ ls -1 >files [mircea@ example]$ sort -k 1.2,1.4 files sample files example example2 myexample [mircea@example]$cat files |grep sample sample

RPC Signals

11

Inter-Process communication (IPC)


Semaphores Shared Memory Filesystem Pipes
Unnamed Named / FIFOs Network Sockets
*mircea* ~/eg$ mkfifo newfifo *mircea* ~/eg$ *mircea* ~/eg$ ls -l newfifo prw-r--r-- 1 mircea dps 0 Mar 28 19:52 newfifo
*mircea* ~/eg$ while true; do echo $RANDOM > /home/mircea/example/newfifo; sleep 5; done On a different console: *mircea* ~/eg$ ls -l newfifo prw-r--r-- 1 mircea dps 0 Mar 28 19:52 newfifo *mircea* ~/eg$ date ; cat newfifo ; date Fri Nov 29556 Fri Nov 9 11:36:25 CET 2007 9 11:36:25 CET 2007

RPC Signals

*mircea* ~/eg$ date ; cat newfifo ; date Fri Nov 20321 Fri Nov 9 11:36:49 CET 2007 9 11:36:49 CET 2007

12

Inter-Process communication (IPC)


Semaphores Shared Memory Filesystem Pipes
Unnamed Named / FIFOs Network Sockets
2-way pipes over a network mircea@kreusspitze example]$ sudo netstat -anp >|grep -E "(178):(3306|80|22)"|head -3

Tcp 0 0 192.168.69.178:80 ESTABLISHED Tcp 0 0 192.168.64.178:22 ESTABLISHED 18199/11

Tcp 0 0 192.168.64.178:22 ESTABLISHED 18691/16

RPC Signals

13

Inter-Process communication (IPC)


Semaphores Shared Memory Filesystem Pipes RPC
RPC process Portmapper process YP/NIS & NFS
*mircea* ~/eg$ ps -e -o uid,pid,cmd | > grep rpc 29 3338 0 3599 0 4093 0 4274 0 4312 0 4323 201 17112 rpc.statd rpc.idmapd rpc.yppasswdd -e chsh -e chfn rpc.rquotad [rpciod] rpc.mountd grep rpc

*mircea* ~/eg$ rpcinfo -p | uniq -f 4 program vers proto 100000 2 tcp 100024 1 udp 100007 2 udp 100004 2 udp 100009 1 udp 100011 1 udp 100003 2 udp 100021 1 udp 100005 1 udp port 111 32768 856 845 878 640 2049 32774 686

Signals

portmapper status ypbind ypserv yppasswdd rquotad nfs nlockmgr mountd

14

Inter-Process communication (IPC)


Semaphores Shared Memory Filesystem Pipes RPC Signals
Only within a process family Processes 0, -1

*mircea* ~/eg$ kill -l | head -8 1) SIGHUP 2) SIGINT 3) SIGQUIT 5) SIGTRAP 6) SIGABRT 7) SIGBUS 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 13) SIGPIPE 14) SIGALRM 15) SIGTERM 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 22) SIGTTOU 23) SIGURG 24) SIGXCPU 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
*mircea* ~/eg$ echo $$ 2432 *mircea* ~/eg$ /bin/ash *\u* \w$ echo $$ 18201 *\u* \w$ kill -HUP 18201 Hangup *mircea* ~/eg$ echo $$ 2432 *mircea* ~/eg$ trap & [1] 18664 *mircea* ~/eg$ kill -1 18664 ; sleep 1 Caught signal! *mircea* ~/eg$ kill 18664 ; sleep 1 [1]+ Terminated trap *mircea* ~/eg$

4) 8) 12) 17) 21) 25) 29) 35)

SIGILL SIGFPE SIGUSR2 SIGCHLD SIGTTIN SIGXFSZ SIGIO SIGRTMI

SIGHUP SIGTERM SIGKILL SIGSEGV

15

UNIX Filesystem
Tree Structure Promiscuous Permissions

16

UNIX Filesystem Tree Structure


Tree Structure
File-types
files directories soft-links fifos network sockets character devices block devices
drwxr-xr-x lrwxrwxrwx srw-rw-rwbrw-rw---crw-rw-rw-rw-r--r-2 1 1 1 1 2 root root root root root root root root root disk root root 4096 4 0 8, 0 5, 0 1132 Aug Mar Oct Oct Oct Mar 30 26 28 28 28 20 2006 2006 12:51 12:50 21:40 16:40 /bin /bin/csh /dev/log /dev/sda /dev/tty /etc/hosts

brw------crw-r--r-prw-r--r-srw-r--r-mrw-r--r-lrwxrwxrwx

1 1 1 1 1 1

root root root root root root

sys sys sys sys sys sys

bdevfile cdevfile namedpipe semaphore sharedmem symlink -> /etc/passwd

Mounting drives Breaking structure

Promiscuous Permissions

bdevfile: cdevfile: dir: disks.html: namedpipe: semaphore: sharedmem: symlink:

block special (1/0) character special (2/1) directory ascii text fifo Xenix semaphore Xenix shared memory handle ascii text

17

UNIX Filesystem Tree Structure


Tree Structure
File-types Mounting drives Breaking structure

Promiscuous Permissions
# This file is edited by fstab-sync - see 'man fstab-sync' for details /dev/md0 / ext3 defaults 1 1 /dev/sda1 /boot ext3 defaults 1 2 none /dev/pts devpts gid=5,mode=620 0 0 none /dev/shm tmpfs defaults 0 0 none /proc proc defaults 0 0 none /sys sysfs defaults 0 0 /dev/md1 /var ext3 defaults 1 2 #/boot/swap swap swap defaults 0 0 /dev/sda2 swap swap defaults 0 0 /dev/hdc /media/cdrom auto pamconsole,exec,noauto,managed 0 0 /dev/fd0 /media/floppy auto pamconsole,exec,noauto,managed 0 0

18

UNIX Filesystem Tree Structure


Tree Structure
File-types Mounting drives
Reuqires root
*mircea* ~$ mount /media/cdrom mount: only root can mount /dev/hda on /media/cdrom *mircea* ~$ mount /dev/md0 on / type ext3 (rw) none on /proc type proc (rw) none on /sys type sysfs (rw) none on /dev/pts type devpts (rw,gid=5,mode=620) usbfs on /proc/bus/usb type usbfs (rw) /dev/sde1 on /boot type ext3 (rw) none on /dev/shm type tmpfs (rw) <truncated>

Breaking structure
Soft-links Loopback mounts Bind mounts

Promiscuous Permissions

19

UNIX Filesystem Tree Structure


Tree Structure
File-types Mounting drives Breaking structure
Soft-links Loopback mounts Bind mounts
*mircea* ~/eg$ ln -s /etc etc *mircea* ~/eg$ ls -l etc lrwxrwxrwx 1 mircea dps 4 Oct 25 08:41 etc -> /etc *mircea* ~/eg$ pwd /home/mircea/eg *mircea* ~/eg$ cd etc /home/mircea/eg/etc *mircea* ~/eg/etc$ pwd /home/mircea/eg/etc *mircea* ~/eg/etc$ /bin/pwd /etc *mircea* ~/eg/etc$

Promiscuous Permissions

20

UNIX Filesystem Tree Structure


Tree Structure
File-types Mounting drives Breaking structure
Soft-links Loopback mounts Bind mounts
*mircea* ~/eg$ sudo vim /etc/exports *mircea* ~/eg$ tail -1 /etc/exports /etc 127.0.0.1(ro,no_root_squash) *mircea* ~/eg$ sudo exportfs -a -r *mircea* ~/eg$ *mircea* ~/eg$ rm etc *mircea* ~/eg$ mkdir etc *mircea* ~/eg$ chmod 755 etc *mircea* ~/eg$ sudo \ mount localhost:/etc $PWD/etc *mircea* ~/eg$ cd ./etc *mircea* ~/eg/etc$ /bin/pwd /home/mircea/eg/etc *mircea* ~/eg/etc$

Promiscuous Permissions

21

UNIX Filesystem Tree Structure


Tree Structure
File-types Mounting drives Breaking structure
Soft-links Loopback mounts Bind mounts
--bind (Linux) -F bindfs (Solaris)
*mircea* ~/eg$ sudo \ mount --bind /etc $PWD/etc *mircea* ~/eg$ cd ./etc *mircea* ~/eg/etc$ /bin/pwd /home/mircea/eg/etc *mircea* ~/eg/etc$ sudo umount etc

Promiscuous Permissions

22

UNIX Filesystem Tree Structure


Tree Structure
File-types Mounting drives Breaking structure
Soft-links Loopback mounts Bind mounts
--bind (Linux) -F bindfs (Solaris) absolute/relative Path

Absolute Pathnames /users/usern/file1 Relative paths start in the current directory usern/file1
Your home directory ~ is where your personal files are located, and where you start when you log in. Handy directories to know ~ Your home directory .. The parent directory . The current directory

Promiscuous Permissions

23

UNIX Filesystem Very Promiscuous


Tree Structure Promiscuous:
Nearly everything in UNIX can be accessed through the filesystem
Devices Processes (Linux & Solaris) via /proc filesystem Kernel stats and configuration (Linux: /proc) Exception: Network interfaces

Permissions

24

UNIX Filesystem Very Promiscuous


Resembles a tree Promiscuous
Devices Processes Kernel stats and configuration
MAKEDEV XOR arpd cdrom cdrom1 console core cpu cpu0 cpu1 cpu2 cpu3 dnrtmsg dvd fd floppy full fwmonitor hda hw_random initctl input ip6_fw js0 kmsg log loop0 loop1 loop2 loop3 loop4 loop5 loop6 loop7 lp0 lp1 lp2 lp3 mapper mcelog md0 mem mice mosal net nflog null parport0 parport1 parport2 parport3 port ppp ptmx pts ram ram0 ram1 ram10 ram11 ram12 ram13 ram14 ram15 ram2 ram3 ram4 ram5 ram6 ram7 ram8 ram9 ramdisk random rawctl root route route6 rtc scd0 sda sdb sdc sdd sde sde1 sde2 sde3 sdf sdf1 sdf2 sdf3 shm skip stderr stdin stdout systty tap0 tap1 tap10 tap11 tap12 tap13 tap14 tap15 tap2 tap3 tap4 tap5 tap6 tap7 tap8 tap9 tcpdiag ts_ua0 ts_ua1 ts_ua2 ts_ua3 ts_ua4 ts_ua5 ts_ua6 tty0 tty1 tty2 tty3 tty4 tty5 tty6 tty7 tty8 tty9 urandom usersock vcs vcs1 vcs2 vcs3 vcs4 vcs5 vcs6 vcsa vcsa1 vcsa2 vcsa3 vcsa4 vcsa5 vcsa6 vipkl xfrm zero

Permissions

25

UNIX Filesystem Very Promiscuous


Resembles a tree Promiscuous
Devices Processes Kernel stats and configuration
*mircea* /proc$ ls -Cd [1-2]* 1 14642 17888 21007 21942 258 10 14644 17889 21008 21943 2619 10377 14657 17890 21009 2251 2620 10395 15 17892 21010 2269 2647 11 155 17894 21017 2270 2648 11639 17473 19570 21019 2312 2649 12 17476 2 21020 2313 2650 13 17478 20998 21021 240 2658 13956 17511 20999 21022 241 27265 13964 17871 21000 2121 2431 286 14 17884 21002 21646 2432 1433 17887 21003 21941 257 *mircea* /proc$ cat 1/cmdline; echo init [4] *mircea* /proc$ ls -F 1 attr/ environ maps stat wchan auxv exe@ mem statm cmdline fd/ mounts status cwd@ loginuid root@ task/ *mircea* /proc$
26

Permissions

UNIX Filesystem Very Promiscuous


Resembles a tree Promiscuous
Devices Processes Kernel stats and configuration
*mircea* /proc$ ls -CdF [a-z]* acpi/ iomem net/ buddyinfo ioports partitions bus/ irq/ pci cmdline kallsyms scsi/ cpuinfo kcore self@ crypto key-users slabinfo devices keys stat diskstats kmsg swaps dma loadavg sys/ driver/ locks sysrq-trigger execdomains mdstat sysvipc/ fb meminfo topspin@ filesystems misc tty/ fs/ modules uptime ide/ mounts@ version infiniband/ mpt/ vmstat interrupts mtrr *mircea* /proc$ cat uptime 704398.43 593929.58 *mircea* /proc$ uptime 20:56:58 up 8 days, 3:39, 7 users, load average: 1.72, 1.75, 1.67 *mircea* /proc$

Permissions

27

UNIX Filesystem Security


Tree Structure Promiscuous Permissions
3 Classes:
User Group Other (World)
*mircea* ~/eg$ ls -l pubfile -rwxrwxrwx 1 mircea dps 0 Oct 29 08:55 pubfile

Group name

Owner name

4 bits:
Read Write Execute Special

others permissions

groups permissions User/owners permissions


1 --x execute 2 -w- write 3 -wx write and execute 4 r read 5 r-x read and execute 6 rw- read and write 7 rwx read, write and execute

Octal notation

28

UNIX Filesystem Security


Resembles a tree Promiscuous Permissions
3 Classes:
User Group Other (World)
File Permission Bits
Char Octal Permission to...
read file

w
x

2
1

write / overwrite file


execute file

754 = "-rwxr-xr--" = rwx for user, r-x for group, r-- for other

4 bits:
Read Write Execute Special

Directory Permission Bits

Char

Octal

Permission to...
read contents (ls) create new entires rename entries remove entries

r w

4 2

cd into operate on files in this directory


29

UNIX Filesystem Security


Resembles a tree Promiscuous Permissions
4 bits:
Read Write Execute Special
Directories Set-uid Set-gid

permissions set for a directory apply to every file in that directory and sub-directories,unsess specified Chmod to change read, write, and execute permissions for files and directories. Chown to change ownership of files and directories. Chgrp to change the group for files and directories.

chmod u=rwx, g=r, o=wr my_file


chmod 751 my_file

chmod u+x my_file

30

UNIX Filesystem Security


Resembles a tree Promiscuous Permissions
4 bits:
Read Write Execute Special
Directories Set-uid Set-gid

* Set User IDand"Set Group ID * are access right flags that can be assigned to files and directories needed for tasks that require higher privileges than those which a common user has
* mircea@$ ll /bin/ping

-rwsr-xr-x 1 root root 36568 Jul 30 2006 /bin/ping

31

Regular Expressions
One of computings most powerful information retrieval tools

32

Regular Expressions Quick Overview


Regular Expressions a string that represents multiple instances It can be used along with grep,find,ls to search for pattern. Examples: grep "a[x-z]c" file1 file2

Pattern
a.c
a[xyz]c or a[x-z]c a[^xyz]c

Matches
a[any single character]c, e.g. abc, a1c, a c.
axc, ayc, and azc only a[any single character but x, y, or z]c

ab*c
ab+c ^abc abc$

a[0 or more b]c, e.g. ac, abc, abbbc


a[1 or more b]c, e.g. abc, abbbc abc only at the beginning of a line abc only at the end of a line

a(bc|de)f myarray\[.+\]

abcf and adef myarray[anything that has 1 or more character]

33

The Shells
"the command line"

34

The Shells
Run applications and commands
Interactively Batch mode In parallel
*mircea* ~/eg$ \ echo -n The time is: ; date The time is:Thu Mar 29 14:45:09 CEST 2007
*mircea* ~/eg$ date; sleep 5 & Thu Mar 29 09:04:41 CEST 2007 [1] 22986 *mircea* ~/eg$ wait; date [1]+ Done sleep 5 Thu Mar 29 09:04:46 CEST 2007 *mircea* ~/eg$ sleep 5 & sleep 8 & [1] 21435 [2] 21436 *mircea* ~/eg$ jobs -l [1]- 21439 Running sleep [2]+ 21440 Running sleep *mircea* ~/eg$ jobs [1]- Running sleep [2]+ Running sleep

Interact with Files Manage environment used by applications

5 & 8 & 5 & 8 & 35

The Shells
Run applications and commands Interact with Files
Basic Fileglob Send files to commands. Send commands output to files.
*mircea* example *mircea* example *mircea* example2 *mircea* example *mircea* example2 ~/eg$ ls example2 newfile ~/eg$ ls exam* example2 ~/eg$ ls *2
~/eg$ ls [en]* example2 newfile ~/eg$ ls *[0-9]* test

Manage environment used by applications

*mircea* ~/eg$ \ head -1 /etc/exports >myexports *mircea* ~/eg$ cat myexports /export/mail 138.232.66.164/32 *mircea* ~/eg$ \ grep /export/mail </etc/exports /export/mail 138.232.66.164/32

36

The Shells
Run applications and commands Interact with Files Manage environment used by applications
Environment variables File creation mask
*mircea* ~/eg$ printenv|head REMOTEHOST=192.168.64.52 HOSTNAME=habicht BGCOLOR=black:dark PAGER_OPTIONS=-FeRJgX~ -SHELL=/usr/bin/bash TERM=screen HISTSIZE=500 OLDPWD=/home/mircea/eg/etc LSX=/bin/ls *mircea* ~/eg$ HISTSIZE=400 *mircea* ~/eg$ printenv|head REMOTEHOST=192.168.64.52 HOSTNAME=habicht BGCOLOR=black:dark PAGER_OPTIONS=-FeRJgX~ -SHELL=/usr/bin/bash TERM=screen HISTSIZE=400 OLDPWD=/home/mircea/eg/etc LSX=/bin/ls
37

The Shells
Run applications and commands Interact with Files Manage environment used by applications
Environment variables File creation mask
*mircea* ~/eg$ umask 0022 *mircea* ~/eg$ umask 0002; touch sample;ll -rw-rw-r-- 1 mircea dps samplemask *mircea* ~/eg$ umask 0002; touch sample;ll -rw-r--r-- 1 mircea dps samplemask

38

The Shells
Flavors
Bourne (sh)
Korn (ksh) Bourne-Again (bash) Almquist (ash) Z shell (zsh)
Left File Command Options Right ><^~^^^^^^^^^^^^^^^^^^^^^^^v>>><^~/eg^^^^^^^^^^^^^^^^^^^^^v>> | Name |Size | MTime || Name |Size | MTime | |/help | 4096|p 4 2006||/.. |--DIR| | |/hosts | 4096|t 14 2005||/test | 4096|ar 29 14:30| |~karl | 8|p 1 2006|| example | 0|ar 29 14:51| |/lib | 4096|c 28 15:11|| example2 | 0|ar 29 14:51| |/mail | 4096|v 22 2005|| newfile | 0|ar 29 14:51| |/nsmail | 4096|l 19 2006|| | | | |/private | 4096|r 17 13:38|| | | | |/profile | 4096|v 25 23:27|| | | | |/projects | 4096|r 14 22:04|| | | | |~publi~html| 20|v 22 14:41|| | | | |/roteg~blem| 4096|n 22 13:39|| | | | |/share | 4096|t 26 2005|| | | | |/software |20480|c 6 16:15|| | | | |/tmp |12288|r 29 12:47|| | | | |/toburn | 4096|c 11 2005|| | | | |/topics | 4096|v 7 2005|| | | | |/vmware | 4096|v 16 19:00|| | | | |/workspace | 4096|b 12 11:25|| | | | |@.DCOP~_:14| 55|b 4 00:09|| | | | |@.DCOP~_:19| 55|b 12 19:08|| | | | |@.DCOP~_:22| 55|c 20 22:47|| | | | |@.DCOP~_:25| 55|b 22 14:04|| | | | |@.DCOP~_:27| 55|n 21 14:36|| | | | | .DCOP~__14| 75|b 4 00:09|| | | | | .DCOP~__19| 75|b 12 19:08|| | | | | .DCOP~__22| 75|c 20 22:47|| | | | >^^^^^^^^^^^^^^^^^^^^^^^^^^^^>>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^> |/profile ||/.. | >^^^^^^^^^^^^^^^^^^^^^^^^^^^^>>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^> Hint: Tired of these messages? Turn them off from the Option [habicht:mircea] ~$ [^] 1Help 2Menu 3View 4Edit 5Copy 6RenMov 7Mkdir

C Shell (csh)
tcsh

Exotic
fish zoidberg

GUI
Konqueror

Text-GUI
Midnight Commander (mc)

39

The Shells
Mans Best Friend help (BASH only) man
$PAGER (more or less) man k topic
*mircea@$ help pwd pwd: pwd [-PL] Print the current working directory.
*mircea* ~/eg$ man -k sudo sudo (8) - execute a command as another user sudoers (5) - list of which users may execute what visudo (8) - edit the sudoers file

Sections man S n (Solaris) man n (GNU/BSD)


1. 2. 3. 4. 5. 6. 7. 8. commands system calls library calls drivers/structures config files games concepts, protocols system admin commands

*mircea* ~/eg$ man 6 sudo No entry for sudo in section 6 of the manual

info
40

The Shells
Mans Best Friend help (BASH only) man info
Hypertext document viewer Alternate manual page system developed for / with GNU Needed for make, awk, diff q h c-h TAB ENTR u quits tutorial keyboard help between topics-links to follow link to return UP from link
File: info.info, Node: Top, arted, Up: (dir)
Info: An Introduction ********************* The GNU Project distributes most of its on-lin\ e manuals in the "Info format", which you read using an "Info reader"\ . You are probably using an Info reader to read this now. If you are new to the Info reader and want \ to learn how to use it, type the command `h' now. It brings you to a \ programmed instruction --zz-Info: (info.info.gz)Top, 35 lines --Top--Basic Commands in Info Windows ****************************** C-x 0 C-x C-c h Quit this help. Quit Info altogether. Invoke the Info tutorial.

Next: Getting St\

Launch with --vi-keys for more/less interface q quits h keyboard help ESC-h tutorial c-x ENTR follow link c-x u return from link

Selecting other nodes: ---------------------n Move to the "next" node of this no\ de. p Move to the "previous" node of thi\ s node. u Move "up" from this node. m Pick menu item specified by name. -----Info: *Info Help*, 763 lines --Top-------No cross references in this node.

41

The Shells Using Aliases


What is it? Creating an Alias Automatic aliasing (bash)
alias
unalias

type / which
hash

*mircea* ~/eg$ alias alias ll='ls -l' alias vi='vim' *mircea* ~/eg$ alias ltr="ls -ltr" *mircea* ~/eg$ ltr total 4 drwx---r-x 2 mircea python 4096 Mar -rw-r--r-- 1 mircea dps 0 Mar -rw-r--r-- 1 mircea dps 0 Mar -rw-r--r-- 1 mircea dps 0 Mar

29 29 29 29

14:30 14:51 14:51 14:51

test newfile example example2

42

The Shells Using Aliases


What is it? Creating an Alias Automatic aliasing (bash)
type / which
hash
*mircea* ~/eg$ type kill kill is a shell builtin *mircea* ~/eg$ type -a kill kill is a shell builtin kill is /bin/kill kill is /usr/bin/kill *mircea* ~/eg$ type -ap kill /bin/kill /usr/bin/kill *mircea* ~/eg$ type head head is hashed (/usr/bin/head) *mircea* ~/eg$ type -a head head is /usr/bin/head *mircea* ~/eg$ hash hits command 5 /usr/bin/tail 1 /usr/bin/head 7 /bin/chmod 3 /bin/date *mircea* ~/eg$ hash r; hash hash: hash table empty *mircea* ~/eg$

43

Advanced Shell Programming


An in-depth exploration of the art of shell programming

44

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Manipulation File Specification Negative Logic Execution Control Functions
pwd
cd

set / echo
export

eval
fg / bg exec

pwd mircea@ pwd /home/mircea/example mircea@ cd /example cd .. // one step up

45

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Manipulation File Specification Negative Logic Execution Control Functions
pwd
cd

set / echo
export

eval
fg / bg exec

export var=Linux echo $var Linux a=$b;b=10; eval echo $a; test Set -- Displays or set the value of a shell variable. set PRINTER=HPLaserJet Echo $PRINTER HPLaserJet
46

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Manipulation File Specification Negative Logic Execution Control Functions
export
eval

fg / bg
exec

jobs
kill ls

mircea@$ nvu CTR+Z [1]+ Stopped nvu [mircea@$ bg 1 [1]+ nvu & mircea@$ jobs [1]+ Running nvu & mircea@$ fg 1 nvu find . -name "*.c -exec sh -c \'grep SMTH $1 | wc -l' {} {} \; kill PID
47

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection and Manipulation
Listing Files Viewing Files
exec
ls
list directory contents

cat
more / less head / tail grep rm /cp / mv

concatenate files and print on the standard output


view the content of a text file without starting an editor/ backwords Displays the first/last ten lines of a file Finds text within a file; -v exclude Remove copy move files

cat /etc/hosts
# Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost loopback

File Specification Negative Logic Execution Control Functions

grep localhost /etc/hosts


127.0.0.1 localhost.localdomain localhost loopback

48

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Manipulation
standard streams Copying, moving, renaming, new dir
head / tail
grep

rm /cp /mv
mkdir / rmdir

chmod / chown
id / groups newgrp

rm example
rm: cannot remove `example/': Is a directory

rm r example

File Specification Negative Logic Execution Control Functions

mv ./example ./test cp r /example /tmp

49

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Manipulation
Changing owner Changing permissions
mkdir / rmdir
chmod / chown

Removes empty directories

id / groups
newgrp
rmdir examples rm: cannot remove `example/': Is a directory mircea@ ll -rw-r--r-- 1 mircea dps 0 Nov 9 16:05 samplemask drwxr-xr-x 2 mircea dps 6 Nov 9 17:16 test [root@]# chown root:root test mircea@ll -rw-r--r-- 1 mircea dps 0 Nov 9 16:05 samplemask -drwxr-xr-x 2 root root 4096 Nov 9 17:16 test
id uid=233(mircea) gid=111(dps) groups=111(dps), 122(python) newgrp python uid=233(mircea) gid=111(python) groups=111(dps), 122(python)

File Specification Negative Logic Execution Control Functions

50

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification
find and xargs
id / groups
newgrp

umask
find

xargs
fg / bg exec

find /home/mircea/tmp -type f -print0 | xargs -0 grep ProxyPass

Negative Logic Execution Control Functions

51

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control Functions
eval
test

true / false
if .. fi

case W in .. esac
for VAR in .. do .. done
test d sample; echo$?; --> 0 [ian@pinguino ~]$ test "abc" = "def" ;echo $? 1 [ian@pinguino ~]$ [ "abc" != "def" ];echo $? 0 [ian@pinguino ~]$ [ "abc" \< "def" ];echo $? 0 [ian@pinguino ~]$ [ "abc" \> "def" ];echo $? 1 [ian@pinguino ~]$ [ "abc" \<"abc" ];echo $? 1 a=$b;b=10; eval echo $a; -- > 10 test 3 -gt 4 && echo True || echo false false

52

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control
if/elif/else/fi
true / false
if .. fi

case W in .. esac
for VAR in .. do .. done while.. do.. done

let a=10; if [ $a = 10 ]; then { echo true; } else { echo false; } fi;

Functions

let a=1;[ $a != 10 ] && echo true|| echo false;

53

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control
case WORD in ... esac
true / false
if .. fi

case W in .. esac
for VAR in .. do .. done while.. do.. done
echo Hit a key, then hit return; read Keypress ; case "$Keypress" in [[:lower:]] ) echo "Lowercase letter";; [[:upper:]] ) echo "Uppercase letter";; [0-9] ) echo "Digit";; * ) echo "Punctuation, whitespace, or other";; esac

Functions

54

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control
for VAR in ... done
true / false
if .. fi

case W in .. esac
for VAR in .. do .. done while.. do.. done

> > # >

for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet Each planet on a separate line. done
for i in `seq 1 10` ; do echo $i;done

Functions

55

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control
while ... do ... done
true / false
if .. fi

case W in .. esac
for VAR in .. do .. done while.. do.. done
while [ "$var1" != "end" ] # while test "$var1" != "end" do echo "Input variable #1 (end to exit) " read var1 # Not 'read $var1 echo "variable #1 = $var1" # Need quotes because of "#" . . . # If input is 'end', echoes it here. # Does not test for termination condition until top of loop. echo done

Functions

56

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control
break, continue, exit ||, && type, which, whence
while.. do.. done
break continue exit

|| and &&
LIMIT=19 # Upper limit echo echo "Printing Numbers 1 through 20 (but not 3 and 11). a=0 while [ $a -le "$LIMIT" ] do a=$(($a+1)) if [ "$a" -eq 3 ] || [ "$a" -eq 11 ] # Excludes 3 and 11. then continue # Skip rest of this particular loop iteration. fi echo -n "$a " # This will not execute for 3 and 11. done # Exercise: # Why does loop print up to 20? if [ "$a" -gt 8 ] then break # Skip entire rest of loop. exit # exit the shell / script fi

Functions

57

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control
break, next, exit ||, && type, which, whence
break/continue
next

exit
|| and &&
and (logical)

if [ $condition1 ] && [ $condition2 ] # Same as: if [ $condition1 -a $condition2 ] # Returns true if both condition1 and condition2 hold true... or (logical)

if [ $condition1 ] || [ $condition2 ] # Same as: if [ $condition1 -o $condition2 ] # Returns true if either condition1 or condition2 holds true... # Note that || operator not permitted within [ ... ] construct.

a=24 b=47 if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]

Functions

then echo "Test #1 succeeds." else echo "Test #1 fails." fi if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]

58

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control
break, next, exit ||, && type, which, whence
type
which

whence
[mircea@ ~]$ type -a ls ls is aliased to `ls --color=tty' ls is /bin/ls

[mircea@ ~]$ type -a ll ll is aliased to `ls -l --color=tty


[mircea@ ~]$ which ls alias ls='ls --color=tty' /bin/ls

Functions
59

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control Functions
Declaring, returning
return
declare

command

[mircea@ ~]$ type fun fun is a function fun () { echo "This is a function"; return 44; } fun;echo $? This is a function 44 exit() { true; }

60

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control Functions
Using Overriding builtins & commands
61

declare
command

builtin

fun () { echo "$1"; echo; } for i in `seq 1 3` ; > do fun $i; done; 1

2
3

Advanced Shell Programming with BASH


Working Directory Variable Creation & Expansion Job Control File Redirection File Specification Negative Logic Execution Control Functions
Using Overriding builtins & commands
type
command

builtin

# Something like this might be useful in some contexts. NO_EXIT=1 # Will enable function definition below. [[ $NO_EXIT -eq 1 ]] && exit() { true; } # Function definition in an "andlist". # If $NO_EXIT is 1, declares "exit()". # This disables the "exit" builtin by aliasing it to "true". exit # Invokes "exit ()" function, not "exit" builtin.

use UNSET to delete the function


62

Questions ?

THANK YOU and GOOD LUCK!,


Mircea Munteanu DPS System Administrator www.dps.uibk.ac.at mircea@dps.uibk.ac.at

63

Das könnte Ihnen auch gefallen