Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?

Slides:



Advertisements
Similar presentations
EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
Advertisements

Using Linux Commands Lab 4 1. Create empty files To create an empty file $ touch apple banana grape grapefruit watermelon $ ls -l Using file-matching.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
UNIX command line. In this module you will learn: What is the computer shell What is the command line interface (or Terminal) What is the filesystem tree.
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
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.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
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.
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Workbook 6 – Part 2 The Bash Shell
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
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.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
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.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
Introduction to UNIX (part 2) CSE 2031 Fall March 2016.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
Writing Shell Scripts ─ part 1
Prepared by: Eng. Maryam Adel Abdel-Hady
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
The UNIX Shell Learning Objectives:
CSE 390a Lecture 5 Intro to shell scripting
Using Linux Commands Lab 3.
Linux Shell Script Programming
Writing Shell Scripts ─ part 1
Chapter 3 The UNIX Shells
Introduction to UNIX (part 2)
CSC 4630 Meeting 4 January 29, 2007.
Introduction to UNIX (part 2)
Introduction to Bash Programming, part 3
Introduction to UNIX (part 2)
Presentation transcript:

Introduction to Bash Programming Ellen Zhang

Previous three classes What have we learnt so far ?

Outline Shell command line syntax Shell special characters, metacharacters – Write our own echo command – Use echo to experiment with metacharacters – Quotation to protect metacharacters Practice problems Our first shell script – Passing arguments to shell script – Generate arguments by running a command

Shell command line A command ends with a newline, or a semicolon (;), or an ampersand (&) – date; – date; who – sleep 20&who Sending two commands through a pipe: – date; who | wc – What happens ? – ls –l | grep ^d &

Shell command line Use parenthesis to group commands – (date;who) | wc sleep command: pause for given amount of time, and then return – How to show a message in the console to yourself in 30 minutes ? “get up and do some exercise !” – (sleep 1800; echo “get up and do some exercise!” )& – We will learn how to put this into a loop …

Useful commands tee: copies its input to a file and to its standard output – (date; who) | tee save | wc

Special SHELL characters Characters in the command line that shell interpret them and substitute them – >, >>, <, |, &, ;, ! – #: making the rest of the line comments (must be the first letter of a word) – $: $var means value of variable var – *: match any string of zero or more characters in file name (except those starting with.) – ?, [aeiou], [1-9], [a-z], [^0z],[^a-z]

Remember echo command echo: display a line of text – echo Good morning, everyone ! Writing you own echo program ? – Remember how are command line options passed to a program ? main(int argc, char * argv[]) argc: number of command line arguments, including command itself argv: the arguments

Our own echo program #include main(int argc, char ** argv) { //printf (“Number of arguments:%d\n”,argc); //display all command line arguments, except the // command name itself for (int i=1;i<argc;i++) printf (“%s ”,argv[i]); }

Write a small program A program called pick – Syntax: pick arguments… – Function: pick command presents the arguments one at a time and waits after each for a response. The output of pick is those argument selected by y response. Any other response causes the argument to be discarded.

Some Experiments with echo Let’s now use echo command to experiment with metacharacters – echo Good morning, #everyone! – echo Good morning > everyone – echo < message_file – echo $SHELL, echo $PS1 – echo *, echo *.o If no matching is found, original string is passed on – echo *.pdf

What if I want to display special characters ? What happens if we run echo Hello to the world >> ! Use quotation to tell the shell to leave the metacharacters alone… – echo Hello to the world “>>” ! – echo “Hello to the world >> !” – echo “*”

Different quotation Strongest one: single quote character – Echo ‘***’ – Echo ‘$PS1’ Less strong quote: double quote character – Shell peeks inside for $, `…` and \ – echo “Your PS1 is set to $PS1” To display “Your $PS1 is …” – Echo “Your \$PS1 is set to $PS1” – Backslash,\, protect a single character following it

Echo multiple lines message When using quotes, shell knows the command is not finished when a newline is entered Echo]$ echo "Hello, > world, > I can write my own echo." Hello, world, I can write my own echo. To enter a long command line Echo]$ echo Hello world,\ > I can write my own echo. Hello world,I can write my own echo. Echo]$

Practice Problems Create a file named a*b without using any editor, and then delete the file Start a program named long_job in the background, sleep for 20 seconds and then check if the long_job process is still running or not – Hint: ps command lists all current processes – grep file: search for the given string in the file or standard input

Our first shell script How to find the ten largest file under your directory ? – Commands to use ? Save the command sequence into a file – Run the file script using: bash 10largest bash <10largest

Script: 10largest ls -Rl * | grep ^- | sort -k 5 -nr | head -10

Run script like a command Would be nicer if the command is invoked same way as other commands Default permission for a new created file is uc]$ cat >dd echo Hello World ! uc]$ ls -l dd -rw-r--r-- 1 zhang staff :44 dd

Change file permission Make the file executable – chmod +x 10largest – #all users now have the right to execute the file – chmod u+x 10largest – # the owner can execute the file Remember “file” command ? – What will “file 10largest” return ? – One can add an initial comment line: – #!/bin/bash

Pass argument to script 10largest script does not take arguments – Would be nice if we can specify the directory to check, i.e., not always check current directory – i.e. 10largest path_name We need to access the argument, and call commands with according directory – ls -Rl * path_name| grep ^- | sort -k 5 -nr | head - 10

Shell parameter variables If your script is invoked with parameters, shell set the following variables – $#: the number of parameters – $0: the command/script name – $1: the first parameter given to the script – $2,… – the list of all parameters Example: 10largest2

Script: 10largest2 #echo $# #echo $1 #echo $0 ls -Rl * $1 | grep ^- | sort -k 5 -nr | head -10 What if we call 10largest2 without argument ?

Say we want to generate arguments We have used filename expansion (*,? etc) as a way to generate arguments One can use a command to generate the argument – echo Welcome, `who am i`. The time is `date`. Write to all students – mail `cat student_account.txt` < SampleScript What happens if we enter `date` on command line ?

Use pick to choose from the list Remember our pick program ? Say we want to remove all files in a directory, but want to make sure before we delete each file – rm `pick *` mail `pick \`cat mailinglist`` < sampleCode

Summary Shell command line syntax Shell special characters, metacharacters – Write our own echo command – Use echo to experiment with metacharacters – Quotation to protect metacharacters Our first shell script – Passing arguments to shell script – Generate arguments by running a command