OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
Information Networking Security and Assurance Lab National Chung Cheng University 1 What Linux is? Free Unix Like Open Source Network operating system.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
UNIX command line. In this module you will learn: What is the computer shell What is the command line interface (or Terminal) What is the filesystem tree.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Linux+ Guide to Linux Certification, Second Edition
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Shell Advanced Features. Module 8 Shell Advanced Features ♦ Introduction In Linux systems, the shells are often referred to as command line interfaces.
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
Writing Scripts Hadi Otrok COEN 346.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Linux Filesystem Management
Lesson 5-Exploring Utilities
The UNIX Shell Learning Objectives:
Part 1: Basic Commands/Utilities
Shell Script Assignment 1.
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Introduction Paul Flynn
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
Shell Control Structures
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
LPI Linux Certification
Presentation transcript:

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 Multitasking – handles more than one job (task) per person at the same time

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.

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

Linux directories defined by FHS

FHS: Linux directories

Directory Structure

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

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.

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

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

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

The structure of a mode

Interpreting permissions

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)

#!/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.

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

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

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.

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.

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: \\.

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 & For while 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.

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

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

[ /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 '/^#/d’ sed -e 's/foo/bar/g'

Regular Expressions 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 (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

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.

AWK (contd.) BEGIN actions performed before first input line processed END actions performed after last input line processed

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 Example Print fields in reverse order one per line { for (i=NF; i>=1; i--) print $i; }

END OF LAB-1