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.

Slides:



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

2006-Jan-231 Shell Scripts Jacob Morzinski
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Linux+ Guide to Linux Certification, Second Edition
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
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, or Scripting Shirley Moore CPS 5401 Fall August 29,
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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 UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
An Introduction to Unix Shell Scripting
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
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
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
#!/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.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
A Practical Guide to Fedora and Red Hat Enterprise Linux Unit 5: Scripting in Linux Chapter 27: Programming the Bourne Again Shell By Fred R. McClurg Linux.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
1 Unix Seminar #1 T.J. Borrelli Lecturer for CS and NSSA February 6th, 2009.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Linux+ Guide to Linux Certification, Second Edition
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
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 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
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.
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.
Implementation of a simple shell, xssh
Shell Features CSCI N321 – System and Network Administration
Implementation of a simple shell, xssh
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
Lecture 9 Shell Programming – Command substitution
CSE 303 Concepts and Tools for Software Development
Writing Shell Scripts ─ part 3
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
CHAPTER 8 (skip , ) CHAPTER 10
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Chapter 5 Bourne Shells Scripts
CST8177 Scripting 2: What?.
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

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.

Some Special characters used in shell scripts #:Comments ~:home directory

Invoking the script The first line must be “#!/bin/bash”. –setup the shell path chmod u+x scriptname (gives only the script owner execute permission)./scripname

Some Internal Commands and Builtins getopts: –parses command line arguments passed to the script. exit: –Unconditionally terminates a script set: –changes the value of internal script variables. read: –Reads" the value of a variable from stdin –also "read" its variable value from a file redirected to stdin wait: –Stop script execution until all jobs running in background have terminated

Some Internal Commands and Builtins (cont.) grep: –grep pattern file –search the files file, etc. for occurrences of pattern expr: –evaluates the arguments according to the operation given –y=`expr $y + 1` (same as y=$(($y+1))

I/O Redirection >: Redirect stdout to a file, Creates the file if not present, otherwise overwrites it < : Accept input from a file. >>: Creates the file if not present, otherwise appends to it. <<: –Forces the input to a command to be the shell ’ s input, which until there is a line that contains only label. –cat >> mshfile <<. |:pipe, similar to ">",

if if [ condition ] then command1 elif # Same as else if then command1 else default-command fi

case x=5 case $x in 0) echo "Value of x is 0." ; 5) echo "Value of x is 5." ; 9) echo "Value of x is 9." ; *) echo "Unrecognized value." esac done

Loops for [arg] in [list]; do command done while [condition]; do command... done

Loops (cont.) break, continue –break command terminates the loop –continue causes a jump to the next iteration of the loop

Introduction to Variables $: variable substitution –If variable1 is the name of a variable, then $variable1 is a reference to its value.

Pattern Matching ${variable#pattern} ${variable##pattern} ${variable%pattern} ${variable%pattern}

Examples of Pattern Matching x=/home/cam/book/long.file.name echo ${x#/*/} echo ${x##/*/} echo ${x%.*} echo ${x%.*} cam/book/long.file.name long.file.name /home/cam/book/long.file /home/cam/book/long

Aliases avoiding typing a long command sequence Ex: alias lm="ls -l | more"

Array Declare: –declare -a array_name To dereference (find the contents of) an array variable, use curly bracket notation, that is, ${ array[xx]} refers to all the elements of the array or ${array_name[*]} get a count of the number of elements in an array or ${#array_name[*]}

Functions Type –function function-name { command... } – function-name () { command... } Local variables in function: –Declare: local var_name functions may have arguments –function-name $arg1 $arg2

Positional Parameters $1, $2, $3 ….. $0 is the name of the script. The variable $# holds the number of positional parameter.

Positional Parameters in Functions $1, $2, $3…. Not from $0

Files /etc/profile –systemwide defaults, mostly setting the environment /etc/bashrc –systemwide functions and and aliases for Bash $HOME/.bash_profile –user-specific Bash environmental default settings, found in each user's home directory $HOME/.bashrc –user-specific Bash init file, found in each user's home directory

Debugging The Bash shell contains no debugger, nor even any debugging-specific commands or constructs. The simplest debugging aid is the output statement, echo. Set option –-n: Don’t run command; check for syntax error only –-v: Echo commands before running them –-x: Echo commands after command-line processing