1 / 39

OPERATING SYSTEMS DESIGN

OPERATING SYSTEMS DESIGN. UNIX BASICS & SHELL SCRIPTING. What Is UNIX?. UNIX is an operating system (OS) Multiuser – computer system used by more than one person at the same time Multithreaded – multiple simultaneous use of the same program

lowri
Download Presentation

OPERATING SYSTEMS DESIGN

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING

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

  3. Kernel • The 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.

  4. Filesystem Hierarchy Standard • Outlines standard locations for files and directories • Gives software developers a consistent context regardless of the distribution • Helps users of one system work on another

  5. Linux directories defined by FHS

  6. FHS: Linux directories

  7. Directory Structure

  8. Some useful commands • cd - change directories (built into shell) • wc - word, line, character, and byte count • echo - echo characters back (print) • passwd - change password • sort - sort lines of a text file • ls - lists the contents of your current working directory

  9. Some useful commands • cp - copy a file(s) • ps - status of processes (what’s 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.

  10. Some useful commands • grep - 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

  11. Finding files • locate command • Fastest method to search for files • A shortcut to the slocate (or secure locate) command • find command • Used to find files using various criteria • Searches the directory tree recursively, starting from a certain directory, for files that meet criteria

  12. which command and PATH variable • which command • Used to locate files within directories listed in the PATH variable • PATH variable • Stores list of directories searched when commands are executed without an absolute or relative pathname

  13. The structure of a mode

  14. Interpreting permissions

  15. SHELL • A 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)

  16. #!/bin/sh • The 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.

  17. Running a script • chmod +x {script_name} • ./{script_name}

  18. Variables • Variables 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 variable • COLOR=yellow echo This looks $COLORish echo This seems ${COLOR}ish

  19. Special variables • Command-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.

  20. 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.

  21. 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.

  22. Quoting • Single 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: \\.

  23. 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

  24. While & For • while condition; do commandsdone • for i in foo bar baz "do be do"; do echo "$i" done

  25. 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.

  26. 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.

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

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

  29. [ • /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

  30. [ (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.

  31. 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.

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

  33. Sed uses regular expressions to match patterns in the input text, and then perform 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

  34. 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

  35. AWK • Awk (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.

  36. AWK (contd.)

  37. 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}

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

  39. END OF LAB-1 LABWORK TIME!...

More Related