LINUX System : Lecture 6 Shell Programming

Slides:



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

A Guide to Unix Using Linux Fourth Edition
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.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (shell).
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Scripts 4 A shell usually interprets a single line of input, but we can also create a file containing a number of lines of commands to be interpreted.
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
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,
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
Linux+ Guide to Linux Certification, Third Edition
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.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts.
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.
1 Applied CyberInfrastructure Concepts ISTA 420/520 Fall Nirav Merchant Bio Computing & iPlant Collaborative Eric Lyons.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Writing Scripts Hadi Otrok COEN 346.
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.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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.
Shell Control Statements and More
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
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.
1 Writing Shell Scripts Professor Ching-Chi Hsu 1998 年 4 月.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
CIRC Winter Boot Camp 2017 Baowei Liu
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CAP652 Lecture - Shell Programming
LINUX System : Lecture 5 (English-Only Lecture)
Shell Programming.
Linux Shell Script Programming
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

LINUX System : Lecture 6 Shell Programming

Extension of Functionality UNIX is designed so that users can extend the functionality To build new tools easily and efficiently To customize the shell and user interface. To string together a series of Unix commands to create new functionality. To create custom commands that do exactly what we want.

Shell Command Interpreter that turns text that you type (at the command line) in to actions User Interface: take the command from user Shell Programming We often want to do a number of commands together And bundle them up into one new command. Just like a batch file in MS-DOS

Shell scripts Any collection of shell commands can be stored in a file called a shell script. Scripts have variables and flow control statements like other programming languages.

Popular Shells sh Bourne Shell ksh Korn Shell bash Bourne-Again Shell csh,tcsh C Shell (for this course) Shell scripts among those shells are slightly different

shell script Creating a simple shell script Run a shell script A shell script is a file that contains commands that the shell can execute. Any commands you enter in response to a shell prompt. A utility A compiled program Another shell script Control flow commands Run a shell script Enter the script filename on the command line The shell interprets and execute the commands one after another Why shell script? Simply and quickly initiate a complex series of tasks or a repetitive procedure.

Shell script example C Shell #!/bin/csh echo “Current Time - `date`” echo I am `whoami` C Shell

Invoking scripts There are two ways to launch scripts: 1) Direct interpretation csh scriptfile [args …] 2) Indirect interpretation The first line of the file must be #!/bin/csh and the file must be executable (permission). C Shell

Shell Variables Environment Variables (Global) environment variable Used to provide information to programs (Global) environment variable New programs and shells inherit environment variables from their parent shell (Local) shell variable Used only by that shell Not passed to other processes

Environment Variables “env” or “printenv” command Display current environment variables DISPLAY The graphical display to use, e.g. nyssa:0.0 EDITOR The path to your default editor, e.g. /usr/bin/vi GROUP Your login group, e.g. staff HOME Path to your home directory, e.g. /home/frank HOST The hostname of your system, e.g. nyssa IFS Internal field separators, usually any white space (defaults to tab, space and <newline>) LOGNAME The name you login with, e.g. frank PATH Paths to be searched for commands, e.g. /usr/bin:/usr/ucb:/usr/local/bin PS1 The primary prompt string, Bourne shell only (defaults to $) PS2 The secondary prompt string, Bourne shell only (defaults to >) SHELL The login shell you're using, e.g. /usr/bin/csh TERM Your terminal type, e.g. xterm USER Your username, e.g. frank

Set Shell Variables Mostly set automatically when log in setenv set $ setenv NAME value # in C Shell set $ set name = value # in C Shell

Variables To set variables: set X [= value] # processed as a string To unset variables : unset X Variable contents are accessed using ‘$’: echo $PATH C Shell

Array C Shell To create lists: set Y = (abc 1 123) To set a list element: set Y[2] = 3 To view a list element: echo $Y[2] To count the number of variable elements: echo $#Y set fname = prog1 rm ${fname}.c C Shell

Built-in Variables $user -- who am I? $path -- my execution path (list of directories to be searched for executables) $term -- what kind of terminal I am using $status -- a numeric variable, usually used to retun error codes $prompt -- what I am currently using for a prompt $shell -- which shell am I using (usu. either /bin/csh or /bin/sh) % set Will display the variable lists.

Arithmetic (@) command C shell provides arithmetic operaters must be used with the arithmetic (@) command Arithmetic command works only with integers. set count = 5 @ count += 2 echo $count 90

Shell Arithmetic expr op1 math-operator op2 Example % expr 1 + 3 % set A = `expr 3 + $B`

Command arguments A shell script to swap files: #! /bin/csh –f set tmp = $argv[1] cp $argv[2] $argv[1] cp $tmp $argv[2] Arguments : $argv The number of arguments to a script: $#argv -f option says we want fast startup (no read .cshrc) . C Shell

if-then-else if ( expr ) simple-command if ( expr ) then commandlist-1 endif C Shell

if-then-else cont’d An example: if ($#argv != 2) then echo “we need two parameters!“ else set name1 = $argv[1] set name2 = $argv[2] endif C Shell

Loops while ( expr ) commandlist end foreach var ( worddlist ) C Shell

switch C Shell switch ( str ) case string1: commandlist1 breaksw default commandlist endsw C Shell

goto (Considered harmful!) To jump unconditionally: goto label A label is a line such as: label: The classic paper on why not to use goto: Go To Statement Considered Harmful Edsger W. Dijkstra, CACM, March 1968 C Shell

shift command Moves the values in the parameters toward the beginning of the parameter list #!/bin/csh –f echo “There are” $#argv “parameters\n” while ($#argv > 0) echo –n “$argv[1] “ shift end echo “\n” echo “There are now” $#argv “parameters” echo “end of script” C Shell

Input Reading Line by Line % set x = $< This is a line. % echo $x

File Operators -e file : True if file exists -r file : True if file is readable -l file : True if file exists and is a symbolic link -w file : True if file exists and is writable -x file : True if file exists and is executable -o file : True if the user owns it -f file : True if the file exists and is a regular file -d file : True if the file exists and is a directory -s file : True if file exists and has a size greater than zero -z file : True if file length is zero (empty)

Logical operator ! : NEGATE && : logical AND || : logical OR Ex) if (! -e somefile) then # does not exist

Debugging %csh –vx somescript args -v : vervose -x : echoes the commands after all substitutions are made -n : syntax check. No execution

example #!/bin/csh if (-e $argv[1]) then echo $argv[1] exists else echo $argv[1] does not exist and cannot be opened endif # rest of script here C Shell

example #!/bin/csh set sum = 0 echo –n “Enter a number: ” set num = $< while ($num != “”) @ sum += $num echo –n “Enter the next number: ” end echo “\nThe sum of the number is : $sum” C Shell

Guidelines Shell script is better than C program if the problem can be solved by using UNIX commands Why script? Easier to create and modify Easy to debug Good thing to do Use redirection and pipe Do validity check (argument number , type) Check existence of files and directories Display error messages

example #!/bin/csh set j = (1 2 3 4 5) foreach i ($j) echo $i Hello end C Shell

example #!/bin/csh set ary = `cat ary.dat` echo “The whole array : $ary” echo “The number of elements : $#ary” echo “The first element: $ary[1]” echo “The last element: $ary[$#ary]” C Shell

Numeric validation example #!/bin/csh echo $argv[2] > temp grep ‘^[0-9]*$’ temp > /dev/null if ($status != 0) then echo “Month argument is not numeric” exit 1 endif if ($argv[2] < 1 || $argv[2] > 12) then echo “Month argument must be <1…12>” exit 2 echo “Validation is OK. We continue.” C Shell

example C Shell #! /bin/csh -f foreach name ($argv) if ( -f $name ) then echo -n "delete the file '${name}' (y/n/q)?" else echo -n "delete the entire dir '${name}' (y/n/q)? " endif set ans = $< # $< means “read a line” switch ($ans) case n: continue case q: exit case y: rm -rf $name endsw end: C Shell

Exercise 1 Write a shell script that displays the number of files and directories in a given directory format % ./fd_count.csh directory_name

Exercise 2 Write a shell script that removes duplicate words from an input text file. Format % remove_dup.csh in.txt out.txt Four Two One Three Four Two One Three out.txt in.txt