Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
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 497C – Introduction to UNIX Lecture 32: - Shell Programming Chin-Chih Chang
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
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
The Bourne Shell 吳 瑞 麟 National Cheng Kung University, Computer and Network Center.
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.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
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.
Introduction to UNIX / Linux - 11
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.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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 11: Shell.
CS465 - UNIX The Bourne Shell.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
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.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
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.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Chapter 15 Introductory Bourne Shell Programming.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
Introduction to the bash Shell (Bourne-Again SHell)
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
Implementation of a simple shell, xssh
Implementation of a simple shell, xssh (Section 1 version)
Implementation of a simple shell, xssh
Shell Scripting March 1st, 2004 Class Meeting 7.
Shell Script Assignment 1.
Writing Shell Scripts ─ part 3
Linux Shell Script Programming
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Review.
Presentation transcript:

Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad

The Shell Definitions, Reserved words, Shell Grammar Creating simple shell scripts Variables definition Shell expansion Built-in shell variables Flow control

Definitions blank A space or tab. word A sequence of characters considered as a single unit by the shell. Also known as a token. name A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier. metacharacterA character that, when unquoted, separates words. One of the following: | & ; ( ) space tab control operator A token that performs a control function. It is one of the following symbols: || & && ; ;; ( ) |

Reserved Words Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or for command: ! case do done elif else esac fi for function if in select then until while { }

Shell Grammar Simple commands Pipelines Lists Compound commands Comments Quoting Parameters Shell variables Expansion Redirection Functions Aliases Job control ||

Creating a Simple Shell Script A shell script is a file that contains commands to be executed by the shell. Making a File Executable – use chmod $ chmod +x scriptname Example: $ cat > whoson date echo Users Currently Logged In who Ctrl+D $ whoson bash: whoson: command not found $ chmod u+x whoson Start whoson now!$ whoson Does not work! $./whoson WHY?

Ways to run a shell script

Command Separation and Grouping Command separator - ; character $ a; b; c (a,b,c - commands) The \ character – when entering very long command line – quotes the New-line character The | and & characters $ a | b | c $ a & b & c a and b in the background Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed. | - pipeline symbol If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

Example 1.Create files a, b and c which echo its names $ cat > a echo “aaaaaaaaaaaaaaaaaaaaaaaa” Ctrl+D 4.Execute now: $./a&./b&./c& 2.Now we will create files b and c using tr command: $ tr 'a' 'b' b $ tr 'a' 'c' c 3.………………??? 5.Explain (comment) the output

Processes Process structure – parents, child and root Example: 1.$ ps –ef |more 2.$ ps -ef | grep your_user_name Discussion ….. Parent process forkes (spawns) a child, child may forks another child, etc.

Variables User-Created variables ( = ) $ person=alex $ echo $person Removing variables – unset or $ person= export command NO spaces around = sign!

The read Command $ cat read2 echo –n Enter command: read command $command echo Thanks $ cat read1 echo –n Go ahead: read firstline echo “You entered: $firstline” $ cat read3 echo -n Enter something: read word1 word2 word3 echo "Word 1 is: $word1" echo "Word 2 is: $word2" echo "Word 3 is: $word3" To accept the user input and store the input in variable – examples: Create script files read1, read2 and read3 and execute the scripts. Explain (comment) the output!

Expansion Expansion is performed on the command line after it has been split into words. There are seven kinds of expansion performed: brace expansion tilde expansion parameter and variable expansion command substitution arithmetic expansion word splitting, and pathname expansion. The order of expansions is: brace expansion, tilde expansion, parameter, variable, command, and arithmetic substitution (done in a left-to-right fashion), word splitting, and pathname expansion.

Tilde expansion If a word begins with a tilde character (`~'), all of the characters preceding the first slash (or all characters, if there is no slash) are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the parameter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead. If a `+' follows the tilde, the value of PWD replaces the tilde and `+'. If a `-' follows, the value of OLDPWD is substituted. If the value following the tilde is a valid login name, the tilde and login name are replaced with the home directory associated with that name. If the name is invalid, or the tilde expansion fails, the word is unchanged.

Command substitution Command substitution allows the output of a command to replace the command name. There are two forms: $(command) or `command` Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Command substitutions may be nested. To nest when using the old form, escape the inner backquotes with back slashes. If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.

Command substitution Example $ cat dir dir=`pwd` echo You are using the $dir directory. ls –l $dir Create script file named dir and execute it. What does the script do? Explain!

Example If you want a for loop that stores several different shell variables and makes one pass through the loop for each set of variables (instead of one pass for each item, as a regular for loop does). This loop does the job: for bunch in "ellie file16" "donna file23" "steve file34" do # PUT FIRST WORD (USER) IN $1, SECOND (FILE) IN $2... set $bunch echo ”mail $1 < $2” done

Example1: $ cat sysmgr #!/bin/sh until who | grep "vkaragiozov " do sleep 60 done echo The Instructor just logged on. $ sh sysmgr & [1] time passes... AUBGIN\vkaragiozov pts/ :52 The Instructor just logged on.

Example: echo “Enter A, B, or C:” read letter case “$letter” in a|A) echo You entered A ;; b|B) echo You entered B ;; c|C) echo You entered C ;; esac