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

25
Introduction to Bash Programming Ellen Zhang

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

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

Introduction to Bash Programming

Ellen Zhang

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

Previous three classes

• What have we learnt so far ?

Page 3: 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

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

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 &

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

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 …

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

Useful commands

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

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

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]

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

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

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

Our own echo program#include <stdio.h>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]);}

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

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.

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

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

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

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 “*”

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

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

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

Echo multiple lines message• When using quotes, shell knows the command is not finished

when a newline is entered[zhang@storm Echo]$ echo "Hello,> world,> I can write my own echo."Hello,world,I can write my own echo.

• To enter a long command line[zhang@storm Echo]$ echo Hello world,\> I can write my own echo.Hello world,I can write my own echo.[zhang@storm Echo]$

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

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 <some_string> file: search for the given

string in the file or standard input

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

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

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

Script: 10largest

• ls -Rl * | grep ^- | sort -k 5 -nr | head -10

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

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@storm uc]$ cat >ddecho Hello World ![zhang@storm uc]$ ls -l dd-rw-r--r-- 1 zhang staff 19 2008-01-24 21:44 dd

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

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

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

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

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

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

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

Script: 10largest2

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

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

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 ?

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

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

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

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