Introduction to Bash Programming - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Introduction to Bash Programming

Description:

Our first shell script. How to find the ten largest file under your directory ? ... If your script is invoked with parameters, shell set the following variables ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 26
Provided by: stormCis
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Bash Programming


1
Introduction to Bash Programming
  • Ellen Zhang

2
Previous three classes
  • What have we learnt so far ?

3
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

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

5
Shell command line
  • Use parenthesis to group commands
  • (datewho) 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

6
Useful commands
  • tee copies its input to a file and to its
    standard output
  • (date who) tee save wc

7
Special SHELL characters
  • Characters in the command line that shell
    interpret them and substitute them
  • gt, gtgt, lt, , , , !
  • 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

8
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

9
Our own echo program
  • include ltstdio.hgt
  • main(int argc, char argv)
  • //printf (Number of argumentsd\n,argc)
  • //display all command line arguments, except
    the
  • // command name itself
  • for (int i1iltargci)
  • printf (s ,argvi)

10
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.

11
Some Experiments with echo
  • Lets now use echo command to experiment with
    metacharacters
  • echo Good morning, everyone!
  • echo Good morning gt everyone
  • echo lt message_file
  • echo SHELL, echo PS1
  • echo , echo .o
  • If no matching is found, original string is
    passed on
  • echo .pdf

12
What if I want to display special characters ?
  • What happens if we run
  • echo Hello to the world gtgt !
  • Use quotation to tell the shell to leave the
    metacharacters alone
  • echo Hello to the world gtgt !
  • echo Hello to the world gtgt !
  • echo

13
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

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

15
Practice Problems
  • Create a file named ab 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 ltsome_stringgt file search for the given
    string in the file or standard input

16
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 lt10largest

17
Script 10largest
  • ls -Rl grep - sort -k 5 -nr head -10

18
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
  • zhang_at_storm uc cat gtdd
  • echo Hello World !
  • zhang_at_storm uc ls -l dd
  • -rw-r--r-- 1 zhang staff 19 2008-01-24 2144 dd

19
Change file permission
  • Make the file executable
  • chmod x 10largest
  • all users now have the right to execute the file
  • chmod ux 10largest
  • the owner can execute the file
  • Remember file command ?
  • What will file 10largest return ?
  • One can add an initial comment line
  • !/bin/bash

20
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

21
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,
  • ,_at_ the list of all parameters
  • Example 10largest2

22
Script 10largest2
  • echo
  • echo 1
  • echo 0
  • ls -Rl 1 grep - sort -k 5 -nr head -10
  • What if we call 10largest2 without argument ?

23
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 lt SampleScript
  • What happens if we enter date on command line ?

24
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 lt sampleCode

25
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
Write a Comment
User Comments (0)
About PowerShow.com