510 likes | 623 Views
Unix Seminar #1. T.J. Borrelli Lecturer for CS and NSSA. February 6th, 2009. Processes.1. Types of processes: Foreground Shell waits for task to complete (before next prompt) Task is connected to terminal Shell is task’s parent Background Shell does not wait for task to complete
E N D
Unix Seminar #1 T.J. Borrelli Lecturer for CS and NSSA February 6th, 2009
Processes.1 • Types of processes: • Foreground • Shell waits for task to complete (before next prompt) • Task is connected to terminal • Shell is task’s parent • Background • Shell does not wait for task to complete • Task is connected to terminal • Shell is task’s parent • Daemon • Shell does not wait for task to complete • Task is not connected to terminal • Shell is not task’s parent (parent = PID 1)
Try and Learn • Start the following in the background • sleep 30 & • When your prompt comes back, type ps
Job Control.1 • Shows you the processes that are running in the background, or suspended in the fore/background • Ideal for the ‘multitasking’ • Can have several tasks running at once • Can switch among them at will • Stop one task • Restart another • Can view stopped tasks • Can kill tasks
Job Control.3 • Useful commands: • ^Z (ctrl-z) • Stops a foreground job • Job is frozen in place until controlled • bg • Places the most recently stopped job in the background • As though you had typed ‘&’ at end of the line • Can use bg %N to move job N to background if there is more than one stopped job(NOTE: the number after the % is not a process ID, it is a job number) • jobs • List existing fore/back-ground jobs • Each appears with a job number • Job number IS NOT THE SAME AS process id
Job Control.4 • More job control commands • fg • Bring the most recently stopped job OR background process to the foreground • Can use fg %N to bring job N to the foreground if there is more than one stopped job(NOTE: the number after the % is not a process ID, it is a job number • kill %2 • Kill job 2 … any legitimate job number may be used • Can also … kill 12954 • Kills by process ID • kill -9 12954 kills with extreme prejudice • ^C (ctrl-c) • Control-C kills the foreground job
Simple Shell Script.1and Try and Learn • Using ‘vi’ create a file named ‘firstscript’ • Once created … save the file • At the prompt, typebash firstscriptto run your script #!/bin/bash# First shell scriptecho "List my home directory”ls -l
Simple Shell Script.2 • What is a script? • Text file • First line must be: #!/bin/bash • Contains one Unix command per line • Lines that begin with ‘#’ are comments (except that first one) • When run, the commands are run in the order they appear in the script file
Script Execution.1 • Run the script with the shell • bash scriptname • Run the script as an executable command • Type ‘man chmod’ to learn about chmod • Try: chmod u+x scriptname ./scriptname • NOTE: no need to invoke the shell explicitly • The shell to use is found in the #!/bin/bash line of the script
Script Execution.2 • Scripts are run in subshells • Create a script named myps • Type:ps; bash myps • What is the value of PID and what processes are running? #!/bin/bashecho "My PS"echo "PID=$$"ps lx
Script Execution.3 • When running a script, the shell starts a copy of itself called a subshell • The subshell reads commands directly from the script file Input from terminal shell Fork(copy) Input from script shell
Environment.1 • Scripts execute in an “environment” • Environment = state information • Includes: • Open files • Current directory • User/group • Subshells inherit their environment from the shell that starts them
Environment.2 • Create two scripts called firstScript and secondScript #!/bin/bash # This is called secondScript echo secondScript echo Working Directory pwd echo " " echo User/group id #!/bin/bash # This is called firstScript echo firstScript echo Working Directory pwd echo " " echo User/group id ./secondScript • Make both of these executable and run firstScript • Note: your directory and “id” are the same inside secondScript as in firstScript
Environment.3 • NOW: rerun firstScript as:./firstScript >output • The “>output” sends the output to the file output • This is called redirection as it redirects the output to the file • NOTE: the output of the secondScript is redirected to the file as well that of firstScript
Simple Shell Variables.1 • Assignment • VAR_NAME="a string" • NOTE: name is all upper case … not a requirement, a convention to make variables stand out in scripts • NO spaces around the ‘=‘ … required • The string is inside quotes “…” … required • NO semicolon at end of line … not allowed • Printing • echo "$VAR_NAME" • NOTE: the name is preceded by ‘$’ … required • The $VAR_NAME is in quotes … required sometimes
Simple Shell Variables.2 • variables are not in the environment • i.e., they are not inherited by subshells • Variables can be placed in the environment by exporting them • export VAR_NAME • Exports without setting the value • export ANOTHER_VAR="a string" • Exports and sets the value at once • Modify firstScript and secondScript to try this out
The Command Line.1 • The shell processes commands line-by-line • Each line is read • Then parsed: separated into individual ‘words’ • Then executed: the first ‘word’ is the command to execute and the remaining words (if any) are arguments to the command • The parsing step can be altered by the use of special characters called meta characters
Shell Metacharacters (wildcards) Special characters used to represent some other meanings & Processes a job in background sh MyJobInBackground & ; allows for multiple commands on line echo –n “Enter name: ” ; read Name > Redirects output ls –l > MyFiles >> Same except appends to end of file cd MyOtherDir ; ls –l >> MyFiles < Redirects input read Line < MyFiles ; echo $Line ( ) Starts subshell in which to run command(s) A=5 ; echo $A ; (A=3;echo $A) ; echo $A
Shell Metacharacters (Con’t) • $ Substitutes variables • ? Matches for a single character • * Matches for zero or more characters • [abc] Matches for one character form a set of characters • [!abc] Matches for one character not from a set of characters
Filename Generation.1 • The shell will automatically generate a list of file names for you based on the use of wild card characters • Characters: • * any string of characters of any length • ? any single character • [aeiou] any single character from the set inside the brackets • Can use with echo to find out what files match a pattern • echo a* files starting with ‘a’ • echo *[0-9]* files containing a digit • echo [A-Ka-k]?? Filenames 3 characters long that start with an upper- or lower-case a,b,c,d,…,k
`back ticks` EX: A=‘ls’ ; echo $A A=`ls`; echo $A A=`ls` for F in $A do if [ -f $F ] ; then echo -n $F fi done
Script.1 • Write the following shell script #!/bin/bash # Name this script quotes VAR="Hi There" CMD=date echo Trying with single quotes echo '$VAR `$CMD`' echo " " echo Trying with double quotes echo "$VAR `$CMD`"
I/O Redirection.1 • As discussed earlier, there are three files automatically opened for each process: • I/O redirection is about “hooking” these files up to other files • When a file descriptor is assigned to something other than a terminal, it is called I/O redirection Name File descriptor Purpose ---- --------------- ----------------------- stdin 0 keyboard stdout 1 screen (normal output) stderr 2 screen (error messages)
File descriptor.1 • A small unsigned integer • Used by kernel to reference open file and I/O stream • The first three, 0, 1, and 2 are assigned to your terminal. • A new open file will get a number of ?
First three file descriptors.2 • STDIN = 0 /dev/stdin • STDOUT=1 /dev/stdout • STDERR=2 /dev/stderr
I/O Redirection.2 2 0 1 2 2 0 1 0 1 • We have already seen two types of redirection echo hi there >output • Output (stdout) goes to disk file instead of screen • catfile |tr'a-z''A-Z' • Output goes to stdinof tr instead of screen X echo hi there X cat tr X
I/O Redirection.3 • Appending to a file • cat file1 >>file2 • Redirects output to file2, just as with > • HOWEVER … appends to file2 instead of overwriting • Try this: echo "Today's time and date" >newfile date >>newfile cat newfile
2 0 1 I/O Redirection.4 • Redirecting stderr • cat * >allfiles • If some of the files are not readable, will cause errors to go to screen. • Solution, send stderr (file descriptor 2) to errorfile • cat * >allfiles 2>errorfile errorfile cat allfiles
2 0 1 I/O Redirection.5 cat * >allfiles 2>errorfile If we NEVER want to see the errors we can send them to a special file called /dev/null: cat * > allfiles 2>/dev/null /dev/null - is the “bit bucket” It’s like a black hole from which no data ever returns cat allfiles
Try and Learn • The find command will walk the file system looking for files the match your criteria and then act on them. • find / -name ′*.so’ –print • Finds all files whose names end with ‘.so’ starting at the root(-name ’*.so’) • When it finds one, it prints its name (-print) • Because you do not have permission to all directories, this will generate error messages • Try it as written above and see • Execute this command, discarding error messages
2 0 1 2 0 1 I/O Redirection.5 • Sending stdout and stderr to same place • cat * >bigfile 2>&1 • The 2>&1 says to sendfile descriptor 2 to the same place as 1. • Order counts … consider:cat * 2>&1 >bigfile • Why? At the time we reach2>&1, 1 is stillconnected to the screen cat allfiles cat allfiles
find • Rerun the find command redirecting all of its output to a file • Redirect both normal output and error output • Now, try this: • find / -name '*.so' -print >file1 2>&1 >file2 • what happens above? Can you explain?
2 0 1 I/O Redirection.6 • We can also redirect stdin • tr ‘a-z’ ‘A-Z’ <myfile • Many Unix commands read from stdin by default • Some allow files to be specified on the command line • Others require this form of redirection to read from a file X tr myfile
I/O Redirection.7 • The exec command • exec <file • Opens the file as stdin for the script. Since open files are part of the environment, this is also stdin for all commands, unless explicitly changed • Similarly can redirect other streams • exec >file • exec 3<file1 # file descriptor 3 connected to file1anycommand 0<&3 # connect stdin to file descriptor 3(file1)exec 3<&- # close file descriptor 3 • exec 5>outfile # file descriptor 5 connected to outfilecommand1 # default I/O connectionscommand2 2>&5 # stderr goes to outfilecommand3 1>&5 # stdout goes to outfileexec 5>&- # close outfile
Issuing this command will send all stdout (fd 1) to file temp Sends stdout (fd1) BACK to screen /dev/tty
Pipelines.1 • Each command has three I/O files automatically associated with it. Each has a name and a number (called the file descriptor) • The output of one command can be connected to the input of the next using a pipelinecat file | tr 'a-z''A-Z' • Prints the file … the output goes to ‘tr’ filter which (in this case) translates all lower case characters to their upper case counterparts Name File descriptor Purpose ---- --------------- ----------------------- stdin 0 keyboard stdout 1 screen (normal output) stderr 2 screen (error messages)
103 NOTICE HOW STDOUT OF ONE PROCESS IS CONNECTED TO STDIN OF ANOTHER
Pipelines.2 • Pipelines can be very longcat file | head -75 | tail -10 • Using ‘man’ learn about head and tail • Explain what the above pipeline does, assuming the file has 110 lines of text in it • Pipelines are neat because we don’t have to create a bunch of temporary files! $ cat file > file.1 $ head -75 file.1 > file.2 $ tail -10 file.2 $ rm file.1 file.2
Try and Learn • Type the command: • Using the file ‘file1’ from the last exercise, trycatfile1echo$?You should see a ‘0’ printed • Now, change the permissions so that you do not have read permission for ‘file1’ • Repeat the above test • What is the output this time
Positional Parameters.1 • Shell scripts can receive parameters • The shell parses the command that starts the script into words • Each word, except the script name, is passed to the script as a ‘positional parameter’ • You can get a parameter if you know its position on the line • $1 = 1st parameter, $2 = 2nd parameter, … $9 = ninth parameter, ${10} = tenth parameter. • $10 IS $1 with a “0” appended to it
Try and Learn • Write a script called swap that prints out its first two parameters in reverse order • e.g., swap a b prints <b> <a> • swap Hello “you guys” prints <you guys> <Hello> • The builtin variable $# tells you how many parameters there are • Change swap so that it prints an error message and exits if there are not exactly 2 arguments • Use conditional execution and grouping as in:[ $# -eq 1 ] || { echo Error message; exit 1; }
test command • Arguments indicate what test to perform • Sort of like a Boolean expression • test returns 0 if the test succeeds, 1 if it does not • Alternative syntax to test [ … ] • Example: • iftest-dpathnamethenechoItISadirectoryfi • if [ -dpathname ] # SPACES ARE VERY IMPORTANT HERE! thenechoItISadirectoryfi • Type man test form more types of tests
If statements options • if [ $VAR -eq $VAR2 ]; then …;fi #equal to • if [ $V -ne $V2 ]; then …;fi #not equal to • if [ $V -gt $V2 ]; then …;fi # greater than • if [ $V -lt $V2 ]; then …;fi # less than • if [ $V -ge $V2 ]…#greater than or equal to • if [ $V -le $V2 ]… # less than or equal to
Positional Parameters.2 • If you know how many parameters there are, you can get at all of them using the shift command • shift Deletes $1 and shifts all the rest to the left by 1 place: $1 $2 $3 … • Repeated use gets to all parameters • Special for loop • for VARNAME # executes 1 time/parameter do . . . # $VARNAME changes to next # parameter on each iterationdone
Positional Parameters.3 • In addition to $#, there are two other ‘special’ variables for use with positional parameters • $* list of all parameters • $@ list of all parameters • These appear to be the same • They differ when quoted with double quotes
Try One More • Modify the for loop to • for NAME in "$*"do echo <$NAME>done • Create a script with the above in it and run it • Be sure to include some quoted phrases as well as single words in your parameters • Now … change the for statement tofor NAME in "$@"and try again • How do these to differ? • What if you leave the quotes off? • Which appears to operate the same as the for NAME (by itself)?
‘Here’ Documents.1 • It reads in inline text for a program expecting input • Data is read by using << redirection • Reads data starting at the next line of the script up to a special ‘marker’ line • Example: • echo “Start”cat << EOFThis is the help for my script.Usage: $0 arg1EOF • NOTE: the ‘marker’ is the EOF following the << • The marker must be at the START of the line to be recognized as the END of the ‘document’ • The marker can be ANY word (EOF is not special)
‘Here’ Document.2 • So … • Each line of the ‘document’ is evaluated by the shell • Variables are substituted (and other meta characters are evaluated) • To avoid this, if you wish, single quote the marker • command << ‘EODATA’. . .EODATA • NOW … the lines of the ‘document’ are treated as though they are single quoted (i.e., they are not evaluated by the shell) • NOTE: the marker at the end of the ‘document’ is NOT quoted