Introduction to Bash Programming, part 3

Slides:



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

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
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.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
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 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.
Shell Script Examples.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
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+ Guide to Linux Certification, Third Edition
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Workbook 6 – Part 2 The Bash Shell
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again 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.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
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.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
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.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Sed. Class Issues vSphere Issues – root only until lab 3.
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.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
 Prepared by: Eng. Maryam Adel Abdel-Hady
Linux Administration Working with the BASH Shell.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Bash Shell Scripting 10 Second Guide.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Part 1: Basic Commands/Utilities
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
What is Bash Shell Scripting?
Pepper (Help from Dr. Robert Siegfried)
UNIX Reference Sheets CSE 2031 Fall 2010.
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
More Shell Programming
Review.
Presentation transcript:

Introduction to Bash Programming, part 3 Ellen Zhang

Outline A review about what we learnt about bash Customize your bash environment list10 script Bash variable

Shell command line Shell command line syntax ls –R –l ~ Command name and options separated by space Command ends with newline, ; and & Command name argument Options (arguments)

Bash special characters Globbing, filename expansion ls *, rm *, etc. Shell expands filename patterns or templates containing special characters Can be turned off: set –f, shopt Shell special characters *, ?, [,], |, >, <, >> Quotations: single quote, double quote

Shell parameter variables If your script is invoked with parameters, shell sets parameter variables $#: the number of parameters $0: the command/script name $1: the first parameter given to the script $2: the second parameter $*: a list of all parameters, seperated by first char in $IFS $@: a list of all parameters, seperated by space Also called positional parameter

Our first shell script: list10 ls -Rl | grep ^- | sort -k 5 -nr | head -10 To make it accept path_name ls -Rl $1 | grep ^- | sort -k 5 -nr | head -10 To make it accept multiple path names ls -Rl $* | grep ^- | sort -k 5 -nr | head -10

Bash programming: Control Structures Similar to other programming language Differs in syntax Control structures in bash if … then … else … fi if … then …elif … else … fi for … in … do … done while … do … done until … do … done case … in … esac

Specifying conditions Shell’s boolean check command: test, or [ if test –f list10 # if [ -f list10 ] then .. fi Important note: the spaces before and after [,] To put if, then in one line: if [ -f list10 ]; then

Conditions that one can “test” File conditionals -d file: true if the file is a directory -e file: true if the file exists -f file: true if the file is a regular file -r file: true if the file is readable; -x,-w … -s file: true if the file has nonzero size more …. Read bash tutorial & manuals

Conditions that one can “test”(cont’d) String comparison string1 == string2: strings are equal -n string: string is not null -z string: string is null Arithmetic comparison: exp1 –eq exp2: true if two expressions are equal exp1 –ne exp2: true if two expressions are not equal Others: –gt, -ge, -lt, -le ! exp1: true if exp1 if false

Special bash scripts

Shell startup file Profile files: executed for login shell /etc/profile: system wide default, setting environment for all shell. $HOME/.bash_profile : user-specific bash environment default settings Initialization files: executed for login and interactive shell /etc/bashrc: system wide function and aliases for bash $HOME/.bashrc: user-specific initialization files

Customize your bash environment Edit the ~/.bash_profile file Print a greeting message with your account name, home directory, date … echo –n “Hello” who am i echo –n “your home is “ pwd Echo –n “today is “ date

To generate nicer output Command substitution: substitute output of a command (sequence) into another context Syntax: enclose using backquote, or $() As argument for another command rm `ls *.o` To set a variable time1=$(date); echo $times1 To be used in “for” construct for file in `ls *`; do … done

Generate adapted welcome msg Examples echo Welcome, `who am i`! Your home is `pwd` echo The time is $(date) You can also use: echo Welcome, $USER

Even nicer output set command, a shell builtin command display current variables, “set” set shell options, “set –f”, “set –n” .. set position parameters, set Hello world; echo $1, $2 Combine command substitution, set set `who am i` echo Welcome, $1! You logged in from $5.

To test your settings Reloggin Type bash to create an another sub-shell Current shell is not altered Run a script from current shell: to change settings of current shell source .bashrc , or . .bashrc

Outline A review about what we learnt about BASH Customize your bash environment list10 script Bash variables

Script: list10 #!/bin/bash #echo $# #echo $1 #echo $0 ls -Rl $* | grep ^- | sort -k 5 -nr | head -10

Handling command line option To accept an optional arguments: list_n [–largest_num] path1 path2 path3 … display 20 largest files under a directory list_n -20 ~zhang/documents If the number argument (starting with -) is not given, then list 10 largest files 20 20

Coming back to list10 To test if first argument is “–” followed by a number: if [[ "$1" == -[0-9]* ]] then echo "list10 with number argument " else echo "list10 without number argument" fi Double brackets allow for pattern matching

Our list10 script if [[ "$1" == -[0-9]* ]] then # list10 with number argument # how to implement this ? else # list10 withoutnot number argument # all arguments are paths ls –Rl $* | grep ^- | sort -k 5 –nr | head -10 fi

Bash Programming: Loop Structure for construct: to loop through a range of values, which can be any set of strings for student in jack, john, alice do echo “Hello, $student” > greeting_$student write $student < greeting_$student done

Final list10 script if [[ "$1" == -[0-9]* ]] then for path in $* do if [ $path != $1 ] path_list="$path_list $path" fi done ls -Rl $path_list | sort -k 5 -nr | head $1 else ls -Rl $* | grep ^- | sort -k 5 -nr | head -10

Another example … Save student account name in a file, all.txt for student in `cat all.txt` do echo “Hello, $student” > greeting_$student grep $student student_rec.txt >>greeting_$student write $student < greeting_$student rm greeting_$student done exit How to avoid using the temporary file, greeting_$student ?

Outline A review about what we learnt about BASH Customize your bash environment list10 script Bash variables

SHELL Variables Different types of variables, “set” Environment variables: HOME, PATH, PS1, PS2 … Parameter variables: $1, $*, … User defined variables: student, … Declare variables by using them, e.g., for path in $* Or $x=1 # variable x is set to 1 Note: no spaces before and after “=“

SHELL Variables Access variable by preceding it with $ $ echo $x Need quote marks if there are spaces $ greeting=“Hello world” # need to quotation $ echo $greeting Hello world

Read variable value from input $ read timeofday Morning $ echo Good $timeofday! Good Morning! $ read greeting Good morning # don’t need to quote $ echo $greeting $ Good morning $ echo “$greeting” is \$greeting. What will be the output ?

Variable’s value type: string Variables values are stored as strings $ number=7+5 $ echo $number 7+5 $ x=2; y=3 $ z1=x+y; z2=$x+$y $ echo $z1 $z2 # What will be the output?

Arithmetic Evaluation To evaluate an arithmetic expression Use expr command $ x=1 $ x=`expr $x + 1` # increment x by 1 Or $ x=$(expr $x+1) Or $ x=$(( $x+1)) Other operations: Comparison: =, <, >, >=, <=, != Arithmetic: +, -, *, /, %

A bash based calculator First, let’s implement addition echo “calculate x+y” echo –n “x=“ read x echo –n “y=“ read y echo “x+y=“ `expr $x + $y `

Exercises: work on the following problems, finish them up at your own time and submit by Monday 8pm.

Compare string variable Remember the conditionals for testing strings: = , !=, -n , -z Exercise #1: Write a script that read from standard input a string, and check if it’s the same as your secret password “secret”; if yes, print out “welcome!”; print out “Go away” if not.

Compare string variable Exercise #2 Use while construct to rewrite the script, so that user can keep on trying until getting it right. while condition do statements done

Exercise 3: A Simple Calculator To perform addition, subtraction, … echo "evaluate binary operation on x,y" echo -n "x=" read x echo -n "op (+, -, *, /, %)" read op echo -n "y=" read y # What’s next ? Hint, case construct Evaluate multiple expressions, until user select to exit

Exercise4: loop10 Write a script that wake up every 5 seconds, print out a message “Get up and do some exercise”. Extend the above script so that it loops for 10 times.

Next two classes here document More advanced shell scripts Regular expression Sed, grep, awk, …