Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX...

63
Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus For University of Innsbruck, November 2007 1

Transcript of Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX...

Page 1: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced UNIX Tutorial

Munteanu, Mirceabased on slides by Shelling, Otheus

For University of Innsbruck, November 2007

1

Page 2: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Topics Covered• Why Unix?

• Brief History

• Architecture

• Inter-process

Communication

• Filesystem

• Regular Expressions

• Program Execution

• PATH to enlightenment

• The Shells

• BASH Programming

• Shell Startup

• Short-cuts

• Cool Commands

root:/ $

2

Page 3: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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 don‟t require fine-grain control

• Currently there are

different versions of UNIX

such as SunOS, Linux, DEC

OSF/1, AIX, HP-UX 10,

Solaris, IRIX, SCO UNIX,etc.

3

Page 4: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Flavors, Versions, DistributionsFlavors

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

Page 5: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX HistoryBegan in 1969 as ―Unics‖ at Bell

Labs (AT&T) by

• Ken Thompson

• Dennis Ritchie (C)

Other pioneers:

• Brian Kernighan (AWK, m4)

• Douglas McIlroy (pipes)

• Bill Joy (vi, csh, NFS)

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

Page 6: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX HistoryX11 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

6

Page 7: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Architecture – System LevelDevices -

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

7

Page 8: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Architecture – User LevelUser:

interacts with an application through a device

Application:commands

ls, qsub,

run-time environment

bash, perl, python, Java,

PHP, mozilla

subsystem

X Windows, SGE

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.

[prompt]$ <command> <flags> <args>

fiji:~$ ls –l -a unix-tutorial

8

Page 9: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Inter-Process Communication

How process communicate

9

Page 10: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

Page 11: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Inter-Process communication (IPC)• Semaphores

• Shared Memory

• Filesystem

• Pipes

– Unnamed

– Named / FIFOs

– Network Sockets

• 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

[mircea@example]$cat files |grep sample

sample

11

Page 12: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Inter-Process communication (IPC)• Semaphores

• Shared Memory

• Filesystem

• Pipes

– Unnamed

– Named / FIFOs

– Network Sockets

• RPC

• Signals

*mircea* ~/eg$ mkfifo newfifo*mircea* ~/eg$ *mircea* ~/eg$ ls -l newfifoprw-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 9 11:36:25 CET 200729556Fri Nov 9 11:36:25 CET 2007

*mircea* ~/eg$ date ; cat newfifo ; date

Fri Nov 9 11:36:49 CET 200720321Fri Nov 9 11:36:49 CET 2007

12

Page 13: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Inter-Process communication (IPC)• Semaphores

• Shared Memory

• Filesystem

• Pipes

– Unnamed

– Named / FIFOs

– Network Sockets2-way pipes over a network

• RPC

• Signals

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

13

Page 14: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Inter-Process communication (IPC)• Semaphores

• Shared Memory

• Filesystem

• Pipes

• RPC

– RPC process

– Portmapper

process

– YP/NIS & NFS

• Signals

*mircea* ~/eg$ ps -e -o uid,pid,cmd |> grep rpc

29 3338 rpc.statd0 3599 rpc.idmapd0 4093 rpc.yppasswdd -e chsh -e chfn0 4274 rpc.rquotad0 4312 [rpciod]0 4323 rpc.mountd

201 17112 grep rpc

*mircea* ~/eg$ rpcinfo -p | uniq -f 4

program vers proto port100000 2 tcp 111 portmapper100024 1 udp 32768 status100007 2 udp 856 ypbind100004 2 udp 845 ypserv100009 1 udp 878 yppasswdd100011 1 udp 640 rquotad100003 2 udp 2049 nfs100021 1 udp 32774 nlockmgr100005 1 udp 686 mountd

14

Page 15: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Inter-Process communication (IPC)• Semaphores

• Shared

Memory

• Filesystem

• Pipes

• RPC

• Signals– Only within a

process “family”

– Processes “0”, “-1”

– SIGHUP

– SIGTERM

– SIGKILL

– SIGSEGV

*mircea* ~/eg$ kill -l | head -8

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL

5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE

9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2

13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD

18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN

22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO

30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMI

*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$

15

Page 16: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem

Tree Structure

Promiscuous

Permissions

16

Page 17: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Tree Structure• Tree Structure

– File-types

• files

• directories

• soft-links

• fifos

• network

sockets

• character

devices

• block devices

– Mounting drives

– Breaking

structure

• Promiscuous

• Permissions

drwxr-xr-x 2 root root 4096 Aug 30 2006 /bin

lrwxrwxrwx 1 root root 4 Mar 26 2006 /bin/csh

srw-rw-rw- 1 root root 0 Oct 28 12:51 /dev/log

brw-rw---- 1 root disk 8, 0 Oct 28 12:50 /dev/sda

crw-rw-rw- 1 root root 5, 0 Oct 28 21:40 /dev/tty

-rw-r--r-- 2 root root 1132 Mar 20 16:40 /etc/hosts

brw------- 1 root sys bdevfile

crw-r--r-- 1 root sys cdevfile

prw-r--r-- 1 root sys namedpipe

srw-r--r-- 1 root sys semaphore

mrw-r--r-- 1 root sys sharedmem

lrwxrwxrwx 1 root sys symlink -> /etc/passwd

bdevfile: block special (1/0)

cdevfile: character special (2/1)

dir: directory

disks.html: ascii text

namedpipe: fifo

semaphore: Xenix semaphore

sharedmem: Xenix shared memory handle

symlink: ascii text

17

Page 18: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

Page 19: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Tree Structure• Tree Structure

– File-types

– Mounting drives

Reuqires “root”

– Breaking

structure

• Soft-links

• Loopback mounts

• ―Bind‖ mounts

• Promiscuous

• Permissions

*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>

19

Page 20: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Tree Structure• Tree Structure

– File-types

– Mounting drives

– Breaking

structure

• Soft-links

• Loopback mounts

• ―Bind‖ mounts

• Promiscuous

• Permissions

*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$

20

Page 21: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Tree Structure• Tree Structure

– File-types

– Mounting drives

– Breaking

structure

• Soft-links

• Loopback mounts

• ―Bind‖ mounts

• Promiscuous

• Permissions

*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$

21

Page 22: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Tree Structure• Tree Structure

– File-types

– Mounting drives

– Breaking

structure

• Soft-links

• Loopback mounts

• “Bind” mounts

--bind (Linux)

-F bindfs

(Solaris)

• Promiscuous

• Permissions

*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

22

Page 23: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

• Promiscuous

• Permissions

• 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

23

Page 24: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

Page 25: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Very Promiscuous• Resembles a tree

• Promiscuous

– Devices

– Processes

– Kernel stats and

configuration

• Permissions

MAKEDEV loop5 ram14 skip tty2XOR loop6 ram15 stderr tty3arpd loop7 ram2 stdin tty4cdrom lp0 ram3 stdout tty5cdrom1 lp1 ram4 systty tty6console lp2 ram5 tap0 tty7core lp3 ram6 tap1 tty8cpu mapper ram7 tap10 tty9cpu0 mcelog ram8 tap11 urandomcpu1 md0 ram9 tap12 usersockcpu2 mem ramdisk tap13 vcscpu3 mice random tap14 vcs1dnrtmsg mosal rawctl tap15 vcs2dvd net root tap2 vcs3fd nflog route tap3 vcs4floppy null route6 tap4 vcs5full parport0 rtc tap5 vcs6fwmonitor parport1 scd0 tap6 vcsahda parport2 sda tap7 vcsa1hw_random parport3 sdb tap8 vcsa2initctl port sdc tap9 vcsa3input ppp sdd tcpdiag vcsa4ip6_fw ptmx sde ts_ua0 vcsa5js0 pts sde1 ts_ua1 vcsa6kmsg ram sde2 ts_ua2 vipkllog ram0 sde3 ts_ua3 xfrmloop0 ram1 sdf ts_ua4 zeroloop1 ram10 sdf1 ts_ua5loop2 ram11 sdf2 ts_ua6loop3 ram12 sdf3 tty0loop4 ram13 shm tty1

25

Page 26: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Very Promiscuous• Resembles a tree

• Promiscuous

– Devices

– Processes

– Kernel stats and

configuration

• Permissions

*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

Page 27: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Very Promiscuous• Resembles a tree

• Promiscuous

– Devices

– Processes

– Kernel stats and

configuration

• Permissions

*mircea* /proc$ ls -CdF [a-z]*acpi/ iomem net/buddyinfo ioports partitionsbus/ irq/ pcicmdline kallsyms scsi/cpuinfo kcore self@crypto key-users slabinfodevices keys statdiskstats kmsg swapsdma loadavg sys/driver/ locks sysrq-triggerexecdomains mdstat sysvipc/fb meminfo topspin@filesystems misc tty/fs/ modules uptimeide/ mounts@ versioninfiniband/ mpt/ vmstatinterrupts mtrr*mircea* /proc$ cat uptime704398.43 593929.58*mircea* /proc$ uptime20:56:58 up 8 days, 3:39, 7 users,

load average: 1.72, 1.75, 1.67*mircea* /proc$

27

Page 28: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Security• Tree Structure

• Promiscuous

• Permissions

– 3 Classes:

• User

• Group

• Other (World)

– 4 bits:

• Read

• Write

• Execute

• Special

*mircea* ~/eg$ ls -l pubfile

-rwxrwxrwx 1 mircea dps 0 Oct 29 08:55 pubfile

User/owner‟s permissions

group‟s permissions

other‟s permissions

Owner name

Group name

•1 --x execute

•2 -w- write

•3 -wx write and execute

•4 r– read Octal notation•5 r-x read and execute

•6 rw- read and write

•7 rwx read, write and execute

28

Page 29: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Security• Resembles a tree

• Promiscuous

• Permissions

– 3 Classes:

• User

• Group

• Other (World)

– 4 bits:

• Read

• Write

• Execute

• Special

File Permission Bits

Char Octal Permission to...

r 4 read file

w 2 write / overwrite file

x 1 execute file

Directory Permission Bits

Char Octal Permission to...

r 4 •read contents (ls)

w 2 •create new entires

•rename entries

•remove entries

x 1 • ―cd‖ into

• operate on files in

this directory

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

29

Page 30: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

Page 31: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

UNIX Filesystem – Security• Resembles a tree

• Promiscuous

• Permissions

– 4 bits:

• Read

• Write

• Execute

• Special

– Directories

– Set-uid

– Set-gid

* “Set User ID”and"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

Page 32: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Regular Expressions

One of computing’s most

powerful information

retrieval tools

32

Page 33: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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 Matches

a.c a[any single character]c, e.g. abc, a1c, a c.

a[xyz]c or a[x-z]c axc, ayc, and azc only

a[^xyz]c a[any single character but x, y, or z]c

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

ab+c a[1 or more b]c, e.g. abc, abbbc

^abc abc only at the beginning of a line

abc$ abc only at the end of a line

a(bc|de)f abcf and adef

myarray\[.+\] myarray[anything that has 1 or more character]

33

Page 34: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The Shells

"the command line"

34

Page 35: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The Shells• Run applications

and ―commands‖

– Interactively

– “Batch mode”

– In parallel

• Interact with

Files

• Manage

environment used

by applications

*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 5 &

[2]+ 21440 Running sleep 8 &

*mircea* ~/eg$ jobs

[1]- Running sleep 5 &

[2]+ Running sleep 8 &

35

Page 36: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The Shells• Run applications

and ―commands‖

• Interact with

Files

– Basic “Fileglob”

– “Send” files to

commands.

– “Send” command’s

output to files.

• Manage

environment used

by applications

*mircea* ~/eg$ ls

example example2 newfile test

*mircea* ~/eg$ ls exam*

example example2

*mircea* ~/eg$ ls *2

example2

*mircea* ~/eg$ ls [en]*

example example2 newfile

*mircea* ~/eg$ ls *[0-9]*

example2

*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

Page 37: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

Page 38: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

Page 39: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The Shells• Flavors

– Bourne (sh)

• Korn (ksh)

• Bourne-Again

(bash)

• Almquist (ash)

• ―Z‖ shell (zsh)

– ―C‖ Shell (csh)

• tcsh

– ―Exotic‖

• fish

• zoidberg

– GUI

• Konqueror

– Text-GUI

• Midnight

Commander (mc)

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

39

Page 40: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The ShellsMan’s Best Friend

• help (BASH only)

• man

$PAGER

(more or less)

man –k topic

Sections

man –S n (Solaris)

man n (GNU/BSD)

1. commands

2. system calls

3. library calls

4. drivers/structures

5. config files

6. games

7. concepts, protocols

8. system admin commands

• info

*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

*mircea* ~/eg$ man 6 sudo

No entry for sudo in section 6 of the manual

40

Page 41: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The ShellsMan’s Best Friend

• help (BASH only)

• man

• infoHypertext document viewer

Alternate manual page system developed for / with GNU

Needed for make, awk, diff

q quits

h tutorial

c-h keyboard help

TAB between topics-links

ENTR to follow link

u to return UP from link

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

File: info.info, Node: Top, Next: Getting St\arted, Up: (dir)

Info: An Introduction*********************

The GNU Project distributes most of its on-lin\e manuals in the "Infoformat", which you read using an "Info reader"\. You are probably usingan 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 Quit this help.C-x C-c Quit Info altogether.h Invoke the Info tutorial.

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

Page 42: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The Shells – Using Aliases• What is it?

• Creating an Alias

• Automatic

aliasing (bash)

*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 29 14:30 test

-rw-r--r-- 1 mircea dps 0 Mar 29 14:51 newfile

-rw-r--r-- 1 mircea dps 0 Mar 29 14:51 example

-rw-r--r-- 1 mircea dps 0 Mar 29 14:51 example2

alias

unalias

type / which

hash

42

Page 43: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

The Shells – Using Aliases• What is it?

• Creating an Alias

• Automatic

aliasing (bash)

*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$

type / which

hash

43

Page 44: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming

An in-depth exploration of

the art of shell programming

44

Page 45: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Manipulation

• File

Specification

• Negative Logic

• Execution Control

• Functions

• pwd

• mircea@ pwd

/home/mircea/example

• mircea@ cd /example

• cd .. // one step up

pwd

cd

set / echo

export

eval

fg / bg

exec

45

Page 46: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Manipulation

• File

Specification

• Negative Logic

• Execution Control

• Functions

• 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

pwd

cd

set / echo

export

eval

fg / bg

exec

46

Page 47: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Manipulation

• File

Specification

• Negative Logic

• Execution Control

• Functions

• 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

export

eval

fg / bg

exec

jobs

kill

ls

47

Page 48: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

and Manipulation

– Listing Files

– Viewing Files

• File

Specification

• Negative Logic

• Execution Control

• Functions

• 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

• grep localhost /etc/hosts

127.0.0.1 localhost.localdomain localhost loopback

exec

ls list directory contents

cat concatenate files and print on the

standard output

more / less view the content of a text file

without starting an editor/ backwords

head / tail Displays the first/last ten lines of a

file

grep Finds text within a file; -v exclude

rm /cp / mv Remove copy move files

48

Page 49: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Manipulation

– standard streams

– Copying, moving,

renaming, new dir

• File

Specification

• Negative Logic

• Execution Control

• Functions

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

• rm –r example

• mv ./example ./test

• cp –r /example /tmp

head / tail

grep

rm /cp /mv

mkdir / rmdir

chmod / chown

id / groups

newgrp

49

Page 50: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Manipulation

– Changing owner

– Changing

permissions

• File

Specification

• Negative Logic

• Execution Control

• Functions

• 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)

mkdir / rmdir Removes empty directories

chmod / chown

id / groups

newgrp

50

Page 51: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

• File

Specification

– find and xargs

• Negative Logic

• Execution Control

• Functions

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

id / groups

newgrp

umask

find

xargs

fg / bg

exec

51

Page 52: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

• File

Specification

• Negative Logic

• Execution Control

• Functions

• 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

eval

test

true / false

if .. fi

case W in .. esac

for VAR in .. do ..

done

52

Page 53: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

• File

Specification

• Negative Logic

• Execution Control

– if/elif/else/fi

• Functions

• let a=10;

if [ $a = 10 ];

then { echo “true”; }

else { echo “false”; }

fi;

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

true / false

if .. fi

case W in ..

esac

for VAR in ..

do .. done

while.. do..

done

53

Page 54: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

• File

Specification

• Negative Logic

• Execution Control

– case WORD in ...

esac

• Functions

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

true / false

if .. fi

case W in ..

esac

for VAR in ..

do .. done

while.. do..

done

54

Page 55: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

• File

Specification

• Negative Logic

• Execution Control

– for VAR in ...

done

• Functions

• 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

true / false

if .. fi

case W in ..

esac

for VAR in ..

do .. done

while.. do..

done

55

Page 56: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

• File

Specification

• Negative Logic

• Execution Control

– while ... do ...

done

• Functions

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

true / false

if .. fi

case W in ..

esac

for VAR in ..

do .. done

while.. do..

done

56

Page 57: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

• Functions

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

while.. do..

done

break

continue

exit

|| and &&

57

Page 58: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

• Functions

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 ]

then

echo "Test #1 succeeds."

else

echo "Test #1 fails."

fi

• if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]

58

Page 59: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

• Functions

• [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

type

which

whence

59

Page 60: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Advanced Shell Programming with BASH• Working Directory

• Variable Creation

& Expansion

• Job Control

• File Redirection

• File

Specification

• Negative Logic

• Execution Control

• Functions

– Declaring,

returning

• [mircea@ ~]$ type fun

fun is a function

fun ()

{

echo "This is a function";

return 44;

}

• fun;echo $?

This is a function

44

• exit() { true; }

return

declare

command

60

Page 61: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

• fun () { echo "$1"; echo; }

• for i in `seq 1 3` ;

> do fun $i; done;

1

2

3

declare

command

builtin

61

Page 62: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

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

# 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 "and-list".

# 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

type

command

builtin

62

Page 63: Advanced UNIX Tutorial - uibk.ac.attf/lehre/ws07/bs/vorlesungen/UnixTutorial.pdf · Advanced UNIX Tutorial Munteanu, Mircea based on slides by Shelling, Otheus ... • X11R6, Linux

Questions ?

THANK YOU and GOOD LUCK!,Mircea Munteanu

DPS System Administrator

www.dps.uibk.ac.at

[email protected]

63