Beruflich Dokumente
Kultur Dokumente
Munteanu, Mircea
based on slides by Shelling, Otheus
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
Currently there are different versions of UNIX such as SunOS, Linux, DEC OSF/1, AIX, HP-UX 10, Solaris, IRIX, SCO UNIX,etc.
Distributions A particular Flavor will be packaged and marketed through various Distributions
FreeBSD, RedHat, Gentoo
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
Landmark releases:
System V
BSD 4.3
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)
Kernel manages core resources Processes Memory (physical and virtual) Interprocess communication (IPC) presents to users: system calls device files
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
10
RPC Signals
11
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
RPC Signals
13
*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
14
*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$
15
UNIX Filesystem
Tree Structure Promiscuous Permissions
16
brw------crw-r--r-prw-r--r-srw-r--r-mrw-r--r-lrwxrwxrwx
1 1 1 1 1 1
Promiscuous Permissions
block special (1/0) character special (2/1) directory ascii text fifo Xenix semaphore Xenix shared memory handle ascii text
17
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
Breaking structure
Soft-links Loopback mounts Bind mounts
Promiscuous Permissions
19
Promiscuous Permissions
20
Promiscuous Permissions
21
Promiscuous Permissions
22
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
Permissions
24
Permissions
25
Permissions
Permissions
27
Group name
Owner name
4 bits:
Read Write Execute Special
others permissions
Octal notation
28
w
x
2
1
754 = "-rwxr-xr--" = rwx for user, r-x for group, r-- for other
4 bits:
Read Write Execute Special
Char
Octal
Permission to...
read contents (ls) create new entires rename entries remove entries
r w
4 2
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.
30
* 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
31
Regular Expressions
One of computings most powerful information retrieval tools
32
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(bc|de)f myarray\[.+\]
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
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
*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
*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.
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
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
42
43
44
set / echo
export
eval
fg / bg exec
45
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
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
cat
more / less head / tail grep rm /cp / mv
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
48
rm /cp /mv
mkdir / rmdir
chmod / chown
id / groups newgrp
rm example
rm: cannot remove `example/': Is a directory
rm r example
49
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)
50
umask
find
xargs
fg / bg exec
51
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
case W in .. esac
for VAR in .. do .. done while.. do.. done
Functions
53
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
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
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
|| 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
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.
Functions
then echo "Test #1 succeeds." else echo "Test #1 fails." fi if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]
58
whence
[mircea@ ~]$ type -a ls ls is aliased to `ls --color=tty' ls is /bin/ls
Functions
59
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
declare
command
builtin
fun () { echo "$1"; echo; } for i in `seq 1 3` ; > do fun $i; done; 1
2
3
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.
Questions ?
63