Some simple examples A B C Hi First, second, third(with errors) third2 EDIT fun3 func6 looping (week13)

Slides:



Advertisements
Similar presentations
CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
Advertisements

NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
Linux+ Guide to Linux Certification, Second Edition
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
2000 Copyrigths Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 3.
The UNIX Shells 1. What is a Unix shell? 2. A few common shells in the Unix & Linux. A. Bourne shell B. Korn shell C. C shell D. Bash-the default shell.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Some simple examples A B C Hi First, second, third(with errors) third2 EDIT fun3 func6 looping (w13)
CSCI6303 – Principles of I.T. Fall  Student will become familiar with scripting in shell using Linux/Ubuntu  Student will write a script and execute.
System Calls 1.
Introduction to Shell Script Programming
Linux in More Detail Shirley Moore CPS5401 August 29,
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Some simple examples First, second, third(with errors) third2.
Introduction to UNIX / Linux - 11
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
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 Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 6 System Calls OS System.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
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.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CE Operating Systems Lecture 11 Windows – Object manager and process management.
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.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Writing Scripts Hadi Otrok COEN 346.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Lecture 6 Introduction to Process Management COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
WHY AN OPERATING SYSTEM (OS) OS interacts with hardware and manages programs. Programs not expected to know which hardware they will run on. Must be possible.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Introduction to Operating Systems Concepts
SUSE Linux Enterprise Desktop Administration
UNIX System Overview.
Shell Programming.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
CS510 Operating System Foundations
Introduction to Bash Programming, part 3
Lecture 6 Introduction to Process Management
Presentation transcript:

Some simple examples A B C Hi First, second, third(with errors) third2 EDIT fun3 func6 looping (week13)

echo Command Use echo command to display text or value of variable. echo [options] [string, variables...] Displays text or variables value on screen. Options -n Do not output the trailing new line. -e Enable interpretation of the following backslash escaped characters in the strings: \a alert (bell) \b backspace \c suppress trailing new line \n new line \r carriage return \t horizontal tab \\ backslash

Example $ echo -e "An apple a day keeps away \a\t\tdoctor\n"

More about echo Display colorful messages echo “Hello, world!” echo -e "\033[34m Hello Colorful World."

That echo statement uses ANSI escape sequence (\033[34m), above entire string ( i.e. "\033[34m Hello Colorful World." ) is process as follows 1) First \033, is escape character, which causes to take some action. 2) Here it set screen foreground color to Blue using [34m escape code. 3) Then it prints our normal message Hello Colorful World! in blue color.

If we want to display that echo statement in red echo -e "\033[31m Hello Colorful World.“ In general: Syntax echo -e "\033[escape-code your-message" More detail, see file cs315\Echo_sequence

Shell Arithmetic Use to perform arithmetic operations. Syntax: expr op1 math-operator op2 Examples: $ expr $ expr $ expr 10 / 2 $ expr 20 % 3 $ expr 10 \* 3 $ echo `expr 6 + 3`

Double quote, single quote, and back quote echo “Today is `date`” Double quote remove the meaning of the characters except “$” and “\”. echo ‘Today is `date`’ Enclosed in single quotes remains unchanged echo `date` To execute command

The read Statement Use to get input (data from user) from keyboard and store (data) to variable. Syntax: read variable1, variable2,...variableN

env command Displays your UNIX environment variables. env ENTER

Variable assignment Variables are assigned in a script program as follows: DONE=no They are used in this manner: while [ $DONE = no ] in your script

#! /bin/bash DONE=no ENTRIES="hello bye ls 1" while [ $DONE = no ] do echo Valid entries are: $ENTRIES read ENTRY # Read the variable ENTRY from the user case $ENTRY in 1) pwd ;; hello) echo How are you? ;; bye) echo exiting... DONE=yes ;; ls) ls -al |more ;; *) echo $ENTRY is an unrecognized command. ;; esac done (looping week14)

/bin and Running Shell Script To run a script, we can: 1../script_Name 2. script_Name 3. sh script_Name In fact, we can run the script with some other ways.

The difference between. Script_Name and./script_Name You can also run a script by using: bash script_Name enter. Script_Name

Create a bin directory mkdir bin under your home directory. From your current directory, Type: cp script_name../bin to copy the script into /bin directory. Or, cp *../bin to copy all your files to the bin directory. Now, you can run them directly by type in the script_Name.

Example Run looping script under current directory cp looping../bin enter Type looping enter This time, you can type in the script name and run it directly.

chsh –Change Shell chsh enter Type in your password enter Type /bin/ksh will change your login shell to Korn shell

What Is Korn Shell? –The Korn shell (/bin/ksh) is the most advanced 'standard' UNIX shell. It extends the Bourne shell with lots of nice features, and is a lot faster.

Korn Shell It was written by David Korn. It is a powerful superset of the Bourne shell. The improvements include : 1. Job control 2. Command line editing 3. Programming features.

Korn Shell Korn is superset of Bourne. zsh claims to be an enhanced Korn shell, while bash has added some parts of it

Aliases in Korn shell The corn shell allows you to create your own commands by using the alias command. Example: $ alias d= `ls –l` in standard Unix Example in In our Linux alias l=ls [Enter] alias AA=“cal 2004”

Exception in using alias in Korn All built-in commands may be aliased except for: case, do, done, elif, else, esac, fi, for, function, if, select, then, time, until, while, {,}

Remove an alias-unalias The unalias will remove all of the specified aliases. format: unalias aliasName [Enter]

Functions in Korn shell Korn shell allows one to define functions that may be invoked as shell commands. Two basic format to define a function in Korn shell: 1. Function name { list }

Function --- cont. 2. Name ( ) { list } This format looks similar to C language format.

Example f ( ) { echo parameter l = $1 echo parameter list = $* } # main program f l f cat dog goat (week13 func6)

Enhanced job control Command : jobs --- Display current jobs bg %job# --- resumes the specified job as a background process. fg %job# --- resumes the specified job as the foreground process. kill

TILDE “~” substitution ~ --- $HOME ~user --- home directory of user Example echo ~ ENTER ~/pathname --- $HOME pathname ~+ --- $PWD (current working directory) ~- --- $OLDPWD (previous working directory)

More let x=y+z written as ( ( x = y + z )) select - allows use of simple menus functions can be written with parameters f( ) { echo $1 $2 $3 } called by f cat dog cow

While … Do… Done #!/bin/ksh message ( ) { echo hi echo there } i=1 while (( i <= 3)) do message #call the function let i=i+1 done (while week13)

Function in General Form function name { list of command } # the keyword function may be omitted

Function in General Form name ( ) { list of command }

Using typeset to declare a local variable Example: fact2 (week13)

Typeset in Korn The typeset command can also be used to assign values, but unless you are setting attributes, it's a lot more work for nothing. If a value is not given, the variable is set to null. Here, X is reassigned the null value: $ X=

Korn Shell Variables and parameters are used by the Korn shell to store values. Like other high-level programming languages, the Korn shell supports data types and arrays. This is a major difference with the Bourne, C shell, and other scripting languages, which have no concept of data types.

Korn Shell The Korn shell supports four data types: string, integer, float, and array. If a data type is not explicitly defined, the Korn shell will assume that the variable is a string.

Integer (–i) Attribute The integer attribute (–i) is used to explicitly declare integer variables. Although it is not necessary to set this attribute when assigning integer values, there are some benefits to it. We'll cover this later in Chapter 6. In the mea NUM is set to an integer-type variable and assigned a value: $ typeset —i NUM=1 $ print $NUM 1

Float (–E, –F) Attribute The float attributes (–E, –F) are used to declare float variables. The –E is used to specify the number of significant digits, while –F is used to specify the precision. We'll cover this later in Chapter 6. In the following example, X is set to a float variable and assigned a value using both formats:

Float Attribute $ typeset —E5 X= $ print $X $ typeset —F5 X= $ print $X

Assigning Values to Variables variable=declare variable and set it to null typeset variable=declare variable and set it to null variable=valueassign value to variable typeset variable=valueassign value to variable

factorial ( ) # one-parameter function { if (( $1 <= 1 )) then echo 1 else typeset tmp typeset result (( tmp = $1 - 1 )) (( result = `factorial $tmp` * $1)) echo $result fi } x=number echo input a number: read $x # factorial $x echo '$? is' $? echo factorial 15 = `factorial 15` echo '$? is ' $?

Kermel 1. The kernel itself is not a process but a process manager. 2. System calls are some specific program constructs that perform the kernel service required by processes. 3. Each system call sets up the group of parameters that identifies the process request and then executes the hardware- dependent CPU instructions to switch from User Mode to Kernel Mode.

Kernel thread---Privileged processes Besides user processes, Unix systems include a few privileged processes ---Kernel thread. 1. They run Kernel Mode in the kernel address space. 2. They do not interact with users, and thus do not require terminal devices. 3. They usually created during startup and remain alive until the system is shut down.

Transition between User Mode and Kernel Mode User Mode Kernel Mode. Process1 Process2 System callTimer interrupt Device interrupt System call handler Scheduler Interrupt handler

Process Implementation To let the kernel manage processes, each process is represented by a process descriptor that includes information about the current state of the process. When kernel stops the execution of a process, it saves the current contents of several processor registers in the process descriptor. These include: PC, SP, General-purpose registers, floating point registers, processor control registers, and the memory management registers.

Device Drivers The kernel interacts with I/O devices by means of device drivers and each driver interacts with the remaining part of the kernel (even with other drivers) through a specific interface. The advantages are: 1. Device-specific code can be encapsulated in a specific module. 2. Vendors can add new devices without knowing the kernel source code: only the interface specifications must be know. 3. The kernel deals with all devices in a uniform way and accesses them through the same interface. 4. Dynamically load and unload device drivers to minimize the kernel image stored in RAM.

Kernel Basis 1. Sharing the CPU and RAM between competing processes. 2. Processing all system calls. 3. Handling all peripherals. The kernel is mainly written in C, and some parts of the kernel was written in assembly language. Users only interact with the kernel by system calls interface.

Kernel

Device Driver Interface tty TapeDisk System call interface Virtual File System Character device filesBlock device files Kernel tty driver Tape driver Disk driver P P PP Disk

Kernel Subsystems 1. Memory Management 2. Process Management 3. Inter-process Communication 4. Input/Output 5. File Management

Kernel Subsystem Summary

Kernel, hardware, and software. Kernel Peripheral Process System calls Hardware interrupt

User Mode and Kernel Mode Normally, when a user process is running, it operates in a special machine mode called user mode. The only way for a user process to enter kernel mode is to execute a system call.

System calls. Open a file open Close a file close Perform I/O read/write Send a signal kill Create a pipe pipe Create a socket socket Duplicate a process fork Terminate a process exit

Hardware Interrupt. Interrupt vector table Highest priority Lowest priority Hardware errors Clock Disk I/O Keyboard Traps (software interrupt) Pointers to kernel Interrupt Handlers

Interrupt can be interrupted! Because of the priority, a lower priority interrupt can be interrupted by a higher priority interrupt.

Screen Saver Script Example # name: lock # function: lock the screen until a correct password is entered # clear echo -e "\n\nEnter your password>" read pword_1 clear echo -e "\n\n This system is locked..... " pword_2= until [ "$pword_1" = "$pword_2" ] do read pword_2 done clear echo "The password you typed in is correct! You can use the computer now!“ week14 lock1

Problems with the lock1 1. Password being displayed while entering it. 2. There are some ways one can enter the system without password. Such as ctrl+c … The reason is that it can cause an interrupt to the kernel and the kernel knows about the device. The kernel interrupt has higher priority than your process.

Some of the shell signals Signal # Name Meaning 1 hang up Terminal connection lost 2 interrupt One of the interrupt key 3 quit One of the quit key 9 kill The kill –9 has been issued 15 terminator The kill command has been issued.

trap command We can use the trap command to disable the signals created by the kernel. Basically, it change the process default action to whatever you specify. Example: trap “optional commands” signal numbers Trap “echo I refuse to die!” 15 display the message instead of terminate the process. (week14 lock3 use the trap command)

Trap cont… If you write trap “ “ [signal] numbers The system just ignore the signals.

A menu-driven application ULIB EDIT REPORTS REPORT_NO ADD DISPLAY UPDATE DELETE

Examples 1. ULIB program 2. EDIT program 3. ADD program