operating systems design unix basics & shell scripting


Post on 02-Jan-2016




2 download

Embed Size (px)




  • What Is UNIX?UNIX is an operating system (OS)Multiuser computer system used by more than one person at the same timeMultithreaded multiple simultaneous use of the same programMultitasking handles more than one job (task) per person at the same time

  • KernelThe kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls. As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.

  • Filesystem Hierarchy StandardOutlines standard locations for files and directoriesGives software developers a consistent context regardless of the distributionHelps users of one system work on another

  • Linux directories defined by FHS

  • FHS: Linux directories

  • Directory Structure

  • Some useful commandscd - change directories (built into shell)wc - word, line, character, and byte countecho - echo characters back (print)passwd - change passwordsort - sort lines of a text filels - lists the contents of your current working directory

  • Some useful commandscp - copy a file(s)ps - status of processes (whats running)pwd - Prints the current directory.kill [-sig] %job Send signal sig to the specified job. sig can be either numeric or symbolic. kill -l lists all available signals. By default, sig is SIGTERM (15).read name - Reads one line from standard input and assigns it to the variable name.

  • Some useful commandsgrep - It searches files for specified words or patterns.$ grep -i science science.txt wc - does a word count Last but one of most important command is MAN

  • Finding fileslocate commandFastest method to search for filesA shortcut to the slocate (or secure locate) commandfind commandUsed to find files using various criteriaSearches the directory tree recursively, starting from a certain directory, for files that meet criteria

  • which command and PATH variablewhich commandUsed to locate files within directories listed in the PATH variablePATH variableStores list of directories searched when commands are executed without an absolute or relative pathname

  • The structure of a mode

  • Interpreting permissions

  • SHELLA shell is a command interpreter.The shell acts as an interface between the user and the kernel. You can put commands in a file and execute them all at once. The script itself is just a series of commands. Comments begin with a hash (#) and continue to the end of the line (the first line is special)

  • #!/bin/shThe first line of any script must begin with #!, followed by the name of the interpreter.When Unix tries to execute the script, it sees the first two characters (#!) and knows that it is a script. It then reads the rest of the line to find out which program is to execute the script.

  • Running a scriptchmod +x {script_name}./{script_name}

  • VariablesVariables do not need to be declared. There is only one type of variable VAR=value Set value to variable$VAR or ${VAR} Use the value of the variableCOLOR=yellow echo This looks $COLORish echo This seems ${COLOR}ish

  • Special variablesCommand-line arguments$1 refers to the first command-line argument (after the name of the script), $2 refers to the second one, and so forth, up to $9. If you have more than nine command-line arguments, you can use the shift command: this discards the first command-line argument, and bumps the remaining ones up by one position: $2 becomes $1, $8 becomes $7, and so forth.

  • Special variables (contd.)The variable $0 (zero) contains the name of the script $* (star) and $@ (at) Each of these expands to a string containing all of the command-line arguments, as if you had used $1 $2 $3... inside double quotes: $* behaves in the normal way, whereas $@ creates a separate double-quoted string $# contains the number of command-line arguments that were given.

  • Other special variables$? gives the exit status of the last command that was executed. $- lists all of the options with which sh was invoked. $$ holds the PID of the current process. $! holds the PID of the last command that was executed in the background $IFS (Input Field Separator) determines how sh splits strings into words.

  • QuotingSingle quotes : anything inside them (except a single quote) is quoted.A backslash inside single quotes also loses its special meaning Double quotes : preserve spaces and most special characters. However, variables and backquoted expressions are expanded and replaced with their value. Backquotes : the expression is evaluated as a command A backslash (\'') removes any special meaning from the character that follows. The backslash is itself special, so to escape it, just double it: \\.

  • Flow control#!/bin/sh myname=`whoami` if [ $myname = root ]; then echo "Welcome to FooSoft 3.0" else echo "You must be root to run this script" exit 1 fi

  • While & Forwhile condition; do commands done for i in foo bar baz "do be do"; do echo "$i" done

  • I/O redirection< filename Connect standard input to the file filename. This allows you to have a command read from the file.> filename Connect standard output to the file filename. This allows you to save the output of a command to a file. If the file does not exist, it is created. If it does exist, it is emptied before anything happens.

  • I/O redirection (contd.)>> filename Connects standard output to the file filename. Unlike >, however, the output of the command is appended to filename.command1 | command2 Creates a pipeline: the standard output of command1 is connected to the standard input of command2.

  • FunctionsWhen a group of commands occurs several times in a script, it is useful to define a function. name () { commands }

  • Useful utilitiesbasename : basename pathname prints the last component of pathname.basename /foo/bar/baz prints bazdirname : dirnamepathname prints all but the last component of pathname, that is the directory part: pathname dirname /foo/bar/baz prints /foo/bar

  • [/bin/[ is another name for /bin/test. It evaluates its arguments as a boolean expression, and exits with an exit code of 0 if it is true, or 1 if it is false

  • [ (contd.)-e filename True if filename exists. -d filename True if filename exists and is a directory.-r filename True if filename exists and is readable.s1 = s2 True if the strings s1 and s2 are identical. s1 != s2 True if the strings s1 and s2 are not identical.n1 -eq n2 True if the numbers n1 and n2 are equal. n1 -ne n2 True if the numbers n1 and n2 are not equal.expr1 -a expr2 True if both expressions, expr1 and expr2 are true.

  • SED (Strem EDitor)What is Sed ? A non-interactive text editor that is called from the unix command line. Input text flows through the program, is modified, and is directed to standard output.

  • SED (contd.)sed -e '/^#/dsed -e 's/foo/bar/g'

  • Regular ExpressionsSed uses regular expressions to match patterns in the input text, and thenperform operations on those patterns. ^matches the beginning of the line $ matches the end of the line .Matches any single character\Escapes any metacharacter that follows, including itself.(character)*Match arbitrarily many occurences of (character)(character)?Match 0 or 1 instance of (character)(character)+Match 1 or more instances of (character)[abcdef]Match any character enclosed in [ ] (in this instance, a b c d e or f)[^abcdef]Match any character NOT enclosed in [ ] (character)\{m,n\}Match m-n repetitions of (character)(character)\{m,\}Match m or more repetitions of (character)(character)\{,n\}Match n or less (possibly 0) repetitions of (character)(character)\{n\}Match exactly n repetitions of (character)\{n,m\} range of occurrences, n and m are integers \(expression\)Group operator.expression1|expression2 Matches expression1 or expression 2. () groups regular expressions

  • Regular Expressions (character classes) The following character classes are short-hand for matching special characters. [:alnum:]Printable characters (includes white space)[:alpha:]Alphabetic characters[:blank:]Space and tab characters[:cntrl:]Control characters[:digit:]Numeric characters[:graph:]Printable and visible (non-space) characters[:lower:]Lowercase characters[:print:]Alphanumeric characters[:punct:]Punctuation characters[:space:]Whitespace characters[:upper:]Uppercase characters[:xdigit:]Hexadecimal digits

  • AWKAwk (and its derivatives, nawk and gawk) is a full-fledged scripting language.Inside sh scripts, it is generally used for its ability to split input lines into fields and print one or more fields.

  • AWK (contd.)

  • AWK (contd.)awk -F : '{print $1, $3 }' /etc/passwd The -F : option says that the input records are separated by colons. By default, awk uses whitespace as the field separator. Sum up column 2 and print the total{total += $2} END { print total}

  • AWK ExamplePrint fields in reverse order one per line{ for (i=NF; i>=1; i--) print $i;}

  • END OF LAB-1


View more >