Chapter 15 Introductory Bash Programming

Slides:



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

CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Chapter 15 Mr. Mohammad Smirat SHELL PROGRAMMING (Bourne Shell)
Linux+ Guide to Linux Certification, Second Edition
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
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.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
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+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
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.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
CS465 - UNIX The Bourne Shell.
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.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 10 Dr. Jerry Shiao, Silicon Valley University.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Writing Scripts Hadi Otrok COEN 346.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
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,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
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.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 12 Dr. Jerry Shiao, Silicon Valley University.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER 8: USER INPUT.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Chapter 15 Introductory Bourne Shell Programming.
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.
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.
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.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Bash Shell Scripting 10 Second Guide.
CIRC Winter Boot Camp 2017 Baowei Liu
Department of Computer Engineering
Lecture 7 Introduction to Shell Programming
Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Agenda Korn Shell Functions Korn Shell Variables
CSE 390a Lecture 5 Intro to shell scripting
CSE 303 Concepts and Tools for Software Development
Pepper (Help from Dr. Robert Siegfried)
CHAPTER 8 (skip , ) CHAPTER 10
LING 408/508: Computational Techniques for Linguists
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
CSE 303 Concepts and Tools for Software Development
Linux Shell Script Programming
Lab 4: Introduction to Scripting
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

Chapter 15 Introductory Bash Programming To introduce the concept of shell programming To describe how shell programs are executed To discuss how command line arguments are passed to shell programs To explain the concept of command substitution To describe some basic coding principles To write and discuss some shell scripts To cover the related commands

Introductory Bash Programming 15.1 Introduction Bash is more than a command interpreter. It has own programming language. A shell program is commonly known as shell script. In order to permit non-sequential execution of the commands in a shell script, Bash also supports program flow control commands (statements) such as if, case, for, while and until.

Introductory Bash Programming 15.2 Running a Bash Script Three ways to run a Bash script: Firstly, you run chmod u+x script_file If you use the bash shell, then you will run the following command: ./script_file If you use the Linux shell other than the bash shell, you can run /bin/bash Secondly, you run /bin/bash script_file Lastly, you run chmod u+x script_file and you can put the following line at the beginning of the script_file and then you run ./script_file #!/bin/bash

Introductory Bash Programming 15.3 Running a Bash Script Table 15.1 Some Important Writable Bash Environment Variables (continued on next page)‏

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ Table 15.1 Some Important Writable Bash Environment Variables (continued from previous page)‏

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ Two types of shell variables are shell environments variables (writable or read-only) and user-defined variables. Table 15.2 Some Important Read-Only Bash Environment Variables

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ One can use the set command to display shell environment variables and user defined variables with their values. The env command can be used to display the former ones with their values. 15.3.1 Controlling The Prompt Table 15.3 Some Useful Prompt Characters and their Descriptions

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ 15.3.1 Controlling The Prompt (Cont'd) Example: $ PS1='\w $ ' ~ $ $PS1='\d $ ' Mon May 12 $ $PS1='\h $ ' pardus $ $ PS1='\u@\h \w \$ ' ctis@pardus ~ $ $ PS1='bash-\v|\d|\w\$ ' bash-3.1|Mon May 12|~$

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ 15.3.2 Variable Declaration Linux users can use the declare and typeset commands to declare variables, initialize them, and set their attributes. Syntax: declare [± options] [name [=value] ] typeset [± options] [name [=value] ] Commonly used options / features: -a each 'name' is an array -f each 'name' is a function -i 'name' is an integer -r mark each 'name' read-only ( can not be turned off by using +x) -x mark each 'name' exported. Note that using + instead of – turns attributes off.

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ 15.3.2 Variable Declaration (cont'd) Examples: $ declare -i age=42 $ declare -rx OS=LINUX $echo $age 42 $echo $OS LINUX $declare OS $declare age $echo $Os

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ 15.3.2 Variable Declaration (cont'd) Examples: In order to display all integer and read-only variables in your environment: $ declare -ir Similarly, $ declare -x $ declare -i $ declare

Introductory Bash Programming 15.3 Running a Bash Script (cont'd)‏ 15.3.2 Variable Declaration (cont'd) One can change the value of a variable by using the name=value syntax For an integer variable, if a noninteger value is assigned, then this variable will get a value of zero. $ declare -i age=22 $ echo $age 22 $ age=”Twenty” For generic variables, any type of value (integer or string) can be assigned. $ name=John $ echo $name John $ name=22

Introductory Bash Programming. 15. 3 Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables Table 15.4 Variable Substitution Operators and Their Descriptions

Introductory Bash Programming. 15. 3 Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd) Example: $ echo $name $ name=Mutlu Mutlu $ echo $place $ echo ${name:-Sacit} ${place:-Ankara} Mutlu Ankara $ echo ${name:+Defined} Defined $ echo ${place:+Not Defined} $ echo ${place:=Istanbul} Istanbul Mutlu Istanbul

Introductory Bash Programming. 15. 3 Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd) The use of single and double quotes, *, and \ in an assignment statement. Example: $ name=Mutlu $ echo $name Mutlu $ name= Mutlu Ankara bash: Ankara: command not found $ name='Mutlu Ankara' Mutlu Ankara $ touch Ankara{1,2,3} $ name= Ankara* Ankara1 Ankara2 Ankara3 $ echo “$name” Ankara* $ echo "All files starting with Ankara: $name " All files starting with Ankara: Ankara* $ echo \$name $name $ echo '$name'

Introductory Bash Programming. 15. 3 Introductory Bash Programming 15.3.3 Reading and Writing Shell Variables (cont'd) Example: $ command=pwd $ $command /home/ctis $ command=hello bash: hello: command not found $ command=”ls -l” ........ $ $command /etc

Introductory Bash Programming 15.3.4 Command Substitution Syntax: $(command) Example: $ command=pwd $ echo “The value of command is: $command.” The value of command is: pwd $ command=(pwd)‏ The value of command is: /home/ctis/Desktop, $ echo “The date and time is $(date).” The date and time is Wed May 14 13:00 EST 2008

Introductory Bash Programming 15.3.5 Exporting Environment Syntax: declare –x [name-list] typeset –x [name-list] export [name-list] Note that you can download the most of scripts given in the following examples from http://www.bilkent.edu.tr/~hmurat/ctis156/scripts.tar.gz Example: $ cat display_name echo $name exit 0 $ name=“Mutlu Ankara” $ bash display_name $ declare –x name=“Mutlu Ankara” Mutlu Ankara $ echo $?

Introductory Bash Programming 15.3.5 Exporting Environment (Cont’d)‏ Example: $ cat export_name #! /bin/bash declare –x name=“Mutlu Ankara” ./display_change_name bash display_name exit 0 $ cat display_change_name echo $name name=“Blue Rose” $ ./export_name Mutlu Ankara Blue Rose $

Introductory Bash Programming 15.3.6 Resetting Variables Syntax unset [name-list] $ declare name=Mutlu place=Ankara $ echo $name $place Mutlu Ankara $ unset name $ echo “$name” $ echo “$place” Ankara ------------------------------------------------------------------------------ $ unset name place $ declare name=Mutlu ; echo “$name” Mutlu $ name= ; echo “$name”

Introductory Bash Programming. 15. 3 Introductory Bash Programming 15.3.7 Creating Read-Only User-Defined Variables Syntax declare -r [name-list] typeset -r [name-list] readonly [name-list] Example: $ declare –r name=Mutlu place=Ankara $ echo $name $place Mutlu Ankara $ name=Guler place=Bolu bash: name: readonly variable bash: place: readonly variable $ readonly $ unset name bash: unset: name: cannot unset: readonly variable

Introductory Bash Programming 15.3.8 Reading From Standard Input Syntax read [options] [name-list] Example: $ cat read_demo #!/bin/bash echo “Enter input: “ read line echo “You entered: $line” echo “Enter another line: “ read word1 word2 word3 echo “The first word is: $word1” echo “The second word is: $word2” echo “The rest of the line is: $word3”

Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts Syntax read [options] [name-list] Example: $ cat cmdargs_demos #!/bin/bash echo The command name is $0. echo “The num. of command line arg. passed as parameters are $#.” echo “These values: $1 $2 $3 $4 $5 $6 $7 $8 $9.” echo “Another way: $@.” echo “Yet Another way: $*.” exit 0 $ ./cmdargs_demo ... ..

Introductory Bash Programming. 15 Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts (Cont'd)‏ Syntax shift [N] Example: $ cat shift_demo #!/bin/bash echo The command program name is $0. echo The arguments are $@. echo The first three arg. are $1 $2 $3. shift exit 0

Introductory Bash Programming. 15 Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts (Cont'd)‏ Syntax set [options] [argument-list] Example: $ date Wed May 14 03:18:04 EEST 2008 $ set $(date) $ echo $@ $ echo $1, $2 and $4

Introductory Bash Programming. 15 Introductory Bash Programming 15.4 Passing Arguments To Shell Scripts (Cont'd)‏ Example: $ cat set-1 #!/bin/bash filename=output.txt set $(ls)‏ echo $@ echo echo $1 $2 echo $1 $2 > $filename shift 1 echo $1 $2 >> $filename

Introductory Bash Programming. 15. 6 Program Control Flow Commands 15 Introductory Bash Programming 15.6 Program Control Flow Commands 15.6.1 The if-then-elif-else-fi Statement Figure 15.1  Semantics of the if-then-fi statement

Introductory Bash Programming. 15. 6 Program Control Flow Commands 15 Introductory Bash Programming 15.6 Program Control Flow Commands 15.6.1 The if-then-elif-else-fi Statement (cont'd) The test command is used to evaluate the expression. Syntax test [expression] [[expression]] Example: Let's display the contents of the if-1 and run it.

Table 15.5 Some Useful Operators for the test Command (continued on next page)‏

Table 15.5 Some Useful Operators for the test Command (continued from previous page)‏

Table 15.5 Some Useful Operators for the test Command (continued from previous page)‏

Figure 15.2  Semantics of the if-then-else-fi statement

Figure 15.3 Semantics of the if-then-elif-else-fi statement Example: Let's display the contents of the if-2 and if-3 and run them.

the contents of the for-1 and for-2 and run them. Figure 15.4  Semantics of the for statement Example: Let's display the contents of the for-1 and for-2 and run them.

Figure 15.5 Semantics of the while statement Example: Let's display the contents of the while-1 and while-demo run them.

Figure 15.6 Semantics of the until statement Example: Let's display the contents of the until-1 and run it.

Figure 15.7  Semantics of the break and continue commands

Figure 15.8 Semantics of the case statement Example: Let's display the contents of the case-demo and run it.