Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.

Slides:



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

CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
● Perl reference
EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
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
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
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.
Using Unix Shell Scripts to Manage Large Data
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Sydney Opera House. Week Three Agenda Administrative Issues Link of the week Review week two lab assignment This week’s expected outcomes Next lab assignment.
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.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
Chapter 6: Shell Programming
A Guide to Unix Using Linux Fourth Edition
An Introduction to Unix Shell Scripting
Week Three Agenda Administrative Issues Link of the Week Review Week Two Information This Week’s Expected Outcomes Next Lab Assignment Break-Out Problems.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
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 System : Lecture 6 Shell Programming
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Shell Programming. 222 Lecture Overview  Shell variables  Shell scripts  Control flow and Boolean operators  Shell programming tips  Shell programming.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
Chapter Four I/O Redirection1 System Programming Shell Operators.
40 Years and Still Rocking the Terminal!
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
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.
Linux Commands C151 Multi-User Operating Systems.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Sydney Opera House. Week Three Agenda Administrative Issues Link of the week Use of the Virtual Machine Review week two lab assignment This week’s expected.
Linux+ Guide to Linux Certification, Second Edition
A Brief Overview of Unix Brandon Bohrer. Topics What is Unix? – Quick introduction Documentation – Where to get it, how to use it Text Editors – Know.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Shell Control Structures CSE 2031 Fall June 2016.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
Homework / Exams HW7 due today Exam #3 next class
Lesson 5-Exploring Utilities
Part 1: Basic Commands/Utilities
Sydney Opera House.
CSE 303 Concepts and Tools for Software Development
INTRODUCTION TO UNIX: The Shell Command Interface
Chapter Four UNIX File Processing.
UNIX Reference Sheets CSE 2031 Fall 2010.
More advanced BASH usage
Shell Programming.
Shell Control Structures
Presentation transcript:

Unix Shell Scripts

What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn c-shell scripts)

How they differ from C programs ? C programs are compiled into machine code (executables) Executables are not portable, but they run faster (no translation during runtime)

What to use ? Use scripts for lightweight operations: –Simple manipulations on files –Sending mails –Network communication –Rapid development Use c programs for: –CPU bound computations –Complex data structures –Critical time applications –Embedded applications

Some useful c-shell commands head / tail – returns first / last lines of a file echo – print command sort – used to sort files in lexicographic order cat – concatenate files and print grep – find regular expressions in files find – find files by criteria wc – word count on files diff - find differences between files basename / dirname – extract file / directory name from full path touch – change file timestamp mail – sending mail whereis – locate program

Redirection and pipes prog redirection file > : redirect stdout >> : append stdout >& : redirect stdout and stderr >>& : append stdout and stderr < : redirect stdin prog1 pipe prog2 | : redirect stdout of prog1 to stdin of prog2 |& : same, with stdout and stderr

How to write a c-shell script Edit the code file (no mandatory extension) Make sure the first line in the file is: #!/bin/csh –f (for c-shell scripts) #!/bin/awk –f (for awk scripts) Add executable permission: chmod +x filename

#!/bin/csh –f ################ #simple_example # ################ # We can define variables and print their values set course = soft1 echo $course # We can do the same for arrays set names = ( Danny Dina Eyal Ayelet Ori Neta ) echo $names echo $#names # size of array echo $names[2] # the second element echo $names[2-] # starting from the second element echo $names[-2] # until the second element echo $names[2-3] # elements 2,3

# at the beginning of the line - treat as an arithmetic expression and not # as a num = 17 echo num -= 3 echo num *= 14 echo $num # if we want to assign the value of a command to a variable set chars = `wc -l./simple_example` echo $chars # accessing program parameters echo The program name is : $0, the first parameter is $1 and the second is $2 echo The number of parameters \(not including program name\) is $#argv

Loops foreach identifier (set)... end while (condition)... end

Conditional statements if condition then... endif switch (value) case value1: breaksw... default: endsw

Testing files attributes if -op file_name -r : read access -w : write access -x : execute access -e : existence -o : ownership -f : plain file -d : directory -l : link

#!/bin/csh –f ######## # sum # ######## if $#argv == 0 then echo Usage: $0 num1 [num2 num3...] exit 1 sum = 0 foreach sum += $number end echo The sum is : average = $sum / remainder = $sum % $#argv echo The avergae is: $average\($remainder\)

#!/bin/csh -f ############ # sort_files # ############ if $#argv == 0 then echo USAGE: $0 file_names_to_sort echo This command writes on the original files \!\!\! exit 1 endif foreach file($argv) sort $file > $file.tmp mv $file.tmp $file end

#!/bin/csh -f # Biggest_file # INPUT: Directory name # OUTPUT: The file with the biggest number of characters in the given directory if $#argv == 0 then echo USAGE: $0 directory_name exit 1 endif if -d $1 max = 0 foreach file($1/*) if (-r $file && -f $file) then set wc_out = `wc -c $file` if ($wc_out[1] > $max) then set biggest_file = max = $wc_out[1] endif else if !(-r $file) then echo $file unreadable endif end echo The biggest file is $biggest_file echo The number of characters is $max else echo $1 is not a directory endif

#!/bin/csh -f # Modulo3 # INPUT: sequence of integer numbers separated by \n terminated by 0 # OUTPUT: prints the value of each number modulo 3 set num = $< while ($num != modulo = $num % 3 switch ($modulo) case 0: echo 0 breaksw case 1: echo 1 breaksw case 2: echo 2 breaksw default: endsw set num = $< end

Retrieving returned values Returned values (by return, exit) are stored in status environment variable The script: #!/bin/csh -f./prog if $status != 0 then echo "Error occured!" endif runs prog (from current dir) and reports whether an error occurred.

awk Useful utility for file manipulation For details see :

#!/bin/awk -f BEGIN { stud_num = total = high_stud_num = total_high = 0} NF != 3 { print "error in line", FNR, ":", $0 next } { ++stud_num total += $3 if ($3 > 80){ total_high += $3 ++high_stud_num } END { print "Number of students: ", stud_num print "Avergae grade is: ", total / stud_num print "Average of high grades is: ", total_high / high_stud_num}

Running with input : Ron Yael Ran Yoav Tal Output is : error in line 5 : Tal Number of students: 4 Avergae grade is: Average of high grades is: