Popular in Course
Popular in ComputerScienence
This 197 page Class Notes was uploaded by Mallie Crist on Monday September 21, 2015. The Class Notes belongs to CSC 3320 at Georgia State University taught by Staff in Fall. Since its upload, it has received 32 views. For similar materials see /class/209896/csc-3320-georgia-state-university in ComputerScienence at Georgia State University.
Reviews for SYSTEM
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/21/15
Chapter 4 The UNIX Shells Bourne shell Korn shell C shell Graham Glass and King Ables UNIX for Programmers and Users Third Edition Pearson Prentice Hall 2003 Original Notes by Raj Sunderraman Converted to presentation and updated by Michael Weeks No programming language is perfect There is not even a single best lanu uau e there are onl languages well suited or perhaps poorly suited for particular purposes Herbert Mayer Which Shell To change your default shell use the chsh utility Directly type command sh csh ksh bash to change to another shell To examine your default shell type echo SHELL echo SHELL binbash Buildin Command Zommands substitutioi Scripts Variables Redirection Wildcards Pipes Sequences Subshells BaCkgrfmnd process1ng Local Environment Conditional Unconditional CORE Shell Functionality Builtin commands Scripts Variables local environment Redirection Wildcards CORE Shell Functionality Pipes Sequences conditional unconditional Subshells Background processing Command substitution Invoking the Shell A shell is invoked either automatically upon logii u manually from the keyboard or script What does the shell do The following takes place 1 reads a special startup file cshrc for csh in the user39s home directory and executes all the commands in that file bashrc 2 dis la 3 a rom t and waits for a user command 3 If user enters CTRLD end of input the shell terminates otnenNIse It executes tne user commands User Commands Is list files ps process info continues line Ip send to printer 15 ps ef sort 1p 1ssort 1p Builtin commands Most Unix commands invoke utility programs stored in the file hierarch ex ls vi etc The shell has to locate the utility using PATH variable Shells have builtin commands ex echo cd Builtin commands echo arguments echo Hi How are you Hi How are you echo by default appends a new line to inhibit new line use n option in csn cd dir Metacharacters Output redirection gt writes standard output to file gtgt appends standard output to file Input redirection lt reads std input from file ltlttok read std input unti tok Metacharacters Filesubstitution wildcards matches 0 or more characters matches any single character matches any character within brackets Command substitution command replaced by the output of command eg echo ls Metacharacters Pipe send output of one proceSs to the input of another eg list files then use wordcount to count lines Is wc this effectively counts the files Metacharacters Used to sequence commands Conditional execution execute command if previous one fails ampamp execute command if previous one succeeds Metacharacters Group commands amp Run command in background Comment rest of characters ignored by shell Expand the value of a variable Prevent special interpretation of character that follows Redirection The shell redirection facility allows you to store the output of a process to a file use the contents of a file as input to a process Examples cat x1 c gt yc cat x2c gtgt yc mail tony lt hiMom The ltlttok redirection is almost exclusively used in shell scripts will see this later Filename substitution Is 0 list c files Is 0 list files like ac bc 1c etc ls ac list files starting with a or c Is AZaz list files beginning with a letter ls dirC list all c files in directories starting with dir Pipes command1 command2 command3 13 ppp00 ppp24 ppp48 ppp72 lslwcw 4 Pipes cat etcpasswd awk F 39print 139 sort adm bin daemon raj tee utility tee ia filename causes standard Input to be copied to file and also sent to standard output a option appends to file i option ignores interrupts tee utility WhO gt Wh0capture sort WhO tee Wh0capture sort more Wh0capture Command Substitution A command surrounded by grave accents is executed and its standard output is inserted in the commands place in the command line echo today is date today is Sat Jun 19 222328 EDT 2007 echo there are Who l we 1 users on the system there are 2 users on the system Sequences Commands or pipelines separated by semi colons Each command in a sequence may be individually lO redirected Example date pwd ls date gt datetxt pwd gt pwdtxt ls Sequences date pwd ls Sat Jun 19 223319 EDT 2007 h0meraj0racle jdbc ows proc sql squ Wh0capture date gt datetxt pwd gt pwdlxt ls datetxt jdbc ows proc pwdtxt sql squ Wh0capture Sequences Conditional sequences cc m rovc ampamp aout cc myprogc echo compilation failed In a series of commands separated by ampamp the next command is executed if the previous one succeeds returns an exit code of O In a series of commands separated by H the next command is executed if the previous one fails returns an exit code of nonzero Grouping commands Commands can be grouped by putting them within parentheses a sub shell is created to execute the grouped commands Example date ls g wd gt outtxt more outtxt Grouping commands date is pwd gt outtxt 39 more outtxt Sat Jun 19 224043 EDT 2007 datetxt jdbc outtxt ows proc pwdtxt sql sq Wh0capture h0meraj0racle Background processing An amp sign at end of a simple command or pipeline sequence of pipelines or a group of commands Starts a subshell commands are executed as a background process does not take control of the keyboard A process id is displayed when it begins Background processing Redirect the output to a file if desired prevents background output on terminal Background process cannot read from standard input Ifthey attempt to read from standard input they terminate Shell ProgramsScripts Shell commands may be stored in a text file for execu on Use the chmod utility to set execute permissions on the file Executing it by simply typing the file name When a script runs the syste aetermines which shell to use Shell ProgramsScripts To determine which shell if the first line of the script is a pound sign then the script is interpreted by the current shell if the first line of the script is on me noun lbinsh or lbinksh etc then the appropriate shell is used to interpret the script else the scri t is inter reted by the Bourne shell Note pound sign on 1st column in any other line implies a comment line Shell ProgramsScripts Always recommended to use lpathname bincsh A simple Cshell script echo n quotThe date today is quot date Subshells Several ways a subshell can be created Grouped command ls pwd date Script execution Background processes A subshell has its own working directory cd commands in subshell do not change working directory of parent shell pathnamescriptsh Subshells Every shell has two data areas environment space localvariable space Child shell gets a copy of the parent39s environment space starts with an empty localvariable space Variables A shell supports two kinds of variables Local variables Environment variables Both hold data in string format Every shell has a set of predefined environment variables and local variables Accessmg variables In all shells IS done by pretlxmg the name with a sign Variables Some predefined environment variables available in all shells HOME PATH MAIL USER SHELL TERM Assigning values to variables Depends on shell sh bash ksh variablevalue variablequotvaluequot Notice no spaces around equal sign To make a variable an environment variable in shbashksh export variable Assigning values to variables csh set variablevalue set variablequotvaluequot To assign environment variables setenv TERM vt1OO Builtin Variables Common builtin variables with special meaning process ID of shell 0 name of shell script if applicable 1 9 n refers to the nth command line argument if applicable a list of all command line arguments Example using Builtin variables cat script2csh bincsh echo the name of this tile is 0 echo the rst argument is 1 echo the list of all arguments is echo this script places the date into a temporary file called 1 dategt 1 ls l 1 rm 1 Running the EAaIIIpIc script2csh paul ringo george john the name of this file is script2csh the first argument is paul the list of all arguments is paul ringo george john this script places the date into a temporary file called paul554 nNnNr 1 raj raj 29 Jun 20 2133 paun554 Quoting Single quotes 39 inhibit wildcard replacement variable substitution and command substitution Double quotes quot inhibits wildcard replacement only When quotes are nested only the outer quotes have any effect Quoting Examples echo 3 4 12 3og 3tex scriptcsh script2csh 4 12 echo 3934 1239 412 echo quotmy name is USER the date is datequot39 1y name is raj the date is Sun Jun 20 215913 EDT 2007 Job Control ps command generates a list of processes and their attributes kill command terminates processes based on process ID wait allows the shell to wait for one of its child processes to terminate ps Command ps efl e include all running processes f include full listing I include long listing PID process ID nohup Command Bourne and Ksh automatically terminate background I rocesses when ou log out csh allows them to continue To keep the background processes to continue in sh and ksh use nohup command Signaling processes kill kill 1 1 SIGHUP 2 SIGINT 3 SIGQUIT 4 SIGILL 5 SIGTRAP 6 SIGIOT 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 kill kill signa pid if signal is not specified the default signal is SIGTERM 15 SIGKILL 9 is useful if the process refuses to die If a workstation quits responding try logging in from a different workstation and signaling the nonresponsive process Waiting for child processes sleep 30 echo done 1 amp 1 429 sleep 30 echo done a 2 431 echo done 3 wait echo done 4 cone 3 clone 1 1 Done sleep 30 echo done 1 done 2 2 Done sleep 30 echo done 2 done 4 This feature is used in advanced shell scripts Finding a command PATH If the command is a shell builti such as echo or cd it is directly interpreted by the shell if the command begins with a shell assumes that the command is the absolute path name of an executable error occurs if the executable is not found if not builtin and not a full pathname shell searches the directories in the PATH from left to right for the executable current workino directorv mav not be in PATH PATH variable If PATH is empty or is not set only the current working director is searched for the executable Homebrewed utilities some Unix users create their own utilities Stored in their bin directory Place their bin directory ahead of all others Their version of the utility is executed Termination and Exit codes Every Unix process terminates with an exit value By convention a 0 value means success and a nonzero value means failure All builtin commands return 1 when they fail Termination and Exit codes The special variable 55 contains the exit code of the last command execution In csh status also contains the exit code Any script written by you should contain the exit command exit ltnumbergt If the script does not exit with a exit code the exit code of the last command is returned by default Common Core Builtin commands eval command I He even ol ICII CUIIIIIIaI Id executes the OUtpUt Of the command as a regular shell command eval echo x5 echo x 5 Common Core Builtin commands exec command The exec shell command causes the shell39s image to be replaced with the command in the process39 memory space As a result if the command terminates the shell also ceases to exist If the shell was a login shell the login session terminates Common Core Builtin commands shift This command causes all of the positional parameters 2n to be renamed 1 n1 and 1 is lost Useful in rocessin command line parameters Common Core Builtin commands cat script3csh lbincsh echo first argument is 1 all args are shift echo first argument is 1 all args are scri t3csh a b c d first argument is a all args are a b c d first argument is b all args are b c d Review Covered core shell functionality Builtin commands Scripts Variables Redirection Wildcards Pipes Subshells Background processing Practice problems of C programing 1 What is the output of the following program int abc alO b50 030 ifagtbabbcca printf ad bd cd n abc A alOb50 010 B alOb50 030 C alO b30 010 D a50 b30 050 2 what is the output of the following program include ltstdiohgt main int 1 fori3igtli f0rjl jlt2j printfquotdquotij printf n A 234 B 432 C 23 D 45 345 543 34 34 45 23 L What is the output of the following program include ltstdiohgt main int xly2z3 ifxgty ifyltz printf d z else printf d y printfquotdn x A33l B41 C2 Dl 4 What is the output ofthe following program include ltstdiohgt main ifi3l ifi52 printf d i break i whilei 0 printf n A 7 B35 C5 D26 5 What is the output of the following program include ltstdiohgt int funint aint b ifb0 return a else returnfunab main printf dnquotfun42 A l B2 C3 D4 6 What is the output of the following program include ltstdiohgt include ltstdlibhgt int funint n int p pintmallocsizeofint pn return p main int a a funl 0 printf dn afunl 0 A0 Bl 0 C20 D Error 7 What is the output of the following program include ltstdiohgt void fun int a int b int t ta ab bt main int cl0l234567890i fori0ilt10i2 funcicil fori0ilt10i printf dquotci printf n Al234567890 B2l43658709 C098765432l D0l23456789 8 What is the output of the following program include ltstdiohgt struct st int x y data2l10220 main struct st pdata printf d pgty printf dn p gtx A10l B20l C102 D202 9 What is the output of the following program include ltstdiohgt void funint a int n int it fori0iltn2i tai aianli anlit main int k10 l 2345678910i funk5 fori2ilt8i printfquotdquotki printf n A345678 B876543 C1098765 D321678 10 What is the output of the following output include ltstdiohgt define N 4 void funint aN int b int i fori0iltNi bi aii main int XN11gt2gt3gt4gt5gt6gt7gt8gt9gt10gtYNi funxy fori0iltNi printf d yi printf quotn Al234 Bl070 Cl459 D34810 ll What is the output of the following program include ltstdiohgt int funint s4int n int k int mi mS0k fori1iltni ifsikgtmm si k return m main a441234111213142122232431323334 int printf dnquotfuna40 A4 B34 C31 D32 12 What is the output of the following program include ltstdiohgt main struct STU char name9 char sex double score2 struct STU aquotZhao 39m39850900b Qianquot39f950920 ba printf sc20 20fnquot bname bsex bscore0 bscore1 A Qianf9592 B Qianm8590 C Zhaof9592 D Zhaom8590 13 Assume we created a linked list as follows data next gt1b gt1c gt t t The statement to delete and free node q should be Apnextqnext freep Bpqgtnext freeq C pq freeq Dpgtnextqgtnext freeq 14 What is the output of the following program include ltstdiohgt main char a4 printf dn aaltlt1 A 40 B 16 C 8 D 4 15 What is the content of abcdat after the execution of the following program include ltstdiohgt main FILE pf char s1 State s2quotGeorgia pffopen abcdatquot quotwbquot fwrites271pf rewindp0 go back to the head of the file fwrites151pf fclosepf A State B Stateia C StateGeorgia D GeorgiaState 16 The output of the following program is 1 includeltstdiohgt main 1nt 1sum fori3igt1i sum0 forj1jltij sumij printf dn sum 17 The output of the following program is 9911 includeltstdiohgt main intja13579111315pa5 forG 3 j J39 switchj case 1 case 2printf d p break case 3printf d p 18 The output of the following program is 3 includeltstdiohgt define N 5 int funint sint a int n intj sa jn whileas j returnj main int sN1 int k fork1kltNk skk1 printf dn funs4N 19 The output of the following program is 15 include ltstdiohgt int funint x static int t0 returntx main int si fori1ilt5i sfuni printf dnquots 20 Assume we will print out the following graph Please complete the program 4 3 7 2 6 9 1 5 8 10 include ltstdiohgt main int x44n0ij forj0jlt4j fori3igtj i nxi n fori0ilt4i forj 0j ltij printf 3 d xi U D printfquotn 21 In the following program we try to count the number of the characters from the keyboard until is typed Please complete he program include ltstdiohgt long func main long n nfunc printf quotnldnquotn long func long m f0rm0getchar3939L return In Radmg MemogAllocz on Ba cs Allocated storage ls memory whose llfeleE ls under the control of the progammer Compare wth smuc storage used wth globally de ned varlables 39 B ene t 39 Whatlsvuid w 7 A polnter type that an be asslgned to m polnter type r l era 39nlllo me lre he ls used lrlsteld of l to allow for larger slzes than an r argue oorrtalrl For our purposes int ls ne and wlll be used ln Lhe resL of these slldes C Dynamic Memory Allocation Pointers on 6 Chapters 11 Dynamo Memory and 12 Llnked Ll sts also mlled Dynamu Memaryand Debugging CSI 352 e Systems Plus mmms and um Memog Allocauorl Baslcs corrurlued 100 int values kJhy ls acastrlotrequlredv he eue rgquimd l mum l e e mllo 100 a mum if mum ee lulu l fprintfltdrr quotnun Hills of lpl for but flildn39 mm p mum t h Whatlswrorlgvnththlsv fee l e or r lt 100 i m i mum hum i 39 2 l e e mllo sixguf mum if mum ee lulu p r quotHills of but flildnquot o Canalsodothsas in rintfltdr no c i r i0 ilt1oo i urine i i w 2 for r i 0 39lt lixu htjtr i intjtrli i w a aiecutbut wlll llkely muse a problem at some later polrlt lrl the code intjtrllool 200 urine 100 Dynamu Memaryand Debugging CSI 352 e Systems Plus mmms and um Memog Allocauorl Baslcs corrurlued elm w repuucernrc mum lur h elm merger elm weer burger e mllu 0 l 1 lreerrelm if burger e lulu rprlnerrnaerr emu Fee burger quotme r quotreell gh No the llee call 39 J Z L r The 1lsneeded to allow for the termlrlaurlg 1 character f r39l m f7 at the end of the erlng By nlLlon lixa hlrlsl butusmgsheufmh oduces Usage atype lrlto the resultthatlmproves portablllty er m replleeeers r elm want e replleeeera M ale In printiquotllll o See repllcdtezc lrl the Cexamples dlrectory bymc Memrysm bell CSI 352 e Systems Plus mmms and um Memog Allocauorl Baslcs corrurlued lorlger lrl use and making lt avallable for later use Thls wlll rurl forever lrl Java e e r fer l l loo i i e l w a for n l m nw imuool r lt In C allocated memory must be evplrculy released vla the 110 funcLlOn o Thls wlll run forever ln C for r l l 33983 5533 i a trim fprintfltdrr ma exhaust memory wlll become true fer l p r e r Note The c code wlll execute fasLer than the Java codel And not just because Java has to run or top of the JV pt I ireerpcr i lt loo i Dynamu Memaryand Debugging CSI 352 e Systems Plus mmms and um Memonr Allooauon Basrcs conunueol At rst glance rt seems srmple Ifmemory 15 allocated 1 mos nefreed However there are several faoets to oonsroer Ifmemory 15 allocated 1 must be freed Do no use memory after 1 15 peed Corottwy Do no free memory nd won be nsedlder Do no free d block ofmemorymore ndn onoe Do no free memory nd st no duoodted Anol of course Do no use memory ontstde ne bounds ofan duooded block 0 A sad fact CSI 352 7 Systems Plus mmms and um 5 Dynamo Memaxyand Debugging Memonr Allooauon Basrcs conunueol Common type of memory management error memory tedr Occurs when memory ls no longer needed but ls not freed Here ls an obvlous memory lak inun mlle100 w lixefint s if htjtrZHIILL fprintfltdrrr exita o Herelsalessobvlouslak for r 1 1s 1 20 i printiquotln replrearerr w s o How could ths be xed7 CSI 352 7 Systems Plus mmms and um s Dynamo Memaxyand Debugging Memonr Allooauon Basrcs conunueol As a rule the blgger anollor more frequent the lak the asler rt ls to nd A memory lak shoulol never olrreotly cause a progam malfuncLlon But a malfunouon wlll often result lf memory ls exhausteol Here s a oodrng pramoe that works m some cases yanable use the memory then free rt usrng the pornter alum e alreaeerr 1 printfquotlnquotr h2tr fmhtr Thls s mlled used after freedquot A common error s to free the memory then use t Tm mu m on many sysEms 160mm for example 0122 5 i 1 i lt 20 i Whatdoesltprln see rsptw o 2quot 51 I I Thls happens when one 0 oloes not change the contents of rj mmquot the memory belngfreed inward EU The standard for doeSmgumantaEMSbehavlor Thus rtrs oertarn rtwrll fall on some systems an 352 7 systems paganmnn and UNIX 7 Dynamo Memaxyand Debugging Memonr Allooauon B asros conunueol Another common memory management error r dallomuon aid 0 l anquot 39ehjtr mp1ilt10 x sangquot frhtr w t w aid glur 39ptr7h printf flnquotr pun 5h freean h w 9 w 0 Known as double freequot or mulLlple freequot X Themnleadtoaleak The authors of both funcnons remove the 110 CSI 352 7 Systems Plus mmms and um a Dynamo Memaxyand Debugging L39 ked L39 Implemenm39 n Assume the followlng genenc eeuee for the hnlteolhst emee elee pee tiltruzt Mr whuwr nela Irv neeaea fur the eleee w ltnlt neelegume rut l Jet e elenenes set nee aeJet uee when p o Crateanemptyllstmatcontamsonenode r j tum t Use eel he e magistruet w walla lixef ltnlt nud7ltnlt s if 1 Wu pt heunaet e Deemexum NULL The element eld of 11 mn be left alone or you can put approprlate empty Values m Heymg ths empty element at the end of the hst avolds seyeml speclal case sttuettons At the hopefully small memory cost of an eme node CSI 352 7 Systems Plvs mmms and um Dynamu Memaxyand Dehussms Lmlteol LISL Implementauon conunueo Flndlng somethlng m Lhellnked hst m zntjtr 1 set nee magistruet w is set nee elemntiltnlgt x ltnlt neelegume 3911 ltnlt neae Jet nee quotEurrentjtr 0 eueeene e e l e Takes advantage of shortecucultevaluetton m39 lt emuum um lgiuJeplece wth the coneot companson for eleeeemee r umMm Mghtbeafuncnon call e nee emen J eueeenueeammjee en emen m n find CSI 352 7 Systems Plvs mmms and um Dynamu Memaxyand Dehussms Lmlteol LISL Implementauon conunueo if want to insert 25 cum 4 quot CSI 352 7 Systems Plvs mmms and um Dynamu Memaxyand Dehussms Lmlteol LISL Implementauon conunueo The techmque lnsertafter Does not requlre a potnte to the prevlous element current a Check the speclzll mses Does ths work t the new element goes at the front of the hstv Does ths work t the new element goes after the end of the hstv mPePtr emee nudeiltnmt mum w nuaeue lpl fur the new use w mum e tnlpjtrxlemnt urmntjtrxlemnt tmpjtrmext Ilrrntjtrgtnxtjtr Ilrrntjtxgttlnnt x w put nw lee n gurmntjtr l eleee urrntjtxgtnxtjtr e tnlpjtr CSI 352 7 Systems Plvs mmms and um Dynamu Memaxyand Dehussms Llnked LlsL Implemenlauon conunued How to deleE an elemenL o Baslc lda But a ma funcLlon wlll remm a polnter to the element belng delaed CSI 352 e Sysems Plus mmms and UNIX 13 Dynamu Memaxyand Dehussms Llnked LlsL Implemenlauon conunued How when IS the element we want to delete perhaps after a call to nd use IS the element that comes after me element lo be deleted ltnlt elem Jenn quottngjtr tlllpjtr mix rentjtrmextjtr urrntjtxgtllnnt twgtrxlmmnt urrntjtxgtnxtjtr tnlpjtrmudgjtr 11 rum p w De quotor forget arm Check the speclal mses oes mls work If the element to delae IS atthemof Lhe llsm Does mls work If the element to delae IS the last elemenU CSI 352 e Sysems Plus mmms and UNIX N Dynamu Memaxyand Dehussms List of lists A lmlredllsr can have contents mm m part consist of omer llnked llsts Each llsl can end wth an empty node as per prewous dlscusslon Can also have web llst slarlwlm an emply header node CSI 352 e Sysems Plus mmms and UNIX l5 Dynamu Memaxyand Dehussms Debugging with gdb 0 Documema O u n gdb Home Fage my waw gnu orgzsoftwarel gdbdocumentaqu gdb GNU Debugger Commonly used m one of two ways needed 3le e a core dum quot Lermmated lLs execuucm CSI 352 e Sysems Plus mmms and UNIX 15 Dynamu Memaxyand Dehussms gab Ba cs Budlem documentauon s Very emenslve Try quothelpquot at the gun prompt eeeueeegt gee cuu gen eee nee mnu 53 x 004 Axh eupyngnc 200A Free Software Foundatlun Inc db h Type quothelpquot followed by a class name for a e e List of classes of commands hSLOf commands for matdass T 5115 quotfollowed acommandname heses ee meses er uenee eununenes 3m doiummmmby beeekpume ee lung peugee seep ee eeeeem punes data Exam data Command name abbrevauons are allowed mes ee eeryng ene exenunng mes fummb guous neeene e wen nee e enels unseuee ee ob ee reeeuees eunnng ee e nnng ene peugee seeek e axenng ene e k eeeus s us neume s ppm ee s puee remmes eeeepumes ee u ng er peugee exeeuemn menuue seuppng ene peugee useeeeerme sedennee eunnenes Type help i 11 d by e class neue f cuuuuends 111 that class gee Bascs conunued w gee g ueu e gunmen geeueexe Can also be done wa a Make te le arms g unu Note that addmg Lhe g ag causes the ereculable lo be someme larger eeeueeegt gee unl gelseexe e gunmen eeeueeegt l l neee eme e peemk eepe ozau Ape s s geneneemr eeeueeegt gee unl geeueexe e geeueex eeeueeegt l l gunmen eme e peemk eepe ausz Ape s s geneneemr CSI 352 e Sysems Plus mmms en umx Dynamu Memnryen Dehuggng Type nelpl renewed by eunnnene nee rue tun eueuueneeemn cununene nee ephemeemns eee enuwee r unemguuus ea C39 352 e sysens nesnnnns em UNIX 7 meme Menage Deluge gee Bascs conunued eeeueeegt gun gunmen cuu gen Red nee runux rsaooeusen gab quot b The prompt ndcales that gene s ready to proceed In the slmple case the 95 3152ch wthun melnesdsplayed 97 Am neemr39ene are Gaiter quotde su specl edllne number as r sun sue 100 eeneey e a 101 102 103 eeneey 10A punctrlmne ncegee matrlx es en 1dent1ty matrlxhA gab CSI 352 e Sysems Plus mmms en umx Dynamu Memnryen Dehuggng CSI 352 e Sysems Plus mmms en umx gee Bascs conunued To see more of Lhe progam you can press raum whlch wll repeal the lasl command 1 int n ths use gab 5 puncrrlmne ncegen matrlx s not en 1dent1ty uecnx 07 return r processilnteger r W The llst command has several forms A peueess float gee M1 1 ruse speeree runeemn en hne men nu eegunene hses een neee hnes ereee ue eeuune peemus seng ms e hses ene een hnes b ruee emus eenene 1 Mn hseee eeuune enee hne g ene eneung hnes en 11st Llnes cen be speclfled 111 these eys to 11st round that 11119 111 cuee nt rue FILE LINE c 1 uund e eune negnnng er ee run e en err uucurou en eusengusn eneng keeneee stem runeemns Auuesss en se eeeune ene ne eeneemng enee eeeee men We eegs r ene s enpey e seenes rue een nes ewey re ene eenee eeg b Dynamu Memnryen Dehuggng goo Basrcs conunued The rr command causes the target program to be emcuted gdb run 1 Stamng progam homepa1rrck3521Cexamples gdbemamx 71 3 2 330 150 178 1926 750 The 1nteger mamx 1s not an 1denuty mamx Program acted normally gdb 0 Can use 011er to 1ndxcaE BDF on stain Thephrase CSI 352 e Sysems Paws mmms and UNIX 21 Dynamu Memaxyand Debug 5115 gala Breakpomts o aiecutable code m the function gdb hrglk prxelliinkg oreakporno 1 at OXAOOEZO gdb run 1 soarorng program hornepaorrokaszCexanp1esgohenaorrx er 119 gohemaorrxm lure 55 areakporno 1 process rnoeger at gdbrmatxlxm s 1f scanf 31d ear 1 so no 1 gab 1 1 m me Can useabbrevmuonsthatare s2 rno roenoroy onam goons lforun here 51 rno son s4 rno army ss ss rr r soanrrroar earner 1 1 1 r 57 rprrnorrsooerr Falled to read sue of matrlxhA so oaaaer 59 so o o r no n o C31 352 e sysens pananmnn and um 22 Dynamo Menaryana Debugging goo Breakmmts cononoeo anan Jagger gdb hrglk prxelliinkg oreakporno 1 at OXAOOEZO gdb run 1 soarorng program hornepaorrokaszCexanp1esgohenaorrx er 119 gohemaorrxm lure 55 areakporno 1 processilnte 55 1f scanf d gdb print 5h 51 0 er 0 at gdbrmatxlxm s ssrze 1 1 gab rare 1 s1 rr onouo or annoarraa 1 gab prrne 1 2 A gab Thenexeoommandwrnerecorerhenemcommand Lheii r ennrr mthsmse prrne mnbeaborevraredasa I max can be abbrevmted as n CSI 352 e Sysems Paws mmms and UNIX 23 Dynamu Memaxyand Debug 5115 gabstepp along The nexe command an be used to srnglescep rhroogn a program grab a praeeatrneeaer areakporno 1 at oxnooozor 119 aanenaorrm 1m so grab r 1 soarorng program hornepaorrokas2Cexanrp1esgo1hernaorrx er orearporno 1 proaeaairnoeaer at E1onenaorrmrss it 1 1 r scanf kd oarze 1 1 non n 4 51 r r onouo s debugflag gab 5A r 5129 lt 0 H5129 gt 100 r gab 59 furr0 rltsrze x gab 70 ror r 1 1 lt 5129 c gab 1 r r scanf kd Slntimatrlxxc 1 1 r gab 7o ror r 1 a 1 lt 5129 1 gdb p humor mono s1 1 gdb p intimtrix ll 2 r1 0 ltrepeats as tunesgt CSI 352 e Sysems Paws mmms and UNIX u Dynamu Memaxyand Debug 5115 gab stepprn39 along oonunued The name command an be used to srngtestep througn a program goth a mu areatrpolnt 1 at OxA005c7 rlle gothematrlm lme 15 goth rm startlng program homepatrlok352Cexamplesgothematrlx el areatrpolnt 1 halo argoe2 argv0x7ffffff25AE at gothenatrlxor1s 15 mt rloatimatrlx e lntegerimatrlx e 0 gab n 1o lr argo lt 1 teeth 21 r strompargyr1 teeth 27 r argo l 2 gab gab else lr Mega arm Themnoommand 5 epsoverquot gab funcnonmlls A0 prooessirntegem The ma oommand steps mquot o tuncuon mlls o o o The lnteger matrlx ls an lotentlty matrlx A2 return 0 CSo 352 7 Systems Plus mmms ant umx gals where are w Most useful command 1n gab The where command or bt for back Lracequot shows a trace of the stack leoturae h gabward nat ornux 5300e1otrh rots rlle gotheworotso llne 133 goth run Tu mm folquot startlng program homepatrlotr3s2Cexamplesgotheworots er one Twu THREE foUR oreakpolnt 1 getiseaxchiwuxds argoes roh0x7rrrrraaso20 at gotheworotso133 133 011 1 l lt argo S l lt 21 1 g tisearchiwuxds axgt5 argv0x7rrrrmasr0 searoh0x7rrrrrasso20 at 3 1 0x0 gotheworotso1 OOOOOOOOOAOOE7O ln maln axgt5 argv0x7rrrrmasr0 at gotheworots hy does the progm tA9 aroma have i here 1nsterd of the name of m7 Extremety useful as a rst step 1n track mg down Segmentatton tauttquot Memory tauttquot and Bus errorquot probtemst CSo 352 7 Systems Plus mmms ant umx Dynamu Memoryant Dehussms Dynamo Memoryam Dehussms gab breakpoints setting and unsetttng A Can see current breakpomts wrth the ma command goth hrglk praeemoeaer ureatrpornt 1 at 0x200o20 rrle gothematrrm lme 55 teeth urea ts hpolnt 2 at OxAOOEcb rlle gothematrlxo llne 71 goth run startlng program homepatrlok3s2ltexamplesgothematrlx el oreakpolnt 1 processilnteger at gothematrlxoss 55 r soanrtttott e 1 goth eanetmue eontlnulng slze 1 lnteger at gothematr oreakpolnt 2 process 71 l stanf lx 1 d Slntimatrlxrt t goth his tare Num Type Dlsp Enb Astotress what hreakpolnt keep OXOOOOOOOOOOAOOEZO ln processilnteger at gothematrlxo55 hreakpolnt alreaoty hlt 1 tlme 2 hreakpolnt keep y OXOOOOOOOOOOAOOEcb ln processilnteger a gothematrlxo71 hreakpolnt alreaoty hlt 2 tlmes goth delete 1 goth eanesmue CSo 352 7 Systems Plus mmms ant umx Dynamu Memoryant Dehussms Working with cure les t A core le 1s a snapshot of the memory space of the process There 1s a bug 1n a out to scant 1n gdbemamxl c leoturaegt gamma i lt leeeolexe Memory rault leoturaegt l l eare ere e 1 patrrotr otept 129512 my 22 1507 oore Some of the errors that muse core les to be Grated o address space of the process Exampte a dwtstble by four oyenrntten buffer over ow attacks use Lhts technrque Amhmeuc excepuon 1s typrcatty mused by 1nteger dtvtston by zero CSo 352 7 Systems Plus mmms ant umx Dynamu Memoryant Dehussms Worklng wth sore les conunueo uer r wrlte permlsslon check your resource llmlts um mus Woes 7M muses ks rec sets we Woes me me blocks locks If you see somemmg other marl unllmlted for the core le as above Look for a l a or um samng m your shell starteup les An example u murmur o Delae or comment that lme logn agam Note occurred Carr happen wth dynamlcally allomted memory for example CSI 352 e Sysems Plus mmms and um Dynamu Memaxyand Dehussms CSI 352 e Sysems Plus mmms and um Worklng wth sore les conunueo o AfEr an error start gab wth both the emcutable and the core le 159mm gdbmatrixl I 011x r Lnxl 71 ogr cermmaceol wthslgnal ll Segnemauonfault l olorre LformSU39NW SuneFlrEVZSORllbllbcipsr so 1 olorre U39NW SuneFlrEVZSORllbllbcipsr so 1 0 Oxff3109 8 m number 0 fromllbllbc so 1 b w Oxff3109 8 m number 0 from lllbllbc so 1 1 Oxff310034ln olosmmu o from lllbllbc so 1 Dynamu Memaxyand Dehussms Regular expressions 0 Key to powerful efficient and exible text processing 0 De ned as a string composed of letters numbers and special symbols that de nes one or more strings 0 You have already used them in selecting files when you used asterisk and question mark characters to select filenames 0 Used by several Unix utilities such as ed vi emacs grep sed and awk to search for and replace strings Checking the author subject and date of each message in a given mail folder egrep quot FromlSubjectlDate quot ltfoldergt The quotes above are not a part of the regular expression but are needed by the command shell o A regular expression is composed of characters delimiters simple strings special characters and other metacharacters de ned below 0 Characters A character is any character on the keyboard except the newline character 7n7 Most characters represent themselves within a regular expression All the characters that represent themselves are called literals A special character is one that does not represent itself such as a metacharacter and needs to be quoted gtIlt The metacharacters in the example above with egrep are quot l and We can treat the regular expressions as a language in which the literal characters are the words and the metacharacters are the grammar o Delimiters A delimiter is a character to mark the beginning and end of a regular expression Delimiter is always a special character for the regular expression being delimited The delimiter does not represent itself but marks the beginning and end of the regular expression Any character can be used as a delimiter as long as it the same character appears at both ends of the regular expression More often than not people use forward slash 7 as the delimiter guess why If the second delimiter is to be immediately followed by a carriage return it may be omitted Delimiters are not used with the grep family of utilities o The rnetacharacters in the regular expressions are quot In addition the following rnetacharacters have been added to the above for extended regular expressions such as the one used by egrep l The dash is considered to be a rnetacharacter only within the square brackets to indicate a range otherwise it is treated as a literal gtllt Even in this case the dash cannot be the rst character and must be enclosed between the beginning and the end of range characters 0 The regular expression search is not done on a word basis but utilities like egrep display the entire line in which the regular expression rnatches 0 Simple strings The most basic regular expression Matches only itself Examples Reg Exp Matches Examples ring ring ring spring ringing stringing Thursday Thursday Thursday Thursday7s or not or not or not poor nothing 0 Special characters Cause a regular expression to match more than one string Period gtllt Matches any character gtllt Examples Reg Exp Matches Examples alk All strings that contain a space will talk followed by any character may balk followed by alk ing all strings with any character singing preceding ing ping before inglenook 091798 Date with any separator 09 1798 091798 Square brackets gtIlt Define a class of characters that matches any single character within the brackets gtIlt If the first character immediately following the left square bracket is a caret 7 the square brackets define a character class that match any single character not within the brackets gtllt A hyphen can be used to indicate a range of characters gtIlt Within a character class de nition the special characters backslash asterisk and dollar signs lose their special meaning gtIlt A right square bracket appearing as a member of the character class can only appear as the first character following the square bracket gtIlt A caret is special only if it is the first character following the square bracket gtIlt A dot within square brackets will not be a metacharacter O7 17 98 will not match 071798 but will match 071798 1 gtllt Examples Reg Exp Matches Examples bBlill Member of the character class bill b and 13 followed by ill Bill billed taeiouk t followed by a lowercase talkative vowel any character and a k stink teak tanker number 6 9 number followed by a space and a member of the character class 6 through 9 number 60 number 8 get number 9 azA Z any character that is not a letter 1 7 Asterisk gtIlt Can follow a regular expression that represents a single character gtIlt Represents zero or more occurrences of a match of the regular expression gtIlt An asterisk following a period matches any string of characters gtIlt A character class de nition followed by an asterisk matches any string of characters that are members of the character class gtIlt A regular expression that includes a special character always matches the longest possible string starting as far toward the beginning of the line as possible gtllt Examples Reg Exp Matches Examples abc a followed by zero or more b7s ae followed by a e abe abbe debbeaabbbe ab c ab followed by zero or more other abe eharaeters followed by a e abxe ab45e xab 756345 X eat t ing t followed by zero or more thing eharaeters followed by ing ting I thought of going azAZ a string composed only of letters 1 any string without and spaces numbers or punctuation as long a string as possible Get this and that between and W the shortest string possible that this starts with and ends with Get this and that Caret and dollar sign gtIlt A regular expression beginning with a caret 7 can match a string only at the beginning of a line The regular expression cat nds the string cat anywhere on the line but cat matches only if the string cat occurs at the beginning of the line is used to anchor the match to the start of the line gtIlt A dollar sign EB7 at the end of a regular expression matches the end of a line The regular expression cat nds the string cat anywhere on the line but catEB matches only if the string cat occurs at the end of the line it cannot be followed by any character but newline not even space gtllt Examples Reg Exp Matches Examples T a T at the beginning of a line This line That time 09 a plus sign followed by 5 4572 a number at the beginning 759 Keep this of a line 55 a colon that ends a line below Quoting special characters gtIlt Any special character except a digit or a parenthesis can be quoted by preceding it with a backslash gtIlt Quoting a special character makes it represent itself gtllt Examples Reg Exp Matches Examples end all strings that contain end The end followed by a period send pretendmail a single backslash an asterisk 1lt c an asterisk 5 5 it was ve andor andor andor 0 Rules Longest match possible gtIlt A regular expression always matches the longest possible string starting as far towards the beginning of the line as possible Empty regular expressions gtIlt An empty regular expression always represents the last regular expression used gtIlt Let us give the following command to vi smikerobert gtIlt If you want to make the same substitution again the following is sufficient srobert gtllt You can also do the following mike srobert o Braeketing expressions Regular expressions can be bracketed by quoted parentheses and The string matching the bracketed regular expression can be subsequently used as quoted digits The regular expression does not attempt to match quoted parentheses A regular expression Within the quoted parentheses rnatehes exactly with What the regular expression Without the quoted parentheses will match The expressions rexp and rexp match the same patterns 15 Quoted digits gtIlt Within the regular expression a quoted digit n takes on the value of the string that the regular expression beginning with the nth matched gtIlt Assume a list of people in the format last name first name initial gtIlt It can be changed to the format first name initial last name by the following vi eommand Zs 2 1 Quoted parentheses can be nested gtIlt There is no ambiguity in identifying the nested quoted parentheses as they are identified by the opening gtllt Example az AZ X matches 3 t dMNORX7 1 u 0 Replacement string vi and sed use regular expressions as search strings with the substitute eommand Ampersands 85 and quoted digits n can be used to match the replacement strings Within the replacement string An ampersand takes on the value of the string that the search string matehed Example s 09 09 Number 85 0 Word boundaries The word boundaries in the regular expressions are denoted by any Whitespaee eharaeter period end of line or beginning of line Expressed by lt beginning of word gt end of word 0 Regular expressions cannot be used for the newline eharaeter Chapter 13 Systems Programming Graham Glass and King Ables UNIX for Programmers and Users Third Edition Pearson Prentice Hall 2003 Original Notes by Raj Sunderraman Converted to presentation and updated by Michael Weeks 2mm exam 1 Systems Programming UNIX System calls C functions that provide access to the file system processes and error handling S stem Calls Vrou ed into 3 main categories File Management Fig 131 Process Management Fig 132 Error Handling Fig 133 IIEEJZJ csKEII 2 Error Handling Most system calls are capable of failing in some wa Example open a file may fail because file does not exist System call returns a value of 1 when it fails This value does not tell much about the cause of the failure 2mm exam 3 Error Handling global variable errno holds the numeric code of the last systemcall error function perror0 describes the systemcall error void perrorchar str standard C function in stdioh Displays str followed by followed by a description of the last system call error Error 0 is displayed if no error See example showErrnoc IIEEJZJ csKEII a Example File lO r Open a nuanXlstErltfllE El eause an errurV ru uperl ri riEXlSt m oinoonm imu zenrm W an erruruccurred Vi prinm errnei n n errnei r rpree a dlffererlt erreir r m uperl ll oivvnonm l mu nnulnlerrnei rreir rnainl 2mm exam 5 Example File lO Execute a successful System call V d erl riol lexlsttxt X OiRDONLY l OicREAT 0644 all V i Display after succesle c prlrltf errno uedln x errno nWill display previous error nurn 21 perror rnalrl errno o r Manually reset error vanable v perror mam l IIEEJZJ csKEII 5 m 535 m xsngm a quota a S i 2 2m File O File A sequence of bytes Directory A le that includes info on how to nd other les e7 l 1 i J 1 O 2mm exam 1 File O Start atthe current directury which is an attribute erme prucess accessing the path name Links Symbolic Link 43BSD Afile cuntainingthe path name at anuther le can acruss lersystem buundaries Hard Link mine csKEII u File O File Descriptor Nonne ative inte er returned b 0 en or creat 0 OMEN AX 9 y p irtually unrbuundedfurSVRzl e14 3ESD Each descriptor has its own private set of properties le pointer stores offset within le changes on readwritdlseek llag indicating ifthe le descriptor should be closed or not when ecs llag indicating ifoutput to le should be appended to end of le or not others Perprocess base POSIX1 0 STDINFLENO 1 STDOUTFLENO 2 STDERRFLENO ltunistd hgt Cunventiun empluyed by the UrilX shells and applicatiuns 2mm exam IS File lO File Manipulation Operations 0 en close read write seek du fcntl ioctl trunc rename chmod chown mkdir cd opendir readdir closedir etc Typical File O Sequence int fd Iquot file descriptor 0 std in 1 std out 2 std error l fd open leName if fd 1 Iquot deal with errorquot fcntl fd Iquot set l0 flas if needed I readfd Iquot read from file I writefd Iquot write to le ls kf Iquot seek within le I ee closefd Iquot close le I new exam n File O open include ltsystypesgt include ltsysstathgt include ltfcnthgt l luvi FilePath Name ORDONLY OWRONLY ORDWR OAPPEND OTRUNC OCREAT OEXCL ONONBLOCK openpathname OWROIILY I OCRERT I OTRUIIC mode mine csKEII 12 What is the output of the following program include ltsystypes hgt 1nc1qgeltsys tiathgt include ltfcnt1 11gt Iih39hclude ltst dxo hgt inglude ltsgguib hgt in 0 ih t ma int 81 fd39z39 Edi 6pehquotfooltxtquot 0RDONLY o c1ase d1 5d 2 o en bo392txtquot7 39ox ndnLY piniffquotfd2 391mquot 5 2 exit0 open always returns lowest unopened descriptor 2nn2712ri 332 19 File lO creat and close include ltsystypesgt include ltsysstathgt include ltfcntlhgt openpathname OWRON LY OCREAT OTRUNC mode This interface is made obsolete by open Only for writeaccess Q What if you want to create a le for READ and WRITE include ltunistdhgt All open les are automa ically closed by the kernel when a process terminat Closing a le descriptor releases any record locks on that le 2nn2712ri csZZZEI 2n mode Flags for OCREAT De ned in ltsysstathgt S IRUSR owner read permit SJWUSR owner write permit SIXUSR owner execute permit open myfile OCREAT SRUSR SIXOTH 2nn2712ri 332 21 File lO lseek include ltsystypesgt include ltunistdhgt Current le offset in bytes changes le pointer whence SEEKSET SEEKCUR SEEKEND 7 determines how offset l5 to be used 7 SEEKSET offset relativeto start offile e SEEK7CUR offset re ative to current position offile e SEEKEND offset relativeto end offile offt typedef long offt I 231 bytes or typedef longlongt offt I 263 bytes lseek fails if moved before start of le 7 returns new file positron ifsuccessful e returnsrl ifunsuccessfu 2nn2712ri csZZZEI 22 Current Offset of lseek To nd out current posi ion use currentO set lseekfd 0 SEEKCUR If you move past the end of le and write there Unix automa ically extends the size of the le and treats intermediate area as NULLS 0 Unix does not allocate disk area for intermediate space see next example 2nn2712ri 332 23 uinclune lt5ystypesngt uinclune ltunistdhgt uinclune ltstninngt uinclune ltstnihngt int mainuni lt if lseekSIDlNFlLEN n SEEKEHR 71 printfquotcannnt 522knquot 2B printfquot522k nKnquot exit 2nn2712ri csZZZEI 24 sysltyPESJI ufnclune sysstatm me we 5 50 Meg ulnclu e ltFcntngt en gt 0000 00 a o c d e r g n r r 0 o 0 0 0 0 gt 0000020 0 2 gt0000040ABCDEFGHiJ0 O gt 0000060 int mainwni int HI if m creatquot i2hn2quot rugnnnm lt n 2rrsy5quotcr23t ennrquot if mum nun 1n 1n ensys39nun wnte ennrquot offset nw 1n if 1522krn an SEEKSEI 1 quotVivaquot seek error offset nnw un if unread hufz 1n 1 1n errs Squothuf2 write errorquot offset nnw 5n exitn gt 200x71 u csAAAU 25 File lO read and write include ltunistd hgt 7 copy upto count bytes from tne nie referenced by rd into bufferbuf returns 0 it it atternptsto copy after end of file returnsrl ifunsuccess ul 7 tne bytes are read from current position nie pointer wnicn l5 tnen updated accordiri l 7 Less tnan noytes of data are read EOF0 terminal deyice iineinputynemork buffering recordrorierited devices e g tape 4 error ffsetisincreasedforeyeryread include ltunistdhgt 7 Copy upto count bytes from a bufferbuf ii ito tne file referenced by fd 7 Write errors rordrswuii orfilersizerlimit causes 7 When OiAF PEND l5 set tne file offset l5 set to tne end 0fthe file before eacn Write operation 200271271 csZZZEI 25 Suppose that foobartxt consists of the BASCII characters quotfoobarquot Then what is the output ofthe following program l memo int 61 Edz chaz c openquotfogl aztxt ok fd2 openvi o39obax39t xt o eadgfou sc 1 eadfd2 so 1 pxiht w gemquot c exit0 19 0 4 o3939 The descriptors fd1 andfd2 each have their own open le table entry 0 ach descriptor has its own le position for foobantxt Thus the read 39om fd2 reads the rst byte of foobartxt lO Efficiency How do you move from home to dorm 2002424 csZZZEI 27 200271271 csZZZEI 22 Closmg a file close mncluda quotnur nrmquot quotII F39 BUFFSIZE 81V n we int closeint fd int mhw Frees fd releases resources lt When a process terminates all fds are automatically closed in n char hu BIFFSIZE unin n rea STDINFILEN huh BUFFSIZED gt u if writeSYD ulFlLENn nur a n ms quot H y write or r if n lt n ensysquotrem errorquot exitn 200271271 csZZZEI 29 Still it is a good idea to close yourself Returns 0 if success 1 if failure 200271271 csZZZEI an File lO Sharing Table perprucess le desflags a Winter include ltsysmle ngt intflucxmntfd irtuperatiun mocxgsH LOCKiEX LOCLQUN 2mm exam 1 System Call stat int statconst charquot name struct statquot buf stato lls the buffer bufwith information about le e The stat structure is de ned in usr ncudesysstath and includes the following elds stmode he permission ags stuid he user D stgid he group ID stsize the le size statime the last access me stmtime the last modi cation time stctime the last statuschange time IIEEJZJ csKEII Macros in lusrincludesysstath SSDRstmode true if directory SISCHRstmode true if le is a character special device SSBLKstmode true if le is a block special device SISREGstmode true if a regular e SSFFOstmode true ifa pipe 2mm exam 3 Variations of stat stat same as stat except it returns information about the symbolic link itself rather than the file it refers to fstato same as stato except it takes file descriptor as first parameter All return 0 if successful and 1 otherwise IIEEJZJ csKEII Example Using stat Call updateStato if the le 7 l5 a regular le 7 character Special me or e blockspecial le 7 Eitheradds or Ll dates the le s status entr If status has changes updateEntryo is called to display le39s new status TLI 439 I39 39 nefimnll 2mm exam as Directory manipulation DIR opendir const char dirname struct dirent readdir DIR dirstream closedir IIEEJZJ csKEII 36 Opening a Directory Stream Function DIR opendir const char dirname e The epenurrruncnen epens and returns a directury strearn rer readingthe urrecterywnesenie narne is dimame The strearn nas type DIR e irunsuccessrui upendirretums a null perrter Data T 92 structdlrent e ans is a structure type usedtu return rnrerrnatren apeut directury ertnes e Itcuntainsthefulluwing elds enaruarneurnrsrstnenuirterrnrnateumenarnecprnppnent ranUrienprnrsrstnenie serrainurnper unsreneuenaruarnienrnrsrstneienetnprtnerrienarnenptrnciuurnetne terrnrnatrne null haiaclei itstvpersunsreneuenarpecausetnatrstnernteeertvrne er the armrpprrate srze unsreneu chaidepEThis islhelva wine we pussiva unknuwn rnerpupwrne cunstanlsaiedeiinedfu sv ue e nnurtrawr4wlt x murmur xtlealwale puree 2mm exam 7 Reading and Closing a Directory Stream Function struct dirent readdir DIR dirstream e Thisfuncnun readsthe nelttentryrrern tne directury It nurmally returns a puinter te a structure cuntaining rnrerrnatren apeuttnenie ans structure is statically allucated and can be rewritten by a subsequentcall e irtnere are ne rnere entnes in tne directnry er an er is detected re ddir remms a null puinter The fulluwing errne errpr cunditiuns are uenneurertnrs runcnen EEADF The otrstrearn argurnert is nut valid Functiun rntciosedirmR dirstreamJ e ansrunctren cluses tne directury stream dirstream treturns D Em success and 4 un rariure e The fulluvving errnu errurcunditiunsare defined rertnrsrunctren E ADFThedirstreamamumenlisnulvalid mew csKEII a Sample code EZSZZZZSSZIZW rrrirrerntvururt Dik dpr struct dirent epr dp apenditCV Mdp nutmt wquot le ep veeddw urn pmsepgt 3quot 9r vad clasedw din rerse pevvm Cauldnt apen the divedDN l return u r 2mm csKEII 9 Reading Directory Information getdents tgetdentsmntfd struct dirent pm intstructSize ynopsls in ude ltunistd ngt in mu n ude ltllnuxunism ngt nc r an gt rntgetuertsunsrgneu lntfd struct urrert urrp unsigned intcuunt Descnptlon r s nettne runctren yuu are interested in Luck at reauurnsnertne POSIX unfurming c irpraryrrterrace ans page ducumentsthe pare Kernel system call rnterrace The system call etaentso reads several dirent structures rrern tne directnry minted at by fdl e tne rnernery area puinted te by urrp The pararneter 0 came ertnernernery area The dirent structure is declared as fulluws struct dirent I lung u rne r inude number err td BIT r errsette nextdirent unsigned snert direElen r length crtnrs dirent cnar diname NAMEJIAXHL r nienarne nuiieterrnrnateuyr mew csKEII w Reading Directory Information getdents The dirent struct is defined in lusrincludesysdirenth and contains the following fields dino the inode number doff the offset of the next directory entry dreclen length of the dirent struct dname the leName 2mm exam M Reading Directory Information getdents Returns length of the directory entry if successful 0 if last directory entry has already been read 1 if error processDirectory function skips and and uses Iseek to advance to the next directory entry mew csKEII oz System call chmod int chmod const charquot leName int mode int fchmod int fd int mode These change the mode of IeName to mode speci ed as octal ex 0600 Set user ID and set rou ID ars have values 04000 and 02000 respectively Int rharhm Intflag flag chmod testmt xuauut if mag perrur prublem setting mode 2mm exam stmct trht E strmrhe e format date and time SYNOPSIS Ihcluue ltImE hgt SIZE strftirnechar s SIZEJ maX cohst char furmat cohst structtrn trn DESCR F39TION The strrtrrheo tuhetroh torrhats the erkEnrdDWn time trh accurdlngtu the furmatspecificatiun torrhatahu places the result In the character array s or size rhax 2mm em Intimisec r secohus can I leap SEEDnd l llmiml erhutes Desar rhttmjuuh r Hours Maw l ttmimday r Day 31 v IHUNJWEIH r Month D4117 intimiyear fVeare IBEIEI Intimiwday r Day orweeh 1 Inttmiydav r Days In year macaw Inttmilsdst r DST 4 ifdef iusEjsD nglnttmigmtul f rsecohus east orurc stchar tmizuney FTImEZDnE abbrevlanun else lung Imitmigmtulf r secohus east orurc v icunstchar itmizuney FTImEZDnE abbrevlauun v Endlf I mm calm AME asctrrhe ctrrhe gmtrrhe locaitrrhe mK mE asctlmeir ctlmeir gmtlmeir lucaltlmeirr trahsrorrh uate andtlmetu meEnrdWntlme Dr AS I s OPSIS Includ lttlmE hgt char asctlmecunststructtm 39Im har c rcohststructtrh m char buf char ctrrhecohsttrrhe7t39trrhep char ctlmei cunsttimeit tlmep char buf structt gmtlme st mej39IlmEp struct g timeircunsttlmeit tlmepystructtm rEsulU structtrh Iucaltlmemunsttlmej lmep structtrh I ti Ef cunstnmeit tlmep stmcttm result Weft rhhtrrhestructtrh my DESCRIPTION ThectimEOrEMINEOandlucaltimeoYunctiunsalltakeanamumentulnataWpEtlmerhich representscaiehuarhme When interpreted asah absulute time value itvepvesentsthe r 1 s h m urc m csKEII Process Management Every process in UNIX has some code some data a stack 1 The parameters 2 The address on the stack ofthe calling programs stack 39a 3 T retu 4 The automatic local variables a unique process ID PID 2mm exam me he return addresswhere to go when the function rns Init Process When UNIX starts boots there is only one process called inil 1 with process ID m e The onlywayto create a new process is to ouphcate ah existing process 7 So inIisthe ancestorofall supsequeht processes Ini ially inil duplicates forks several times and each child process replaces its code execs with the code of the executable getty which is responsible for user logins usein csKEII Parent and Child Processes It Is very comm of its child proc For examp le Consider the way a shell process executes a u ility in the foreground esses termIna The shell rst duplicates itself using for The parent process waits for he child process to terminate using waifo When the child process terminates using exif the parent process is informed using a signal the parent process presents the user with the shell prompt to accept the next command 2mm exam on for a parent process to suspend itself until one 39 tes H s its code with that of he utility using execo Differentia es System Call fork pitt forkvoid Causes a process to duplicate The child process is almost exactly a duplicate ofthe parent process 7 lnherlts a copy or lts parent s cude oats stack open flle oescnptors ano slgnal tables 7 The only dlfference l n the process Ds pro and parent process Ds Fwd lffork 0 succeeds it returns a value ofO to the child process and the PID ofthe child process to the parent lfforko fails it returns a1 to he parent process andthe child process is not cre e 2mm As be ore suppose foobartxt consists of 6 ASCII characters quotfoobalquot Then what is the output of the following program inc major chat c m e npenf fnnhaxtxt n nnmrul n ifffnxktl e n xeadlfd cc 1 exil mom quotsome EC 1 1 E nn cl Chlld lrlherlt s the parent s descnptortable So chlld and parent share an open nle table entry Hence they share a nle posltlon c o 2mm System Calls fork getpido getppido exit0 wait0 exec family ant371271 csKEII Sample code 331nmqu ltstnllp hgt pluitchlluiplu r storage place tor the pro or the chllo process and lts exltstams lnt hlluistatus lZlEIJZlEI Ehlluiplu romoy r lets fork or a chllo process swltcn chlloiplon r check whattne fork call acmally oro l case cl Ffurk falledV perrur furllt F pnnt a systemcueflneu error message EXl case n Homo succeeueu we re lnsloe the chllo pruress llltl prlrm hellu worlorrorn chllol lZDod FDamW lJ EXltE r here the CHlLD process eats notthe parent oeraolt from succeeded we re lnsloe the parentpruess H5l Waltamphlluistatusy r Walttlll the chllo process Exlts prlrtf hellu worlorrorn parentl lZDod jZDadm hm lr parent s process cooe rnay untlnue here return El l System Call getpid pidt getpidvoid pidt getppidvoid Return the process39 ID and parent process ID respectively They always succeed The PPID value for process with D1 is 1 ant371271 csKEII Example Program with getpid mf i stdw aw l m the uriginai pmcess With WD w arid FWD quotm n 92mm 0 gamma 0 mm 0 ll ate cmiu amp parehtcuritiriuefrum here it pld 1 El r pld is nonzero an i mustbe the parent prim l m the parehtprucess With WD quotm arid FWD mm 92min 0 gamma 0 mm My child s WD is mm pld l Else r pld is zero 5 i must bathe hild prim l m the Ehild process With WD quotm arid FWD w w 92min 0 getFWd 0 prihtf PlD w terminates n getpid 0 r Euth processes Execute this l 2mm Exam ss Example Program With getpid rhyforllt l rh the original process With PlD 639 and PPlD 416 the parent process With PlD 639 and PPlD 416 PlD 639terrhihates l rh the child process With PlD 640 and PPlD1 D 640terrhihates mm calm 56 Orphan Processes If a parent process terminates before its child terminates the child process is automatically adopted by the nit process The following rorram shows this 2mm Exam 57 Example Program of Orphan aw 1 Salim l l l Else r pld is zero su i mustbethe hild sleep 5FMallte surethatthe arerittermiriatesfirst prihtf l m the min prDEESSWith WD w arid FWD w n getpid 0 getppid 0 l priritf l PlD w terminatesw getpid 0 r Euth processes Executethis mm calm m Example Program of Orphan orphah l rh the original process With PlD 680 and PPlD 416 l the parent process With P D 680 and PP D 416 My child s PlD is 681 D 680terrhihates l rri the child process With PlD 681 and PPlD1 D 681 terminates 2mm Exam 59 System Call exit void exitint status When a child process terminates it sends its parent a SIGCHLD signal and waits for its termination code status to be accepted Only lower 8 bits of status are used so value 0255 closes all of a process39 file descriptors deallocates its code data and stack then terminates the process 5 l0 return code is called a zombie process A parent accepts a child39s termination code by executing waito mm calm m System Call exit The kernel makes sure that all of a terminating process39 children are adopted by nit by setting their PPID39s to 1 nit alwa s acce ts its children39s termination code exit never returns 2mm exam Example of eXIt cat rn extc lnclude lt5tdlo ngt lnclude lt5tdllb ngt needed forexlt V rnaln t prlntf l arn golng to exlt Wltn status code 42n exlt42l l myexlt l arn golng to exltvvltn status code 42 ecnc status ls 7 42 may csKEII Zombie Processes A process cannot leave the system until parent process accepts its termination code even if it has exited If parent process is dead init adopts process and accepts code If the arent recess is alive but is unwilling to acce t the child39s termination code because it never executes waito the child process will remain a zombie process 2mm exam Zombie Processes Zombie processes do not take up system resources But do use up an entry in the system39s xedsize process table Too many zombie processes is a problem may csKEII Zombie Example lnclude ltstdlc ngt rnaln t lntp d pld forM noupllcate v n Brancn based on return value from fork v lfpld l o n Nevertennlnate never execute a Walt v Wnlle 4 sleep 4000 elsel exlt 42y Exlt Wltn a sllly number v l 2mm exam Zombie Example zcrnble amp l684 95 684 p4 s o 00 Zomble 685 p4 z o 00 Zomble ltZornblegt 686 4 R s W 684 ll 41 Terrnlnated Zomble 688 64R OOOpS 39 may csKEII Waiting for a Child wait pidt waitint status causes a ocess to suspend until one of its child processes terminates A successful call to wait returns the PID of the child process that terminated places a status code into status 2mm Waiting for a Child wait If a process executes a wait and has no children wait returns immediately with a value of l If a rocess executes a wait And one or more children are already zombies Then wait returns immediately with the status of one of the zombies 2mm Waiting for a Child wait Status code is encoded as If the rightmost byte of status is zero ost byte contains the low 8 bits ofthe value by the child39s exito or returno call If the rightmost byte of status is nonzer the rightmost 7 bits are equal to he Signal Number that caused the child tot hate the lettm returned the last bit is set to 1 ifthe child core dumped mums csKEII Example Program usmg wait nclude ltstdlu hgt maln 0 l lrlt p d status chlldPld prlrltf l m the parentprucess and my PlD ls quotmm getpldO id rum l r Du llcate lr pld l Eur Branch based en remm yalue rmm term l m the parent pmcess Wlth PlD Wad and PPlD WadV l K saw 0 sample 0 FWalttura chlld tn terminate enlluplu waltampstatus pnrttf A chlld Wlth PlD Wad termlrlated Wlth egtltlt Dde drl l EhlldPld status gtgt a l else l pnrltf l m the chlld precess Wlth PlD Wad and PPlD WadV l K saw 0 sample 0 Xlt42 r EXlthth a sllly number E l prlrltf PlD van termlnateslnlu getpld 0 l mums Example Program using wait mwyalt l rn the parent process and my PlD ls 695 process WltHPD695 and PPDAls 96 and PP D 695 95terrnlrlate5 2mm csKEII Differentiating a Process exec With the exec family a process replaces Its current code Data Stack PID and PPID stay the same Only the code that the process is executing changes mums csKEII Differentiating a Process exec Use the absolute or relative name int execlconst char path const char arg0 const char argn NULL int execvconst char path const char argv 2mm 633m 7 Differentiating a Process exec Use PATH variable to find the executable int execlpconst char path const char arg0 const char argn NULL int execvpconst char path const char argv mrm csKEII 7o Differentiating a Process exec argi or argvi ith command line argument for executable argO name of executable lf executable is not found l is returned otherwise the callin rocess re laces its code data and stack with those of the executable and starts executing the new code A successful exec0 never returns 2mm 633m 75 Example Using execl include ltStdl ngt mam 0 l printf l m pruces w and l m abouttu e execl l xec an ls rln getpld minls l rl NULL FExecutE ls t um s printf Thls line should neverbe mm a l a myexec l m process 7lE and l m abuuttu Exec an ls rl tal SE mm ral mm 22 2m 24 alarm c 226ml 22 2m 22 background 5 284ml 22 2m 22 mychdirc ral QUEEJul 22 2m 23 vuunt 5 tray ral 4174Jul2412 ma ZEImblE r lral ral 298ml 22 2m 2m ZEImblE c mrm csKEII 7s Changing Directories chdir Every process has a current working directory Used when processing a relative path name A child process inherits the current working directory from its paren Example Wnen a utility is run from a shell the utility process inherits the shell39s current working directory 2mm 633m 77 Changing Directories chdir int chdirconst char pathname Sets a process39 current working directory to pathname The process must have execute permission from the directory for chdir to succeed chdir0 returns 1 if it fails 0 if it succeeds mrm csKEII 7a ulditsetuldmlditld uldit geteuld uldit Seteuldmldj id gtdtgetgtd ulditsetgldglditld gidit getegid uldit Setegidgid7t id 2mm m E Example Using fork and exec Executes a program in background Original process creates a child process to exec the specified executable and terminates The orphaned child process is adopted by init mew csKEII a Example Using fork and exec include lt5tdi0 hgt main mt argc char argv exeop argvl ampargvt t execute Sn argvtl A r rintf stderr Could no i a background gcc myvvait 0 2mm exam 2 Review C commands here work with OS Error Handling File O and management en read write close stat mew csKEII a Review System Commands 2mm Review Working with directories and file entries Process Management chdir nice mm csKEII BSD Sockets UNIX Sockets Networking protocols are implemented as part of the operating system 7 The exported networking API is the socket interface 7 Originally provided by BSD 410 71982 The principal abstraction is a socket R Stevens UNIX Network programming VOL 1 7 Point at which an application attaches to the network Beej s Guide to Socket Programming httpbeej usguidebgnet 7 De nes operations for creating connections attaching to network sendingreceiving data closing 59 Connectionoriented example TCP Server Server Client ClientServer Paradigm Create a TCP socket Assign a port to socket Set socket to listen Communicate Repeatedly Close the connection a Accept new connection recv Dalalreue 55mm b Communicate Process 0 Close the connection We DB 1 gt Create a TCP socket Establish connection Jesniv7 close close 3 Connectionless example UDP Server Client socket Data request dc sen 0 Data reply closeo Closeo Preparation step Address Setup 2 struct socksddr 5 unsigned short safamily Address family eg AFIIIET V 5 char sadata1 l Protocolspecific address info V 5 struct socksddrrn unsigned short sinfamily n RFIIIET V unsr ned short sin ort Port isbits 395 struct inadd sinsddr Internet address szbits 3 char sinrerolal not used V U 7 E struct insddr unsigned long saddr Internet address 32bits V Network vs Host Byte Ordering 7 Store 32bit integer value in memory for example 4A3 B 2C 1 D 7 Native host representation LittleEndiarl MSB last 39 dinjarriily can be in Host Byte Order LittleEndian 7 Network byte order BigEndiarl MSB rst 39 Use for multibyte binary data exchange 39 htonlO htorls ntoth ntohso 39 major and niaddr must be in Network Byte Order BigEndian sockaddrl Farm Blob 2bytes 2bytes 4bytes 8bytes I I sockaddrjnl Family Port Internetaddress Notused 6 socket 1nclude ltsystypeshgt 1nclude ltsyssockethgt lnt socket1nt domain int type lnt protocol domain is either AFiUNIX AFJNET or AFOSI or AFiUNIX is the Unix domain it is used for communication within a single computer system AFiLOC L is the Posix name for AFiUNIX AFJNET is for communication on the intemet to IPv4 addresses type is either SOCKisTREAM nection oriented reliable or SOCKiDGRAM UDP datagram unreliable or SOCKiRAW I level 39 itisusuallyo r a default protocol for the chosen domain and type Returns ifsuccessful arocmderm39pmr which is an int It returns 1 in case offailure bind 1nclude ltsystypeshgt 1nclude ltsyssockethgt lnt b1nd1nt sd const struct sockaddr addr lnt addrlen sd File descriptor of local socket as created by the socket lnction addr Pointer to protocol address structure of this socket eg sockaddriin or so un addrlen Length in bytes of structure referenced by addr Returns an integer the return code 0success 1failure connect 1nclude ltsystypeshgt 1nclude lt5yssockethgt int connect1nt 5d const struct sockaddr addr int addrlen sd le descriptor of local socket addr pointer to protocol address of other socket ie the one you Want to connect to addrlen length in bytes of address structure Returns an integer 0success 1failure listen int llsten1nt fd int qlen d le descriptor of a socket that has already been bound qlen speci es the maximum number of connection wait LAW ei er quot requests that can i 39 another connection request Uu ei ILIII Returns an integer 0success 1failure accept 1nclude ltsystypeshgt 1nclude ltsyssockethgt int accept1nt fd struct sockaddr addressp int addrlen m i an int L 39 39r a ll tenin mi in fact it is called the listening socket i e on which the server has successfully completed socket bind and listen addressp points to an address It will be filled with address ofthe calling client We that in 39 L 39 length quot 39 391 r 39 It IS the socket that the server will use from now on to cornrnun with the client that requested connection in fact it is called the connected socket Different calls to accept will result in different connected soc e emeiuuci I I quot until a connection is actually accepted you can change that with fcntl send int send int sockfd const void msg int len int flags sockfd is the socket descriptor you want to send data to whether it39s the one returned by socket or the one you got with acceptO msg is a pointer to the data you want to send It can be any sort of structure ags I am told you should leave this as 0 but see the man page for more info Returns the number ofbytes actually sent If less than len then you must resend the rest sendto for sending over UDP include ltsystypeshgt lnclude lt5yssockethgt int sendtoint 5d char buff int len int flags struct sockaddr addressp int addrlen sd socket file descriptor buff address of buffer with the information to be sent len size ofthe message flags usually 0 could be used for priority messages etc addressp address of process We are sending message to addrlen length of message Returns number of bytes sent It is 1 in case of failure recv int recvint sockfd void buf int len unsigned int flags sockfd is the socket descriptor to read from but is the buffer to read the information into len is the maximum length of the buffer and ags can again be set to 0 See the rech man page for ag information Returns the number of bytes actually read into the buffer or 1 on error recvfrom recv for UDP 1nclude ltsystypes hgt lnt recvfrom int sd r buff lnt ien int flags struct sockaddr addressp lnt addrlen sd socket le descriptor buff address of buffer Where message Will be stored len size of buffer ags usually 0 used for riority messages peeking etc addressp bu er that Will receive a dress ofprocess that sent message addrlen contains size of addressp structure Returns number of bytes received It is 1 in case of failure gethostbyname include ltnetdbhgt struct hostent gethostbynamemonst char name 39 tuastruct h t ht struct hostent l char r Mh addr list c deflne hadd hiaddrilistml And here are the descriptions ofthe elds in the struct hostent hiname Of cial name of the host hiallmzs A NULLterminated array of alternate names for the host hiaddrtypz The type of address being returned usuallylawn hilzngth The length of the address in bytes hiaddrillst A zeroterminated array of network addresses for the host Host addresses are in Network Byte Order hiaddr The first address in hiaddrills t l7 Practical issues You have to be very careful when using these calls 39 Speci c data structures and formats 39 Ports cannot be less than 1024 You can use other tools to see if things are working 39 netstat Client and server can be on same system Think about error handling methods Client must know the server s address and port Server only needs to know its own port Ch 8 Networking Ethernet hardware standard defining cabling signaling and behaviors that allow two or more computers to be connected Each computer contains a Ethernet card a special piece of hardware that has a unique Ethernet address Every computers Ethernet card is connected to the same piece of wire When a computer wishes to send a message to another computer it broadcasts the message along with a destination address Only the Ethernet card whose address matches the destination address accepts the message When a collision occurs two messages sent at the same time both computers wait a random period of time and rebroadcast the message Bridges Special hardware connecting two segments of Ethernet cable serial connection between two groups of computers Routers hooks together two or more networks and automatically routes messages to the proper network Gateways High capacity routers which connect Local Area Networks These are place geographically apart WAN Internet Collection of LANs and WANS working together packet switching messages are split into small packets each of which is routed independently switched through the network specially encoded information in packets allow them to be recombined at the destination internet addresses Internet Protocol IP version 4 addressing is a hardware independent labeling scheme 32 bit address made up of 4 bytes each part is has a value between 0 and 255 IPversion 6 scheme 128 bit addresses numerical addresses are not convenient so names are used etchosts file contains namesnumbers for all local host names routing done statically using information in etcroute file or dynamically using etcrouted and etcgated programs which constantly update the routing tables and sharing this with nearby hosts remote shells if a user has accounts on several machines in the network with the same userid they are able to execute commands on other machines without providing a password from tinmancsgsuedu I can execute X rsh zeus who if you have a file called rhosts in your home directory with a list of host names full names then this is possible machine equivalence Network related Unix commands User Information users displays a simple list of users on your local host rusers a host displays a list of users on all hosts on the local network X who X rwho X W all describe users logged on in more detail X hostname displays your host name finger userId display personal information about users Communicating with other users mesg y l n enables or disables writing to your terminal write userId tty X write userId tty writes line by line to the userId s terminal stop using CTRLD X talk userId tty interactive writing split screen wall write all Moving Around X rlogin host X ftp host telnet host X rsh host command 1 Regular expressions 0 Key to powerful efficient and exible text processing 0 De ned as a string composed of letters numbers and special symbols that de nes one or more strings 0 You have already used them in selecting files when you used asterisk and question mark characters to select filenames 0 Used by several Unix utilities such as ed vi emacs grep sed and awk to search for and replace strings Checking the author subject and date of each message in a given mail folder egrep quot FromlSubjectlDate quot ltfoldergt The quotes above are not a part of the regular expression but are needed by the command shell o A regular expression is composed of characters delimiters simple strings special characters and other metacharacters de ned below 0 Characters A character is any character on the keyboard except the newline character 7n7 Most characters represent themselves within a regular expression All the characters that represent themselves are called literals A special character is one that does not represent itself such as a metacharacter and needs to be quoted gtIlt The metacharacters in the example above with egrep are quot l and We can treat the regular expressions as a language in which the literal characters are the words and the metacharacters are the grammar o Delimiters A delimiter is a character to mark the beginning and end of a regular expression Delimiter is always a special character for the regular expression being delimited The delimiter does not represent itself but marks the beginning and end of the regular expression Any character can be used as a delimiter as long as it the same character appears at both ends of the regular expression More often than not people use forward slash 7 as the delimiter guess why If the second delimiter is to be immediately followed by a carriage return it may be omitted Delimiters are not used with the grep family of utilities o The rnetacharacters in the regular expressions are quot In addition the following rnetacharacters have been added to the above for extended regular expressions such as the one used by egrep l The dash is considered to be a rnetacharacter only within the square brackets to indicate a range otherwise it is treated as a literal gtllt Even in this case the dash cannot be the rst character and must be enclosed between the beginning and the end of range characters 0 The regular expression search is not done on a word basis but utilities like egrep display the entire line in which the regular expression rnatches 0 Simple strings The most basic regular expression Matches only itself Examples Reg Exp Matches Examples ring ring ring spring ringing stringing Thursday Thursday Thursday Thursday7s or not or not or not poor nothing 0 Special characters Cause a regular expression to match more than one string Period gtllt Matches any character gtllt Examples Reg Exp Matches Examples alk All strings that contain a space will talk followed by any character may balk followed by alk ing all strings with any character singing preceding ing ping before inglenook 091798 Date with any separator 09 1798 091798 Square brackets gtIlt Define a class of characters that matches any single character within the brackets gtIlt If the first character immediately following the left square bracket is a caret 7 the square brackets define a character class that match any single character not within the brackets gtllt A hyphen can be used to indicate a range of characters gtIlt Within a character class de nition the special characters backslash asterisk and dollar signs lose their special meaning gtIlt A right square bracket appearing as a member of the character class can only appear as the first character following the square bracket gtIlt A caret is special only if it is the first character following the square bracket gtIlt A dot within square brackets will not be a metacharacter O7 17 98 will not match 071798 but will match 071798 1 gtllt Examples Reg Exp Matches Examples bBlill Member of the character class bill b and 13 followed by ill Bill billed taeiouk t followed by a lowercase talkative vowel any character and a k stink teak tanker number 6 9 number followed by a space and a member of the character class 6 through 9 number 60 number 8 get number 9 azA Z any character that is not a letter 1 7 Asterisk gtIlt Can follow a regular expression that represents a single character gtIlt Represents zero or more occurrences of a match of the regular expression gtIlt An asterisk following a period matches any string of characters gtIlt A character class de nition followed by an asterisk matches any string of characters that are members of the character class gtIlt A regular expression that includes a special character always matches the longest possible string starting as far toward the beginning of the line as possible gtllt Examples Reg Exp Matches Examples abc a followed by zero or more b7s ae followed by a e abe abbe debbeaabbbe ab c ab followed by zero or more other abe eharaeters followed by a e abxe ab45e xab 756345 X eat t ing t followed by zero or more thing eharaeters followed by ing ting I thought of going azAZ a string composed only of letters 1 any string without and spaces numbers or punctuation as long a string as possible Get this and that between and W the shortest string possible that this starts with and ends with Get this and that Caret and dollar sign gtIlt A regular expression beginning with a caret 7 can match a string only at the beginning of a line The regular expression cat nds the string cat anywhere on the line but cat matches only if the string cat occurs at the beginning of the line is used to anchor the match to the start of the line gtIlt A dollar sign EB7 at the end of a regular expression matches the end of a line The regular expression cat nds the string cat anywhere on the line but catEB matches only if the string cat occurs at the end of the line it cannot be followed by any character but newline not even space gtllt Examples Reg Exp Matches Examples T a T at the beginning of a line This line That time 09 a plus sign followed by 5 4572 a number at the beginning 759 Keep this of a line 55 a colon that ends a line below Quoting special characters gtIlt Any special character except a digit or a parenthesis can be quoted by preceding it with a backslash gtIlt Quoting a special character makes it represent itself gtllt Examples Reg Exp Matches Examples end all strings that contain end The end followed by a period send pretendmail a single backslash an asterisk 1lt c an asterisk 5 5 it was ve andor andor andor 0 Rules Longest match possible gtIlt A regular expression always matches the longest possible string starting as far towards the beginning of the line as possible Empty regular expressions gtIlt An empty regular expression always represents the last regular expression used gtIlt Let us give the following command to vi smikerobert gtIlt If you want to make the same substitution again the following is sufficient srobert gtllt You can also do the following mike srobert o Braeketing expressions Regular expressions can be bracketed by quoted parentheses and The string matching the bracketed regular expression can be subsequently used as quoted digits The regular expression does not attempt to match quoted parentheses A regular expression Within the quoted parentheses rnatehes exactly with What the regular expression Without the quoted parentheses will match The expressions rexp and rexp match the same patterns 15 Quoted digits gtIlt Within the regular expression a quoted digit n takes on the value of the string that the regular expression beginning with the nth matched gtIlt Assume a list of people in the format last name first name initial gtIlt It can be changed to the format first name initial last name by the following vi eommand Zs 2 1 Quoted parentheses can be nested gtIlt There is no ambiguity in identifying the nested quoted parentheses as they are identified by the opening gtllt Example az AZ X matches 3 t dMNORX7 1 u 0 Replacement string vi and sed use regular expressions as search strings with the substitute eommand Ampersands 85 and quoted digits n can be used to match the replacement strings Within the replacement string An ampersand takes on the value of the string that the search string matehed Example s 09 09 Number 85 0 Word boundaries The word boundaries in the regular expressions are denoted by any Whitespaee eharaeter period end of line or beginning of line Expressed by lt beginning of word gt end of word 0 Regular expressions cannot be used for the newline eharaeter Chapter 2 Unix Utilities for nonprogrammers Graham Glass and King Ables UNIX for Programmers and Users Third Edition Pearson Prentice Hall 2003 Original Notes by Raj Sunderraman Converted to presentation and updated by Mic ael Weeks BZ ZEIEIE csaazn Getting Started Obtain an account OR install Linux Logging in locally enter username and password remotely use the sshtelnet utility to sign on to Upon logging on you will see a prompt usually or which is displayed by a special kind of program called a SHELL program To set your password use passwd BZ ZEIEIE csaazn 2 Shells Popular shells Bourne Shell Korn Shell C Shell Bash Bourne Again SHell All have common core functionality some differences Each shell has its own programming language Shell programming BZ ZEIEIE csaazn Running Utilities To run a utility simply type the name ofthe utility after the prompt Some utilities date man clear stty passwd Utility date yymmddhhmmss Utility clear Utility man s section word man k keyword To logout enter CTRLD or exit BZ ZEIEIE csaazn 6 Special Characters list meta characters with stty a command quot means control quotC means hold down control key CTRL and pressthe C key BZ ZEIEIE csaazn 5 Special Characters erase CTRLH erase character before cursor werase CTRLW erase word before cursor kill CTRLU erase entire line rprnt CTRLR reprint line To test these type some input like one two three four at the terminal promp move cursor around try the above characters winInna Special Characters intr CTRLC program interrupt running 0 x n U 0 0 39T N suspend running stop CTRLSCTRLQ stop printing to screen eof CTRLD give program end of le To test these Try a command like nd For eof try cat gt test le then CTRLD on new line BZ ZEIEIE csaazn 7 Some common UNIX utilities pwd oat like concatenate more page head tail ls od mv op rm mkdir rmdir le determine file type we word count lp vi pioo emaos groups ohgrp ohmod BZ ZEIEIE csaazn Path n a mes homemweeksxc lexpo rthomemweeks U sersmwee ks lusroraclemO1lapporacleproduct804binsqlpl refers to current directory example aout refers to parent directory example p2cpp BZ ZEIEIE csaazn a Example of cat 39 catgt letter Hi Mom Please send me money David quotD 39 ls l letter nvrr 1 qcheng other letter BZ ZEIEIE csaazn 1U 38 May 2811220 List Files Is 39 Is angdR lt espeogt options a hidden les I long listing g group F put character a er le name indicating executable link directory socket s num disk blocks d dir details not contents R recursive listing BZ ZEIEIE csaazn M Show File Contents cat more oat lt e speolistgt list contents of files on screen without pause 39 more lt e speolistgt same as oat pauses after each screen more pace bar takes you to next screen g quit m shows next line help key for more commands BZ ZEIEIE csaazn 12 Show File Contents page head tail Working With Directories page ltfiespec listgt mkdir ltdnamegt same as more clears screen before each page mal e a new eetory quicker o Cd ltdirgt 39 59 head 39 lt e39spe gt builtin to shell dis la 11 lines from front of file p y 9hanges shell to a different directory 39 tall n lt e specgt pwd display 11 lines from end of file Qrint working directory BZUZEIEIE csaazn l3 BZUZEIEIE csaazn ii Rename Files mv Copy Files cp rename files simply change labels in the file Op 39 ltOdgt new h39erarChY ifor inquire mv i ltodgt ltnewgt i for inquire prompt if ltnewgt already exists mv i ltfiespecgt ltdirgt prompt if ltnewgt already exists op i ltfiespecgt ltdirgt op ir ltfiespecgt ltdirgt rfor recursive mv I ltdlrgt ltdlrgt Removing Files rm rmdir Line Printing Commands remove delete file Print file ltfiespecgt to printer dest rm fir ltfie specgt lp d dest n copies ltfiespecgt f force inhibits all promptsmessages 6150 W i inquire Get status oflobs at I rinter dest r recursive Ipstat dest Be careful This is a permanent deletion Stop a PrlntJOb o remove delete directory Need job number ltrequestidgt from rmdirltdirgt above cancel ltrequestldgt Word Count word count wc cw lt e specgt c counts the bytes counts the newline characters w prints the word counts BZEIZEIEIE csaazn File Attributes JVVX 7777 W I 32qu cscqxcx 3661 Jan 9 2312 RIngEufferAmOutI Iandleriava rwx le type rst char permissions le type regular d dir b buffered le disk drive c unbuffered le terminal I link i e s so 1 hard link count cscqxcx le owner cscqxcx le39s grou 3661 le size in b es Jan 9 2312 le modi cation date RingBufferActOutHandlerjava le name BZEIZEIEIE csaazn File Type file ltfnarnegt ascertains the type of file asciibinary etc example cscqxcxtechie BatteFiedOnFramework le BFComputat onc BFComputat onc39 ASCII C program text with CRLF line terminators BZEIZEIEIE csaazn File Type ample cscqxcxtechie testbed le BattleFieldOnFrarnework directory mework0924 tar POle tar archive RingBu erActOutHander java ASCII Cprogram text Samples directory try ASCII text BZEIZEIEIE csaazn Process Attributes When a process executes it has a real user ID an effective user ID a real group ID an effective group ID BZEIZEIEIE csaazn Process Attributes When you log in your login shell process has its realeffective user id set to your user id realeffective group id set to your group id BZEIZEIEIE csaazn Process39 File Permissions When a process runs the file permissions apply as follows If process39 effective user id owner of file User Privilege applies If process39 effective user id is not owner of file Privilege applies Otherwise Other Privilege applies BZ ZEIEIE csaazn but its effective group id file39s group id Group Real user id is used for accounting purposes only Process Access Rights Process Access Rights depends on who executes the process NOT who owns the executable Sometimes undesirable Example game of rogue file of best scores needs to be modified ON LY by rogue program process when player ends game NOT by players39 shells BZ ZEIEIE csaazn Process Access Rights Special feature 2 special file permissions set user ID 5 instead of x set group ID When an executable with set user ID is executed the effective user ID of the process becomes that of the executable NOT of the players shell Allows process to modify best scores file BZ ZEIEIE csaazn Groups list the groups a particular user belongs to groups ltuseridgt change the group a file belongs to chgrp R ltgroupnarnegt lt e specgt R stands for recursive create su bshell with effective group id grou p newgrp ltgroupnarnegt BZ ZEIEIE csaazn Change File Permissions change file mode ugoa rwxs user group other all read write execute set plus adds permissions minus subtracts Also allows octal numbers BZ ZEIEIE csaazn Change File Permissions Examples chmod R XXX ltfilespecgt chmod R gw ltfilespecgt chmod R urw ltfilespecgt chmod R uwgr ltfilespecgt BZ ZEIEIE csaazn Change File Permissions Example with octa numbers mweekscarmaux ls l exampl etxt rwrr 1 mweeks mweeks 51 200708231606 examplelxt mweekscarmaux chmod 765 examplelxt mweekscarmaux ls l examplelxt rwxrwrx 1 mweeks mweeks 51 20070823 16 06 examplelxt Notice 7 111 binary r on w on x on 5 101 binary r on w an x on BZ ZEIEIE cs332 Change File Owner change owner of file chown ltnew ownergt ltfie specgt only super user can do this BZUZEIEIB cs332 The vi Editor visual text editor Very common on Unix systems Created by Bill Joy in 1976 Many versions exist including vim vi improved BZ ZEIEIE cs332 The vi Editor Command mode vs Text Entry Mode To enter text entry mode use the following commands iaAoOR to get back to command mode use ESC line ranges 135 1 2 512 12 BZUZEIEIB cs332 The vi Editor Cursor Movement up arrow k down arrow j left arrow h right arrow beginning of line 0 first nonwhitespace char in line quot end of line 39 BZ ZEIEIE cs332 The VI Editor Cursor Movement next word previous word back b Goto line number nn nn Go to last line Arithmetic expression BZUZEIEIB cs332 The vi Editor The vi Editor CTRLD down half screen Deleting Text I CTRLU up half screen delete character under cursor x CTRLF forward one screen T dame word dW delete line dd CTRLB back one screen also D delete many lines ltrangegtd Replacing text replace char under cursor r replace word under cursor cw replace entire line cc substitute next 4 characters 45 mm 25332 37 mm 25332 38 The vi Editor The vi Editor Pasting text Searching ltrangegty yank lines search forward sss 5yy yank 5 lines search backward sss P SearchingReplacing P ltrangegtssssttt nnp after line nn ltrangegtsssstttg pu mm 25332 39 mm 25332 N The vi Editor The vi Editor SavingLoading Misc write to file w ltfnamegt re draw screen CTRLL write again w execute a shell command comand write ltrangegtw o Quitting ltfrlamegt quit q T erte and qu39t 39Wq write and quit wq T ed39t saw me e ltfnamegt quit without writing q T read 39nsen a we r ltfnamegt exit and write if changed x mm 25332 M mm 25332 n Review Shells and Special Characters Common Utilitie Working with Di Removing Files File AttributesT Process Attribut S rectories ypes 88 File permissions groups and owner The vi editor BZ ZEIEIE csaazn Chapter 3 UNIX Utilities for Power Users Graham Glass and King Ables UNIX for Programmers and Users Third Edition Pearson Prentice Hall 2003 Original Notes by Raj Sunderraman Converted to presentation and u dated b Michael Weeks A m Q Q an Grep Filtering patterns egrep fgrep grep grep hilnvw pattern fileName dis la s lines from files that match the pattern pattern regular expression h do list file names if many files are specified i ignore case displays list of files containing pattern n dis la line numbers v displays lines that do not match the pattern w matches only whole words only nnno n quotD A m Q Q an Grep variations Tgrep I pattern must be TlXECl string egrep pattern can be extended regular expression x option in fgrep displays only lines that are exactly equal to string extended regular expressions matches one or more of the single preceding character matches zero or one of the single receding character either or ex a b o operate on entire subexpression not just on preceding character ex ab ba nnno n quotD A m Q Q an Sort sort tc r POS1 POSZ sortField ben fileName tc separator is c instead of blank r descending instead of ascending b ignore leading blanks f ignore case M month sort 3 letter month abbreviation n numeric sort POS1 POSZ key positions start up to end A m Q Q an nnno n quotD Sort Examples cat sortdat John Smith 1222 20 Apr 1956 Tony Jones 1012 20 Mar 1950 John Duncan 1111 20 Jan 1966 Larry Jones 1223 20 Dec 1946 sort 0 2 sortdat John Duncan 1111 20 Jan 1966 John Smith 1222 20 Apr 1956 Larry Jones 1223 20 Dec 1946 Tony Jones 1012 20 Mar 1950 A m Q Q an Sort Examples LD bUlL hr 395 39iVl DUI LUGL John Duncan 1111 20 Jan 1966 Tony Jones 1012 20 Mar 1950 John Smith 1222 20 Apr 1956 Larry Jones 1223 20 Dec 1946 sort 4 5 sortdat John Smith 1222 20 Apr 1956 Larry Jones 1223 20 Dec 1946 John Duncan 1111 20 Jan 1966 Tony Jones 1012 20 Mar 1950 nnno n quotD A m Q Q an Comparing Files What if you have two files with the same name but different sizes cmp s file1 file2 offset1 offset2 com ares two files for e luality reports the first byte where there is a mismatch if one file is a prefix of the other EOF message is displayed A m Q Q an Comparing Files cmp s file1 file2 offset1 offset2 Optional values offset1 and offset2 are the offsets into the files where comparison begins 0 tion dis la 3 the line number and byte offset of all mismatched bytes s 0 tion su I resses all output A m Q Q an Comparing Files diff i file1 file2 compares two files outputs a description of their differences i fla iinores case A m Q Q an Archiving tar cfrtuvx tarFiIeName fileList creates a tape archive format file from the fileList Typically use either c or x but not both c for Create x for Extracts t option generates a table of contents r option unconditionally appends listed files to tar formatted file A m Q Q an Archiving tar cfrtuvx tarFiIeName fileList u option appends only files that are more recent than those already archived f option enables you to give a tar file name Default name is devrmt0 v verbose If fileList contains directory its contents are appendedextracted recursively A m Q Q an nnno n quotD Archive Example tar CVf muwinlar muwin ls al mgwin total 24 drwxrwxrX 6 cschCX cschCX 4096 Jan 23 2339 drwxrwxrX 6 cschCX cschCX 4096 Jan 23 2349 drwxrxrX 2 cschCX cschCX 4096 NOV 18 2006 bin drwxrxrX 3 cschCX cschCX 4096 NOV 18 2006 doc drwxrxrX 3 cschCX cschCX 4096 NOV 18 2006 include drwxrxrX 2 cschCX cschCX 4096 NOV 18 2006 lib ls alR mgwin A m Q Q an Archive Example Y rwrwr 1 cscqxcx cscqxcx 5396480 Jan 23 2349 mgwintar tar tvf mgwintar drwxrwxrX cscqxcxcscqxcx 0 20080123 233936 mgwin drwxr xrX cscqxcxcscqxcx 0 20061118 175439 mgwinbin rwxrxrX cscqxcxcscqxcx 11643 20061118 175439 mgwinbinmingwmlOdll drwxr xrX cscqxcxcscqxcx 0 20061118 175436 mgWIIlCIOC drwxr xrX cscqxcxcscqxcx 0 20061118 175440 mgwindocmingwruntime rwrr cscqxcxcscqxcx 1112 20061118 175440 mgwindocmingw runtimeCONTRIBUTORS rwrr cscqxcxcscqxcx 519 20061118 175440 mgwindocmingw runtimeDISCLAHVIER rwrr cscqxcxcscqxcx 398 20061118 175440 mgwindocmingwruntimeREADME drwxr xrX cscqxcxcscqxcx 0 20061118 175447 mgwinmclude nnno n quotD Nan3n 1 CompressUncompress compressuncompress compress fiIeName Z uncompress fiIeName gzip gzip fiIeName gunzip fiIeName crypt crypt key lt sampletxt gt samplecrypt to crypt crypt key lt samplecrypt gt sampletxt to uncrypt key could be any string nnno n quotD A m Q Q an Archive Example rm rf mgwin tar rvf mgwintar netstattxt netstattxt tar xvf mgwintar mgwin mgwinbin mgwinbinmingwm10dl mgwindoc mgwindocmingwruntime mgwindocmingwruntimeCONTRIBUTORS mgwindocmingwruntimeDISCLAIMER tar xvf mgwintar grep netstattxt netstattxt nnno n quotD Nan3n 1E Find Utility find pathList expression Finds files starting at pathList Finds files descending from there Allows you to perform certain actions eg deleting the files A m Q Q an Find Utility name pattern true if the file name matches pattern perm oct true if the octal description of file39s permission equals oct type ch true if the type of the file is oh bbock cchar useruse d true if the owner of the file is userld group groupld true if the group of the file is groupld nnno n quotD A m Q Q an Find Utility atime count true if the file has been accessed within count days ctime count true if the contents of the file have been modified within count da s or an of its file attributes have been modified exec command true if the exit code 0 from executing the command command must be terminated by f is specified as a command line argument it is replaced by the file name currently matched nnno n quotD A m Q Q an 10 Find Utility prnt prints out the name of the current file and returns true s displays the current file39s attributes and returns true expression negation of expression ex r1 a ex r2 short circuit and expr1 o expr2 short circuit or nnno n quotD A m Q Q an Find Examples find name asserth searches for file asserth in the entire file system find mtime 14 ls lists files modified in the last 14 days find name 39bak39 ls exec rm 1 Is and then remove all files that end with bak A m Q Q an nnno n quotD Find Examples find mgwin name 39lib39 mgwinlib mgwinliblibcoldnamea mgwinliblibcrtdlla mvwinliblibvmona mgwinliblibma mgwinliblibmingw32a mgwinliblibmingwexa mgwinliblibmingwthrda mgwinliblibmoldnamea mgwinliblibmoldnameda mgwinliblibmsvcr70a mgwinliblibmsvcr70da mgwinliblibmsvcr71a mgwinliblibmsvcr71da mgwinliblibmsvcr80a mgwinliblibmsvchOda mgwinliblibmsvcrta mgwinliblibmsvcrtda Nan3n 31 Hard Links In original newLink Creates a hard link to original file called newLink Both labels will refer to the same file File will be deleted only when both labels are removed If newLink is a directory then links are made within the directory A m Q Q on Hard EAaIIIpIe cat gt hold This is an example ll In hold hold1 cat hold1 This is an example ll vi hold1 changed an to a great cat hold1 This is a great example ll cat hold This is a great example ll Is I hold rwrwr 2 cscqxcx cscqxcx 22 Jan 28 1328 hold rwrwr 2 cscqxcx cscqxcx 22 Jan 28 1328 hold1 nnno n quotD A m Q Q an 3 Soft Symbolic Links In s original newLink Symbolicsoft links can be created from one file system into another file system Hard links are restricted to one file system Use ls IL to view link details which file it refers to ls l displays the contents of the symbolic link ls l lananan 1 root root 23 20060922 2026 javac gtj2sdk14205binjavac ls lL ananrx 1 10016 users 66156 20040604 0032 javac ls lj2sdk14205binjavac ananrx 1 10016 users 66156 20040604 0032 j2sdk14205binjavac nnno n quotD Nan n Substituting User su userName If userName is not specified root is assumed Need access privileges for this Requires password sudo command User can execute command as superuser Requires password whoami Display the name of the owner of the shell A m Q Q an Scheduling Command cron 39 crontab cronTabName crontab ler useName Allows you to schedule a series ofjobs Executed on a periodic basis Uses a file with the following line format Text following a are copied to a temporary file and used as the commands standard input minute hour day month weekday command 059 023 131 112 17 1Monday any command A m Q Q an Scheduling EAaIIIpIc minute hour da month weekda command 059 023 131 112 17 1Monday any command cat crontabcron echo One Minute passed 0 12 29 1 mail users Are you ready for EXAM nnno n quotD A m Q Q on Scheduling EAaIIIpIc crontab crontabcron crontab DO NOT EDIT THIS FILE edit the master and reinstall crontabcron installed on Sat Jun 26 233335 2007 Cron version Id crontabcv 213 19940117 032037 vixie Exp echo One Minute Passed gt devpts1 pwd gt devpts1 3O 14 29 1 mail users Are you ready for exam1 crontab r unregister crontab file nnno n quotD A m Q Q an quotDO Scheduling Command at at command allows you to schedule onetime commandsscripts at csm time date year increment script c option use Cshell s option use Bourne shell m option send mail time is specified as HH or HHMM followed by an optional AMPM A m Q Q an Scheduling Command at at r jobld at jobld r option remove entry from at queue option list entries in at queue A m Q Q an Scheduling Command at at csm time date year increment script time is specified as HH or HHMM followed by an optional AMPM date is spelled out using first 3 letters of day andor month keyword now can be used in place of time sequence keyword today tomorrow can be used In place or date sequence stated time may be augmented by an increment number followed by minuteshoursdaysweeksmonthsyears A m Q Q an Scheduling EAaIIIpIc cat atCsh lbincsh echo at done gt devtty1 at now 2 minutes atCsh at l lists the job here You may program the script to reschedule itself as follows lbincsh date gt devtty1 at now 2 minutes atCsh nnno n quotD A m Q Q on Pattern Scanning and Processing awk utility that scans one or more files and performs an action on all lines that match a particular condition The conditions and actions are specified in an awk program awk reads a line breaks it into fields separated by tabsspaces or other separators specified by F option A m Q Q an awk Command awk program has one or more commands condition action where condition is one of the following special towns UEGIN Ol an expression involving logical operators relational operators andor regular expressions A m Q Q an awk Command awk condition 1 awon action is one of the following kinds of Clike statements if else while for break continue assignment statement viableexpression print printf next skip remaining patterns on current line exit ski s the rest of the current line list of statements A m Q Q an awk Command accessing individual fields 1 n referto fields 1 thru n 0 refers to entire line builtin variable NF means number of fields awk F 39 print NF 1 39 etcpasswd prints the number of fields and the first field in the etcpasswd file F means to use as the field separator A m Q Q an awk Command BEGIN condition is triggered before first line is read END condition is triggered after last line has been read FILENAME builtin variable for name of file being processed We will use this data in following examples cat etcpasswd nobody 22Unprivileged Userusrbinfalse root0OSystem Administratorvarrootbinsh ib2626Printing Servicesvarspoocupsusrbinfase nnno n quotD A m Q Q an awk Example quot0 cat p2awk BEGIN print quotStart of file quot II II II II END print quotEnd of filequot FILENAME awk F f p2awk letcpasswd Start of file nobody lusrbinfuwv root varroot binsh Ip varspoocups lusrbinfalse End of file letcpasswd nnno n quotD A m Q Q an awk Operators builtin variable NR contains current line remember quotFquot uses colon as separator cat p3awk NR gt1 ampamp NR lt4 print NR 1 6 NF awk F f p3awk etcpasswd 2 root varroot binsh 7 3 daemon varroot usrbinfase 7 nnno n quotD A m Q Q an awk Variables cat awk4 EGIN print quotScanning filequot Drintf quotline d snquot NR 0 lineCount wordCount NF lD rintf quotlines d words dnquot lineCount wordCount awk f awk4 float anning file 1e 1 Wish I was floating in blue across the sky 1e 2 My imagination is strong 1e 3 And I often visit the days 1e 4 When everything seemed so clear 1e 5 Now I wonder what I39m doing here at all les 5 workds 33 nnno n quotD A m Q Q on awk Control Structures cat awk5 for i NF i gt 1 i printf quots i printf quotnquot awk f awk5 float sky the across blue in oating was strong is imagination My days the visit often IAnd Clear so seemed everything vv en all at here doing I39m what wonderl Now nnno n quotD A m Q Q an awk Condition Ranges Condition ranges two expressions separated by comma awk performs action on every line from the first line that matches first expression until line that matches second condition awk F 39 nobodyroot print O39 etcpasswd nobody22Unprivileged Userusrbinfase rootOOSystem Administratorvarrootbinsh awk 39strong cear print O39 float My imagination is strong And I often visit the days When everything seemed so clear nnno n quotD A m Q Q an awk Builtin Functions Builtin functions expo logo SqrtO substr etc awk F 39print substr11239 letCpasswd no ro i6 cat awk8 awk f awk8 test A m Q Q an Stream Editor sed sed scans one or more text files performs an edit on all lines that match a condition actions and conditions may be stored in a file may be specified at command line in single quotes commands begin with an address or an addressRange or a Regular expression does not modify the input file writes modified file to standard output A m Q Q an Things You Can Do with sed a append lines to output until one not ending in c change lines to following text as in a d delete lines A insert following text before next output I list line making all nonprin7 racters visible tabs appear as gt lines broken with P print line q quit for scripts r le read le copy contents to stdout b label branch to command label nnno n quotD A m Q Q an Things You Can Do with sed spat1pat2f substitute pat2 for pat1 f g replace all occurrences f p print f W le write to le tlabel test branch to label if substitution made to current line W le write ines to le ystr1str2 replace each character from SM with corresponding cnaracter trom strz no ranges allowed print current input line number lcmd do sed cmd if line is not selected 39 label set label for b and tcommands nnno n quotD A m Q Q an Al Substituting Text sed 39squot 39 file gt filenew indents each line in the file by 2 spaces o seu 39squot 39 file gt filenew removes all leading spaces from each line of the file v w 39ad39 file gt filenew deletes all lines containing 39a39 Example add two lines in the bevinnin39 of the file nnno n quotD cat sed1 1i abcd efg u ow f sed1 file gt filenew A m Q Q an Example Using sed Replace IInes 13 by Lines 13 are censored nnno n quotD cat sed2 13c Lines 15 are censored cat dummy2 one two three four five six sed f sed2 dummy2 Lines 13 are censored four five six A m Q Q an Example Using sed Multiple commands replaces individual lines cat sed3 1c Line 1 is censored 20 Line 2 is censored 3c Line 3 is censored sed f sed3 dummy2 Line 1 is censored Line 2 is censored Line 3 is censored four five six nnno n quotD A m Q Q an Example Using sed sed 39r file39 f gt g appends file at end of file f sed e 39squotltlt 39 e 39s gtgt39 file multiple commands e option is optional means script on command line 70 560 e 39SAltlt 39 e 39S gtgt39 dummy2 ltlt one gtgt ltlt two gtgt ltlt three gtgt ltlt four gtgt ltlt five gtgt ltlt six gtgt nnno n quotD A m Q Q an Translate Utility tr tr cds string1 stringZ maps all characters in std input from character set string1 to the corresponding character in stringZ If length of stringZ is less the last character is repeated A m Q Q an Translate Utility tr tr cds string1 stringZ c option causes stringl to be complemented every character not in stringl now is in stringl d option causes every character in stringl to be deleted from std input s option causes every repeated character in stringl to be condensed to one occurrence A m Q Q an Examples of tr tr az AZ lt file1 gt fiIeZ causes lowercase to uppercase conversion c C a X lt file1 gt fiIeZ causes every nona character to be replaced by X including newline tr C az 39O1239 lt file1 gt fiIeZ causes all non alphabetic characters to be replaced by ASCII 12 newline 0 u u aC lt gt causes all a b c to be deleted from file1 A m Q Q an Review Advanced commands Pattern matching grep Sort Comparing cmp diff Archiving tar Scheduling cron at Pattern matching and rocessing awk sed A m Q Q an
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'