Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Using Linux Commands Lab 4 1. Create empty files To create an empty file $ touch apple banana grape grapefruit watermelon $ ls -l Using file-matching.
Some simple examples A B C Hi First, second, third(with errors) third2 EDIT fun3 func6 looping (week13)
Information Networking Security and Assurance Lab National Chung Cheng University 1 What Linux is? Free Unix Like Open Source Network operating system.
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
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Using Linux Commands 2 Lab#5
Shell Script Examples.
Using Linux Commands 2 Lab#5. Sort command Sort the lines of text files. $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control.
Or CMD/BATCH.  Title this comand makes the cmd prompt’s title whatever you would like it to be.
Chapter 15 Introductory Bash Programming
Introduction to Shell Script Programming
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.
Some simple examples First, second, third(with errors) third2.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
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.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux Operations and Administration
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.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
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.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
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.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Lab 8 Overview Apache Web Server. SCRIPTS Linux Tricks.
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.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts Scheduling Unix jobs Job Management.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
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
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Implementation of a simple shell, xssh
Implementation of a simple shell, xssh (Section 1 version)
Prepared by: Eng. Maryam Adel Abdel-Hady
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Implementation of a simple shell, xssh
Shell Scripting March 1st, 2004 Class Meeting 7.
The Linux Operating System
CSE 303 Concepts and Tools for Software Development
Using Linux Commands Lab 3.
Sarah Diesburg Operating Systems CS 3430
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting

Chapter Nine Advanced Shell Scripting2 Creating and Using Functions The formal definition of a shell function is as follows: name () { list ; } Valid and invalid function definitions: lsl() { ls -l ; } # valid lsl { ls -l ; } # invalid Aliases definition for sh: $> cat mycd cd () { chdir ${1:-$HOME} ; PS1="`pwd`$ " ; export PS1 ; } $> source mycd

Chapter Nine Advanced Shell Scripting3 Function Examples Listing the current value of PATH, with each directory listed on a single line. lspath() { OLDIFS="$IFS" IFS=: for DIR in $PATH ; do echo $DIR ; done IFS="$OLDIFS" } $> lspath | grep "/usr/dt/bin"

Chapter Nine Advanced Shell Scripting4 Function Examples (Cont.) Tailoring Your Path setPath() { PATH=${PATH:="/sbin:/bin"}; for _DIR in do if [ -d "$_DIR" ] ; then PATH="$PATH":"$_DIR" ; fi done export PATH unset _DIR }

Chapter Nine Advanced Shell Scripting5 Function Examples (Cont.) An example invocation: $> setPath /sbin /usr/sbin /bin /usr/bin /usr/ccs/bin It checks to see whether each of its arguments is a directory, and if a directory exists, it is added to PATH.

Chapter Nine Advanced Shell Scripting6 Function parameter passing A parameter is passed to a function as it is passed to shell script. The syntax to define function: function function-name( ) { statement1 statement2 statementN } This function is called from command line or within the shell script as follows: function-name arg1 arg2 arg3 argN

Chapter Nine Advanced Shell Scripting7 Parameter passing example $ vi pass function demo() { echo "All Arguments to function demo(): $*" echo "First argument $1" echo "Second argument $2" echo "Third argument $3" return } # Call the function demo -f foo barpass

Chapter Nine Advanced Shell Scripting8 Return Value Example definition function add_two { (( sum=$1+$2 )) return $sum } Invoking the function add_two 1 3 echo $? $? value returned by last function call or command Function definition must occur before the function is called in the script

Chapter Nine Advanced Shell Scripting9 Sharing Data Between Functions The C shell, csh, provides three commands for quickly moving around in the UNIX file system: popdpushddirs These commands maintain a stack of directories internally and enable the user to add and remove directories from the stack and list the contents of the stack.

Chapter Nine Advanced Shell Scripting10 Implementing dirs dirs() { # save IFS, then set it to : to access the # the items in _DIR_STACK individually. OLDIFS="$IFS" IFS=: # print each directory followed by a space for i in $_DIR_STACK do echo "$i \c“ done...

Chapter Nine Advanced Shell Scripting11 Implementing dirs (Cont.)... # add a new line after all entries in # _DIR_STACK have been printed echo # restore IFS IFS="$OLDIFS" }

Chapter Nine Advanced Shell Scripting12 Implementing pushd pushd() { # set the requested directory, $REQ, to the first argument # If no argument is given, set REQ to. REQ="$1"; if [ -z "$REQ" ] ; then REQ=. ; fi # if $REQ is a directory, cd to the directory # if the cd is successful update $_DIR_STACK # otherwise issue the appropriate error messages if [ -d "$REQ" ] ; then cd "$REQ" > /dev/null 2>&1...

Chapter Nine Advanced Shell Scripting13 Implementing pushd (Cont.)... if [ $? -eq 0 ] ; then _DIR_STACK=“`pwd`:$_DIR_STACK" ; export _DIR_STACK ; dirs else echo "ERROR: Cannot change to directory $REQ." >&2 fi else echo "ERROR: $REQ is not a directory." >&2 fi unset REQ }

Chapter Nine Advanced Shell Scripting14 Implementing popd _popd_helper() { # set the directory to pop to the first argument, if # this directory is empty, issue an error and return 1 # otherwise get rid of POPD from the arguments POPD="$1" if [ -z "$POPD" ] ; then echo "ERROR: The directory stack is empty." >&2 return 1 fi shift...

Chapter Nine Advanced Shell Scripting15 Implementing popd (Cont.)... # if any more arguments remain, reinitalize the directory # stack, and then update it with the remaining items, # otherwise set the directory stack to null if [ -n "$1" ] ; then _DIR_STACK="$1" ; shift ; for i in ; do _DIR_STACK="$_DIR_STACK:$i" ; done else _DIR_STACK= fi...

Chapter Nine Advanced Shell Scripting16 Implementing popd (Cont.)... # if POPD is a directory cd to it, otherwise issue # an error message if [ -d "$POPD" ] ; then cd "$POPD" > /dev/null 2>&1 if [ $? -ne 0 ] ; then echo "ERROR: Could not cd to $POPD." >&2 fi pwd else echo "ERROR: $POPD is not a directory." >&2 fi...

Chapter Nine Advanced Shell Scripting17 Implementing popd (Cont.)... export _DIR_STACK unset POPD } popd() { OLDIFS="$IFS" IFS=: _popd_helper $_DIR_STACK IFS="$OLDIFS" }

Chapter Nine Advanced Shell Scripting18 echo command echo display text or value of variable. echo [options] [string, variables...] Options -nDo not output the trailing new line. -eInterpret the following escaped chars. \c suppress trailing new line \a alert (bell)\b backspace \n new line\r carriage return \t horizontal tab\\ backslash $ echo -e "An apple a day keeps away \a\t\tdoctor\n"

Chapter Nine Advanced Shell Scripting19 Displaying colorful text There are some control chars used with echo This commad prints message in Blue color. $> echo "\033[34m Hello Colorful World!" Hello Colorful World! This uses ANSI escape sequence (\033[34m). \033, is escape character, takes some action [34m escape code sets foreground color to Blue [ is start of CSI (Command Sequence Introduction). 34 is parameter. m is letter (specifies action). General syntax echo -e "\033[escape-code your-message"

Chapter Nine Advanced Shell Scripting20 Displaying colorful text A list of escape-code/action letter or char. Char. Use in CSI h Set the ANSI mode l Clears the ANSI mode m Show characters in different colors or effects such as BOLD and Blink q Turns keyboard num lock, caps lock, scroll lock LED on or off s Stores the current cursor x, y position (col, row position) and attributes u Restores cursor position and attributes

Chapter Nine Advanced Shell Scripting21 Displaying colorful text m understands following parameters. Param. Meaning 0Sets default color scheme (White foreground and Black background), normal intensity, no blinking etc. 1Set BOLD intensity $> echo –e "I am \033[1m BOLD \033[0m Person“ I am BOLD Person 2Set dim intensity $> echo –e "\033[1m BOLD \033[2m DIM \033[0m“ BOLD DIM

Chapter Nine Advanced Shell Scripting22 Displaying colorful text 5Blink Effect $> echo –e "\033[5m Flash! \033[0m“ Flash! 7Reverse video effect i.e. Black foreground and white background bydefault $> echo -e "\033[7m Linux OS! Best OS!! \033[0m“ Linux OS! Best OSl

Chapter Nine Advanced Shell Scripting23 Displaying colorful text 25Disables blink effect 27Disables reverse effect 30 – 37Set foreground color 31->Red, 32->Green,... $> echo -e "\033[31m I am in Red“ I am in Red 40 – 47Set background color $> echo -e "\033[44m Wow!!!" Wow!!!

Chapter Nine Advanced Shell Scripting24 Displaying colorful text q understand following parameters Param.Meaning 0Turns off all LEDs on Keyboard 1Scroll lock LED on and others off 2Num lock LED on and others off 3Caps lock LED on and others off

Chapter Nine Advanced Shell Scripting25 Script execution Provide script as an argument to the shell program (e.g. bash my_script ) Or specify which shell to use within the script First line of script is #!/bin/bash Make the script executable using chmod Make sure the PATH includes the current directory Run directly from the command line No compilation is necessary!