Hp education services education.hp.com hp education services education.hp.com 1 HP World/Interex 2002 Linux BASH Shell Programming Chris Cooper (734) 805-2172.

Slides:



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

1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Understanding bash Prof. Chris GauthierDickey COMP 2400, Fall 2008.
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
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.
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 Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
Introduction to Shell Script Programming
Linux in More Detail Shirley Moore CPS5401 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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
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
Unix Shells: C, Bourne, Bourne Again, Korn, and Tenex Shells Presentation by: Katie Harris Andrew Murray.
An Introduction to Unix Shell Scripting
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 8 Shell.
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
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 Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
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)
Chapter 2: Linux & POSIX “She sells bash shells by the C shore”
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
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.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
1 Unix Seminar #1 T.J. Borrelli Lecturer for CS and NSSA February 6th, 2009.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
40 Years and Still Rocking the Terminal!
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Introduction to UNIX CS465. What is UNIX? (1) UNIX is an Operating System (OS). An operating system is a control program that allocates the computer's.
Linux+ Guide to Linux Certification, Second Edition
Agenda The Bourne Shell – Part I Redirection ( >, >>,
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Linux Administration Working with the BASH Shell.
Bash Jerome Lewis Kelly Benson Andrew Kimble. Overview  Shell Language (UNIX based)  Paradigms – Command, Scripting  Has ability to read straight from.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
Shell Features CSCI N321 – System and Network Administration
CSC 352– Unix Programming, Fall 2012
UNIX System Overview.
The Linux Operating System
Basic UNIX OLC Training.
Linux Shell Script Programming
CSC 352– Unix Programming, Fall, 2011
Bash Scripting CS 580U - Fall 2018.
LPI Linux Certification
Presentation transcript:

hp education services education.hp.com hp education services education.hp.com 1 HP World/Interex 2002 Linux BASH Shell Programming Chris Cooper (734) George Vish II (404)

hp education services education.hp.com hp education services education.hp.com 2 BASH the Bourne Again Shell A brief overview Version A.00 U2794S Module 8 Slides

© 2002 Hewlett-Packard Company U2794S A.00 3 Linux Kernel v2.x.x Hardware The Linux Kernel The kernel interacts with low-level system features. The kernel is responsible for – device drivers – memory management – CPU scheduling – implementation of the file system The kernel is Linux. This is the only part of the system controlled by Linus Torvalds.

© 2002 Hewlett-Packard Company U2794S A.00 4 The Shell The shell acts as an intermediary between the user and the kernel. The shell interprets user commands and passes them to the kernel. Many shells are available for Linux; the default is the Bourne-Again SHell, bash. Hardware bash vmlinux

© 2002 Hewlett-Packard Company U2794S A.00 5 Available Shells There are five major shell environments, although others are available: sh The Bourne shell is the the original UNIX shell, written by Steven Bourne. csh The C (California) shell is a shell with syntax similar to the C programming language. ksh The Korn shell is the most widely used commercial Unix shell, derived largely from the Bourne shell. POSIX sh A standardized version of the Korn shell (and the default shell for the HP-UX operating system release since 10.x) bash The Bourne-again shell is the default for Linux, GNU alternative to the Korn shell, with extended features.

© 2002 Hewlett-Packard Company U2794S A.00 6 Using the bash Shell The shell is a non-graphical environment providing a command line interpreter as well as a script execution environment. Shell commands are entered at the command prompt, which can be accessed through a non-graphical login, or a graphical terminal emulator, such as an X-term or the Gnome terminal. The shell has a set of built-in commands with terse syntax. Extra commands may be written and utilized like built-in shell commands.

© 2002 Hewlett-Packard Company U2794S A.00 7 Variables Values can be stored by the shell in variables. Variables can be set by –assigning a value directly # myvar=hello –assigning the output of a command to a variable (command expansion) # myvar='ls’ or myvar=$(ls -a) The value stored in a variable can be accessed with the dollar symbol ($). $myvar The echo command can be used to display text, including the contents of variables. # echo $myvar

© 2002 Hewlett-Packard Company U2794S A.00 8 Using Variables Variables in the shell are not strongly typed. For example, a variable containing the value 23 may be either be treated as the number 23 or as the string “23”. To perform numeric addition: variable3=$((variable1 + variable2)) To perform string concatenation: variable1=$variable2$variable3

© 2002 Hewlett-Packard Company U2794S A.00 9 Environment Variables The shell maintains a set of variables that can be accessed by the shell and by programs that run in it. These variables are part of the shell environment and are called environment variables. Currently set environment variables can be viewed with the env command. To modify an environment variable or add a new variable to the environment, it must be exported. # PATH=$PATH:/usr/newbin # export PATH

© 2002 Hewlett-Packard Company U2794S A Streams (I/O redirection) Input from and output to the terminal is handled through an I/O stream. Streams are treated like files by the shell. The default input and output streams are stdin The standard input stream (file descriptor 0) stdout The standard output stream (file descriptor 1) stderr Error output from commands (file descriptor 2) The output from or input to commands can be redirected to other commands or files. > filename Redirect to a file. >> filename Append to a file. < filename Redirect file contents to a command. | Pipe command output to the input of another command. The pipe (}) is a very powerful tool, as it allows the creation of sophisticated commands from the basic building blocks. For example: # head myfile | tail -150 | more The 2> redirection operator can be used to direct command errors to a different location, such as an error log file, or to discard error messages completely. # some-command 2> errors # some-other-command 2> /dev/null

© 2002 Hewlett-Packard Company U2794S A Aliases Aliases can be used as shortcuts for commonly used complex commands. The shortened alias is replaced with its full text when it appears at the start of a command line. –Current aliases may be displayed by entering: # alias –To create an alias: # alias more=more –An alias may be cancelled with: # unalias RedHat Linux aliases rm, cp, and mv to their interactive modes for root, always to prompting when a file is to be destroyed. Local user aliases may be defined in the $HOME/.bashrc file ( Globally used aliases may be defined in /etc/bashrc)

© 2002 Hewlett-Packard Company U2794S A Process Control When a command is run, it may start several processes. All the currently running processes can be viewed with the ps (or the bash shell built in jobs command). Processes can run in the foreground or background: & Run the command in the background. ^C Terminate the current foreground process. ^Z Suspend the current foreground process. bg Move suspended foreground processes to the background. fg Move the background jobs into the foreground.

© 2002 Hewlett-Packard Company U2794S A Resource Files Every user has a set of resource scripts in their home directory that allows per-user configuration of the Linux environment. User resource files are located in the home directory. The names of these files usually begins with a period (.) character..bash_profile Executes once at login..bashrc Executes every time a shell is started..bash_logout Executes when a shell is closed..bash_history A list of recently entered commands.

© 2002 Hewlett-Packard Company U2794S A Shell Scripts Shell scripts are widely used in the UNIX environment to: – Maintain and monitor the system – Simplify complex processes – Grant restricted permissions to ordinary users – Configure the user login procedure and environment Shell scripts use the same syntax as the command line and can call any executable program, including shell commands, scripts, and applications.

© 2002 Hewlett-Packard Company U2794S A Writing Shell Scripts Shell scripts should begin with a line that defines which interpreter to use for the script. #!/bin/bash (this must be on the very first line of the file) Comments are added with the hash (#) character. Everything to the right of the # is ignored. Commands are run in order from the top to the bottom of the file. Shell scripts include commands for flow control allowing conditional execution and looping. The shell is not a high performance programming language, it is intended as a glue for the built in shell commands and other user or third-party created applications.

© 2002 Hewlett-Packard Company U2794S A Conditional Commands contained within an if statement will only be executed if the condition is met. if [ condition ] then.. elif [ condition ] then.. fi Case statements are a convenient way of expressing actions to handle a list of possible variable values.

© 2002 Hewlett-Packard Company U2794S A Comparing Variables Two sets of comparison operators exist for comparing strings or numbers. String TestNumerical Test =-eq Equals !=-ne Not equal to >-gt Greater than <-lt Less than >=-ge Greater than or equal to <=-le Less than or equal to

© 2002 Hewlett-Packard Company U2794S A File Testing Several conditions exist to test the status of files. -e File exists -d File is a directory -f File is an ordinary file -w File is writable -x File is executable

© 2002 Hewlett-Packard Company U2794S A Loops while loops repeat as long as the condition is true. while [ condition ] do.. done for loops repeat for every value in a list. for instance in $list do.. done

© 2002 Hewlett-Packard Company U2794S A Reading User Input User input is collected one line at a time using the read command. read line Files can also be read a line at a time using a combination of the while and read commands. while read line do.. Done < $file When the end of file marker is reached, read will return false. If user input spans multiple lines, it can be terminated with the Ctrl+ d character.

© 2002 Hewlett-Packard Company U2794S A Processing Arguments The shift command moves all arguments to the left, discarding the first argument. The getopts command is useful for processing UNIX style command flags. $1 $2 bananas pears peaches = 4 apples # fruit

© 2002 Hewlett-Packard Company U2794S A Scripting Tips Always attempt to re-use existing programs and scripts whenever possible. Do not try to improve on the efficiency of shell commands. Try commands on the command line first. When first writing scripts, use the echo command to display the contents of variables instead of running commands that will modify the system state. When tracking errors, use echo to track the flow of execution and # to deactivate suspect portions of code. Solve interesting problems. Practice and have fun!

hp education services education.hp.com hp education services education.hp.com 23 Managing Input and Output Version A.02 H4322S Module 13 Slides

© 2002 Hewlett-Packard Company U2794S A Managing Input/Output This module covers methods of handling input and output in shell script programs. The shell provides several methods to direct the flow of data into and out of command programs: Redirection of input command < data_file Redirection of output command > data_file Redirection of input text command << end_file_marker Piping of output command | next_command Redirection of errors command 2> error_file Run in background command &

© 2002 Hewlett-Packard Company U2794S A Data-Flow File Descriptors 3-9 Standard file descriptors: stdin (0) stdout (1) stderr (2) File descriptors 3 to 9 can be used for input or output Syntax: Open file for reading exec 3< data_file Open file for write/overwrite exec 3> data_file Open file for write/append exec 3>> data file Redirection of output command >&3 Close input file descriptor exec 3<&- Close output file descriptor exec 3>&-

© 2002 Hewlett-Packard Company U2794S A Reading/Writing Using File Descriptors exec 3> /tmp/output.txt Open print -u3 "some data" Use(write) exec 3>&- Close exec 3< /tmp/output.txt Open read -u3 var Use(read) exec 3<&- Close

© 2002 Hewlett-Packard Company U2794S A Passing File Contents to a Script command | script script < data_file text_line while read var

© 2002 Hewlett-Packard Company U2794S A Redirecting to a Loop within a Script Piping Data to a LoopRedirecting Data to a Loop command | while read varwhile read vardo...command_set... donedone < data_file Data is piped into the loop.Data is redirected into the The command must create stdout.loop following the word done.

© 2002 Hewlett-Packard Company U2794S A The set command is used to generate a parameter list. Input lines are, conventionally, read into a variable called line. Input line: list of words while read line set $line Creating Parameter Lists from Input Lines list of words listofwords $1$2$3

© 2002 Hewlett-Packard Company U2794S A Using a here Document command << line 1 line 2 line 3 line 4 EOF