unix-21 week 2 4/5/2005. unix-22 topics functions (contd.) pushd, popd, dirs debugging shell...

65
UNIX-2 1 WEEK 2 4/5/2005

Upload: ferdinand-hampton

Post on 20-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

UNIX-23 Sample Files Download states.txt and statecap.txt from acelinux.tripod.com

TRANSCRIPT

Page 1: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 1

WEEK 2

4/5/2005

Page 2: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 2

TOPICS

• Functions (contd.)• pushd, popd, dirs• Debugging Shell scripts• Scheduling Unix jobs• Job Management

Page 3: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 3

Sample Files

• Download states.txt and statecap.txt from acelinux.tripod.com

Page 4: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 4

cut• Used to extract fields from a line• By defaults, fields are delimited by tab• Syntax: cut –d <delimiter> -f <fields>• e.g. cut –d ‘:’ –f1 /etc/passwd displays 1st

column of file /etc/passwd cut –d ‘:’ –f2 /etc/passwd displays 2nd

column of file /etc/passwd

Page 5: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 5

cut (contd.)more /etc/passwdcut –d’:’ -f1 /etc/passwd > usernamesmore usernames

cut –d’:’ –f1-3 /etc/passwdcut –d’ ‘ –f2 statecap.txtcut –d’ ‘ –f2 statecap.txt > capcities

Page 6: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 6

Examples of cut

Page 7: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 7

Exercise• Write a shell script that displays the

current date and time on separate lines`

Page 8: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 8

dt=`date`day=`echo $dt | cut –d’ ‘ –f1`echo Day is $daymnth=`echo $dt | cut –d’ ‘ –f2`echo Month is $mnthdte=`echo $dt | cut –d’ ‘ –f3`echo date is $dtetme=`echo $dt | cut –d’ ‘ –f4`echo time is $tmeyr=`echo $dt | cut –d’ ‘ –f6’echo year is $yr

Page 9: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 9

day=`date | cut –d’ ‘ –f1`echo Day is $daymnth=`date | cut –d’ ‘ –f2`echo Month is $mnthdte=`date | cut –d’ ‘ –f3`echo date is $dtetme=`date | cut –d’ ‘ –f4`echo time is $tmeyr=`date | cut –d’ ‘ –f6’echo year is $yr

Page 10: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 10

paste

• Command to combine files.• By default, adds a space between fields• e.g. paste /etc/passwd /etc/passwd paste –d “X” /etc/passwd /etc/passwd

Page 11: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 11

paste - examples

more states.txt paste states.txt states.txt > 2states.txtmore 2states.txt

paste states.txt /etc/passwd > junkmore junk

Page 12: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 12

wc• wc – word count• Displays the number of lines, words and

characters in the file(s)• Syntax: wc filenamee.g wc states.txt

Page 13: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 13

wc Options

• wc –l states.txtdisplays # of lines

• wc -m /etc/passwd displays # of characters

• wc –w states.txtdisplays # of words

Page 14: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 14

grep bash /etc/passwd | wc –l # displays # of

users with bash as default shell

grep csh /etc/passwd | wc –l # displays # of

users with csh as default shell

Page 15: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 15

export• Used to make variables available to all

child shell processes• By default, shell variables are available

only the shell that they are defined in• To make shell variables available in

child shell processes, they have to be “exported”

Page 16: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 16

mainscriptA in childscript is

echo A in childscript is $A

A=apple # define a variable Achildscript # call another script. This script

# executes in a child shell process

mainscript

childscript

Page 17: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 17

mainscriptA in childscript is apple

echo A in childscript is $A

export A=apple # define a variable Achildscript # call another script. This script

# executes in a child shell process

mainscript

childscript

Page 18: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 18

source, . (dot command)

• source, . are used to execute scripts in the same shell

• . (dot) is more portable than source i.e. available on almost all versions of all shells

• Can be used from command line or from inside a script

Page 19: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 19

. (cmd-line)

• When invoked on command line, executes the script

. myscript Executes myscript in the current shell

Page 20: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 20

county=fairfaxmyscript

myscript

. myscript or source myscript

echo $county

fairfaxecho $county

Page 21: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 21

export city=centrevillemyscript2

myscript2

./myscript2

echo $city

echo $city

Page 22: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 22

echo Setting county variablecounty=fairfax

myscript

myscript

. myscript or source myscript

echo $county

fairfaxecho $countySetting county variable

Setting county variable

Page 23: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 23

. (within script)

• When used within a script, . executes the called script in the same shell as the main script

• Same effect as if “sourced” script was imported into the main script

Page 24: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 24

echo Setting county variablecounty=montgomery

myscript

echo executing myscriptmyscriptecho Value of county is $county

mymainscript

mymainscriptexecuting myscriptSetting county variable

Value of county is

Page 25: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 25

echo Setting county variablecounty=montgomery

myscript

echo executing myscript. myscriptecho Value of county is $county

mymainscript

mymainscriptexecuting myscriptSetting county variable

Value of county is montgomery

Page 26: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 26

Importing functions

• Functions can be imported into a script using the . command

• Once imported, functions can be used in script as if they were defined in the script.

• Need to import the complete function file• Functions are NOT executed when imported

– only when “called”

Page 27: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 27

sum_fn(){ let result=$1+$2 return $result}diff_fn(){ let result=$1-$2 return $result}

funfile

Page 28: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 28

. funfilesum_fn $1 $2echo sum of $1 and $2 is $?diff_fn $1 $2echo difference of $1 and $2 is $?

mathscript

mathscript 10 30sum of 10 and 30 is 40difference of 10 and 30 is -20

Page 29: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 29

Exercise

• Rewrite the calculator script by writing all the functions in a single file and “importing” that file into the main script

Page 30: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 30

pushd, popd, dirs• Set of commands to “bookmark” working

directories• Traverse directories in order• First In Last Out• pushd dir-name pushes dir-name to

directory stack and changes current directory to dir-name

• popdremoves top directory in stack and changes current dir to the dir popped from stack

Page 31: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 31

pushd, popd, dirs

• dirs lists contents of directory stack• pushd, popd commands also list

contents of directory stack• Used in scripts that require changes to

current-working directory without hard-coding directory name

Page 32: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 32

pushd - examplespushd /tmppwdpushd /usrpwddirspushd /binpwddirs

Page 33: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 33

popd - examplespwdpopddirspwdpopdpwddirspopdpwd

Page 34: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 34

pushd, popd in scripts# scriptpushd somedirectory# we are now in somedirectory…popd# back in original directory…

Page 35: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 35

Shell Debugging

• No debugger nor any debugging commands/constructs

• Cryptic error messages• Use bash options for primitive

debugging

Page 36: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 36

Bash debugging options

-v echoes each command before executing it (before evaluating)

-n checks for syntax errors without actually executing the script (weak check)

-x print each command before executing (after evaluating)

Page 37: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 37

Debugging method-1

• Add option to bash definition in 1st line of script

#!/bin/bash –v…

Page 38: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 38

#!/bin/bash –x

echo Enter passwordread passif [ “$pass” != “magic” ]then echo Wrong passwordelse echo Correct passwordfi

Page 39: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 39

#!/bin/bash –v

echo Enter passwordread passif [ “$pass” != “magic” ]then echo Wrong passwordelse echo Correct passwordfi

Page 40: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 40

#!/bin/bash –n

echo Enter passwordread passif[ “$pass” != “magic” ]then echo Wrong passwordelse echo Correct passwordfi

Page 41: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 41

Debugging Method - 2

• Execute script in cmd-line bash with required option

• Don’t need to hard-code option inside script

$ bash –v myscript

Page 42: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 42

#!/bin/bashecho Enter passwordread passif [ “$pass” != “magic” ]then echo Wrong passwordelse echo Correct passwordfi

$ bash –v myscript

myscript

Page 43: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 43

#!/bin/bashecho Enter passwordread passif [ “$pass” != “magic” ]then echo Wrong passwordelse echo Correct passwordfi

$ bash –x myscript

myscript

Page 44: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 44

#!/bin/bashecho Enter passwordread passif[ “$pass” != “magic” ]then echo Wrong passwordelse echo Correct passwordfi

$ bash –n myscript

myscript

Page 45: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 45

Downloading bash 3.0

Create a temporary directory /tmp/bash mkdir /tmp/bashGo to: http://ftp.gnu.org/gnu/bash/File: bash-3.0-tar.gzDownload file to /tmp/bash

Page 46: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 46

Downloading bash-3.0 (contd.)

cd /tmp/bash cd to directorygunzip bash-3.0.tar.gz Unzip filetar –xvf bash-3.0.tar Un-tar the filecd bash-3.0./configure Run configure scriptmake Run make utility to Build bash binary

ls –l bash Verify bash was built successfullycp bash ~ Copy bash binary to home dir

Page 47: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 47

read (re-visited v3.0+)

• read command can be used to read multiple variables

• -s do not echo input• -n N accept only N chars of input• -p string echo string as prompt before

reading input

Page 48: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 48

read –s –n 6 –p “Enter password” pass accepts 6 char input, does not echo

keystrokes and prompt is “Enter password”

read –p “Enter 2 numbers” x y

accepts 2 values into x and y. If less values are passed, remaining variables are not set

Page 49: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 49

#!/home/acestudent7/bash

read –s –n 6 –p “Enter password” pass

Page 50: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 50

Exercise• Write a script that prompts the user to create

a new 6 character password. • The user must be prompted to reconfirm the

new password. • A success message must be displayed if the

user re-enters the password correctly else a failure message must be displayed.

• The password must not be echoed on the screen.

Page 51: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 51

Enter 6 char passwordRe-enter passwordPassword entered successfully

Enter 6 char passwordRe-enter passwordPasswords do not match

Sample execution of script

Page 52: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 52

dt=`date`day=`echo $dt | cut –d’ ‘ –f1`mnth=`echo $dt | cut –d’ ‘ –f2`dte=`echo $dt | cut –d’ ‘ –f3`tme=`echo $dt | cut –d’ ‘ –f4`yr=`echo $dt | cut –d’ ‘ –f6’bkupdir=/tmp/backup_$day_$month_$tme_yrmkdir $bkupdircp –R ~ $bkupdir

Page 53: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 53

Exercise

• Write a shell script that does the following:

• Creates a directory called backup_<timestamp> in the /tmp/directory

• Copies all files and directories in your home directory to the new directory

Page 54: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 54

Scheduling Jobs - cron

• cron – utility used to execute commands at specified times/dates

• Output/error of commands is e-mailed to user

• Commands are executed with privileges of user who setup the cron-job

Page 55: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 55

crontab

• Command to schedule jobs• Each input line corresponds to a job• Each input line must contain 6 space

separated fields• 1-5 date/time• 6 – command to be executed

Page 56: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 56

crontab options• -l list user’s crontab tasks• -r remove all of user’s crontab tasks• -e edit user’s crontab file

Page 57: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 57

crontab time/date fields1. Minute (0 – 59)2. Hour (0 – 23)3. Day-of-month (1 – 31)4. Month-of-year (1 – 12)5. Day-of-week (0 – 6) (0=Sunday) * all valid valuesComma-separated values Specify range using hyphen6. Command to be executed

Page 58: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 58

crontab - examples00 05 * * 1,2,3,4,5 script1 arg1 5am M-

F45 06 * * 5 somescript 6:45am every Fri00 01 1 * * myscript 1am 1st of every

month

Page 59: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 59

crontab – setting up jobs

crontab* * * * * /home/acestudent7/mybackupscriptCtrl-d

Page 60: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 60

crontab – setting up jobs (contd.)

• crontab – e allows to setup jobs in a editor

• EDITOR variable must be set to a valid editor

• export EDITOR=gedit

Page 61: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 61

crontab – viewing jobs

• crontab –l displays current job list

Page 62: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 62

jobs

• List jobs running in the background

sleep 1000 &jobs

kill %1 stops the first jobkill 1932 stops job with id 1932

Page 63: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 63

wait• Stops script execution until all jobs

running in background have terminated• Or until job with number specified

terminates

…wait 200 # wait till job with process id 200 terminates

Page 64: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 64

$!

• Process id of last back-ground process

find / -name core –print 2> /tmp/err > out &pid=$!…

wait $pidmail user < out

Page 65: UNIX-21 WEEK 2 4/5/2005. UNIX-22 TOPICS Functions (contd.) pushd, popd, dirs Debugging Shell scripts…

UNIX-2 65

Next Week

• Application Development Procedures • Compilers and GNU development tools• Version control demo