You are on page 1of 11

Shell Scripting

Sreedhar Rachamadugu Sreedhar.rachamadugu@hp.com

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Scheduling Jobs
Batch - Runs script when system resource usage is low. $ batch add_pkg_files Cron $ cron At Eg: Runs script at specified interval

Runs script at specified time. $at 6pm Oct 12 add_pkg_files

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Scheduling Jobs
% 0 0 * 0 % % crontab -l 0 * * * daily-midnight-job.sh * * * * hourly-job.sh * * * * every-minute.sh 1 * * 0 1AM-on-sunday.sh EDITOR=vi crontab e man 5 crontab allowed values -------------0-59 0-23 1-31 1-12 (or names, see below) 0-7 (0 or 7 is Sun, or use names)

field ----minute hour day of month month day of week


3

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Scheduling Jobs(2)
Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12". A field may be an asterisk (*), which always stands for "first-last". # run five minutes after midnight, every day 5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1 # run at 2:15pm on the first of every month -- output mailed to paul 15 14 1 * * $HOME/bin/monthly # run at 10 pm on weekdays, annoy Joe 0 22 * * 1-5 mail -s "Its 10pm" joe%Joe,%%Where are your kids?% 23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday" 5 4 * * sun echo "run at 5 after 4 every sunday"
4 Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Scheduling Jobs(3)
$ man 5 crontab character: @reboot : Run once after reboot. @yearly : Run once a year, ie. "0 0 1 1 *". @annually : Run once a year, ie. "0 0 1 1 *". @monthly : Run once a month, ie. "0 0 1 * *". @weekly : Run once a week, ie. "0 0 * * 0". @daily : Run once a day, ie. "0 0 * * *". @hourly : Run once an hour, ie. "0 * * * *".

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

SED
Stream editor Derivative of ed Takes a sequence of editor commands Goes over the data line by line and performs the commands on each line Basic syntax sed list of ed commands filename[s] ... The commands are applied from the list in order to each line and the edited form is written to stdout Changing a pattern in the le sed s/pat_1/pat_2/g in_file > out_file sed does not alter the contents of the input le

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

SED(2)
Quotes around the list of commands are necessary as the sed metacharacters should not be translated by the shell Selecting range of lines Command to remove the mail header from a saved mail message sed 1,/$/d in_file > out_file Removing the information from the output of the finger command to get only the user id and login time finger | sed s/\([a-zA-Z][a-zA-Z]*\) .* \([0-9][0-9]:[0-9][0-9]\) .*/\1 \2/ Problem: The rst line should have been removed as well finger | sed s/\([a-zA-Z][a-zA-Z]*\) .* \([0-9][0-9]:[0-9][0-9]\) .*/\1 \2/ | sed 1d Indenting a le one tab stop sed s//->/ file The above matches all the lines (including empty lines)
7 Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

SED(3)
Problem can be solved by sed /./s//->/ file Another way to do it sed /$/!s//->/ file Multiple commands in the same invocation of sed $ finger | sed s/\([a-zA-Z][a-zA-Z]*\) .* \([0-9][0-9]:[0-9][0-9]\) .*/\1 \2/ > 1d The commands must be on separate lines sed scripts The sed commands can be put into script les and can be executed by sed -f cmdfile in_file

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

SED(4)
Automatic printing By default, sed prints each line on the stdout This can be inhibited by using the -n option as follows sed -n /pattern/p Matching conditions can be inverted by the ! sed -n /pattern/!p The last achieves the same effect as grep -v Inserting newlines Converting a document from single space to double space $ sed s/$/\> /

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

SED(5)
Creating a list of words used in the document $ sed s/[ ->][ ->]*/\ > /g file Counting the unique words used in the document $ sed s/[ ->,.][ ->,.]*/\ > /g file | sort | uniq | wc -l Writing on multiple les $ sed -n /pat/w file1 > /pat/!w file2 filename

10

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

10

SED(5)
Line numbering Line numbers can be used to select a range of lines over which the commands will operate Examples $ sed -n 20,30p $ sed 1,10d $ sed 1,/$/d $ sed -n /$/,/end/p sed does not support relative line numbers (difference with respect to ed)

11

Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

11

You might also like